#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
#include <HX711.h>
// Weegschaal en mosfets
const int doutPin = A0;
const int sckPin = A1;
const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46};
HX711 schaal;
// LCD en encoder
LiquidCrystal_I2C lcd(0x27, 20, 4);
Encoder myEncoder(24, 23);
const int buttonPin = 22;
// Kalibratie en instellingen
float kalibratiefactor = 0.42;
float gewichtGlas = 100.0;
int desired_weight = 1000;
long gewichtInGrammen;
int selectedOption = 1;
String optionNames[] = {"vb_alles", "cola", "vodka-redbull", "water", "fanta", "optie 1", "optie 2", "optie 3", "optie 4", "optie 5", "optie 6", "optie 7"};
void setup() {
Serial.begin(9600);
schaal.begin(doutPin, sckPin);
for (int pin : relayPins) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Kies drankje:");
lcd.setCursor(0, 3);
lcd.print("Plaats uw glas");
pinMode(buttonPin, INPUT_PULLUP);
}
void allesUit() {
for (int pin : relayPins) {
digitalWrite(pin, LOW);
}
}
void activeerMosfet(int index) {
allesUit();
if (index >= 0 && index < 15) {
digitalWrite(relayPins[index], HIGH);
}
}
void weeg() {
if (schaal.is_ready()) {
long rawValue = schaal.read();
gewichtInGrammen = (rawValue / kalibratiefactor) - gewichtGlas;
lcd.setCursor(3, 2);
lcd.print(gewichtInGrammen);
lcd.print(" ml");
Serial.print("Gewicht: ");
Serial.print(gewichtInGrammen);
Serial.println(" gram");
} else {
Serial.println("HX711 niet klaar");
}
}
void executeAction(int option) {
lcd.clear();
lcd.print(optionNames[option - 1]);
lcd.setCursor(0, 1);
lcd.print("Wordt uitgevoerd");
// Kalibreer het gewicht van het glas
long rawValue = schaal.read();
gewichtGlas = (rawValue / kalibratiefactor);
Serial.println(gewichtGlas);
while (gewichtInGrammen < desired_weight) {
weeg();
delay(160);
if (option == 1) { // vb_alles (voorbeeldmix)
desired_weight = 1500;
int mosfetIndex = gewichtInGrammen / 100;
activeerMosfet(mosfetIndex);
} else if (option == 2) { // cola
desired_weight = 300;
activeerMosfet((gewichtInGrammen < 60) ? 0 : 1);
} else if (option == 3) { // vodka-redbull
desired_weight = 281;
activeerMosfet((gewichtInGrammen < 60) ? 0 : 1);
} else {
allesUit();
break;
}
}
allesUit();
}
void loop() {
int encoderValue = myEncoder.read() / 4;
encoderValue = constrain(encoderValue, 1, 12);
if (encoderValue != selectedOption) {
selectedOption = encoderValue;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(optionNames[selectedOption - 1]);
}
if (digitalRead(buttonPin) == LOW) {
executeAction(selectedOption);
delay(500);
}
}