#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
#include <HX711.h>
//alles voor de weegschaal
const int doutPin = A0;  // Pin waar de DOUT-pin van de HX711 is aangesloten
const int sckPin = A1;   // Pin waar de SCK-pin van de HX711 is aangesloten

const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46};  // Pinnen voor de 15 mosfets
int encoderValue;
long int gewichtInGrammen;
HX711 schaal;

int desired_weight = 1000;  // bijvoorbeeld, gewenst gewicht in gram of ml
//1ml =+-1gr 1cl=+-100gr

float kalibratiefactor = 0.42;//moet eerst eens kalibaratie programma uitvoeren
float gewichtGlas = 100.0;  // Gewicht van het glas in grammen
//lcd en encoder
LiquidCrystal_I2C lcd(0x27, 20, 4);  // Adres van het LCD-scherm: 0x27, aantal kolommen: 16, aantal rijen: 2
Encoder myEncoder(24, 23);  // Aansluitingen van de rotary encoder: CLK = 24, DT = 23
const int buttonPin = 22;   // Pin voor de knop op de rotary encoder

int selectedOption = 1;

// Definieer de namen van elke optie
String optionNames[] = {"vb_alles", "cola", "vodka-redbull", "Optie D", "Optie E"};//aanvullen met opties

void setup() {
  Serial.begin(9600);
    //weegschaal opstarten
  schaal.begin(doutPin, sckPin);
  
  for (int i = 0; i < 15; i++) {//relais uitzetten
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], LOW);  // Zet alle relais uit bij het opstarten
  }
  //lcd begin
  lcd.begin(20, 4);
  lcd.print("kies drankje:");
  lcd.setCursor(0, 1);


  pinMode(buttonPin, INPUT_PULLUP);  // Interne pull-up voor de knop
  lcd.setCursor(0, 3);
  lcd.print("plaats uw glas");
  lcd.setCursor(0, 1);

}
void weeg(){
if (schaal.is_ready()) {
    long rawValue = schaal.read();
    float gewicht = (rawValue / kalibratiefactor) - gewichtGlas;
    
    Serial.print("Gewicht: ");
    Serial.print(gewicht);
    Serial.println(" gram");

    // Relais logica met switch case
    gewichtInGrammen = int(gewicht);
} else {
    Serial.println("HX711 niet klaar");
  }
}
void alles_uit(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);

}
void drank1_mosfet(){
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank2_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank3_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank4_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank5_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank6_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank7_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank8_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank9_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank10_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank11_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank12_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, HIGH);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank13_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, HIGH);
  digitalWrite(45, LOW);
  digitalWrite(46, LOW);
}
void drank14_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, HIGH);
  digitalWrite(46, LOW);
}
void drank15_mosfet(){
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(44, LOW);
  digitalWrite(45, LOW);
  digitalWrite(46, HIGH);//254
}



void loop() {
  encoderValue = myEncoder.read() / 4;
  encoderValue = constrain(encoderValue, 1, 5);  // Aantal opties laatste getal ALLEEN aanpassen

  if (encoderValue != selectedOption) {
    lcd.setCursor(0, 1);
    lcd.print("                "); // Zet spaties om de bestaande tekst te wissen
    selectedOption = encoderValue;
    lcd.setCursor(0, 1);
    lcd.print(optionNames[selectedOption - 1]); // Aangezien arrays bij 0 beginnen
    //lcd.setCursor(0, 2);
    //lcd.print("                   "); 
    //lcd.setCursor(0, 2);
    //lcd.print(ingredienten[selectedOption - 1]); // Aangezien arrays bij 0 beginnen
  }

  if (digitalRead(buttonPin) == LOW) {  // Lees de knopstatus met pull-up
    executeAction(selectedOption);
    delay(500);  // Voorkom meerdere kliks
  }
 
}


void executeAction(int option) {
  weeg();
  long rawValue = schaal.read();
gewichtGlas = (rawValue/kalibratiefactor);
Serial.println(gewichtGlas);
  delay(160);  // Wacht 1 seconde voordat de volgende meting wordt uitgevoerd aanpassen per 80ms(aanpassen indien nodig)
  lcd.clear();
  lcd.print(optionNames[option - 1]);
  lcd.setCursor(0,1);
  lcd.print("word uitgevoerd");
  while (gewichtInGrammen < desired_weight) {
    weeg();
    delay(160);//delay kun je aanpassen per 80ms best
    switch (option) {
      case 1:  //te lang mms loopt mis bij hoger geiwht
        desired_weight = 1500; //gewenste gewicht van de mix
        switch (gewichtInGrammen) {// code voor drank te vullen met 100gr van elk soort
          case 0 ... 99: //tot welk gewicht moet hij vullen
            drank1_mosfet();//zet de 1 pomp voor drank aan
            break;
          case 100 ... 199:
            drank2_mosfet();
            break;
          case 200 ... 299:
            drank3_mosfet();
            break;
          case 300 ... 399:
            drank4_mosfet();
            break;
          case 400 ... 499:
            drank5_mosfet();
            break;
          case 500 ... 599:
            drank6_mosfet();
            break;
          case 600 ... 699:
            drank7_mosfet();
            break;
          case 700 ... 799:
            drank8_mosfet();
            break;
          case 800 ... 899:
            drank9_mosfet();
            break;
          case 900 ... 999:
            drank10_mosfet();
            break;
          case 1000 ... 1099:
            drank11_mosfet();
            break;
          case 1100 ... 1199:
            drank12_mosfet();
            break;
          case 1200 ... 1299:
            drank13_mosfet();
            break;
          case 1300 ... 1399:
            drank14_mosfet();
            break;
          case 1400 ... 1499:
            drank15_mosfet();
            break;
          default:
            digitalWrite(relayPins[0], LOW);  // Schakel relais 1 uit als het gewicht minder dan 100 gram is
            alles_uit();
            break;
        }
        break;
      case 2:
        desired_weight = 300; //gewenste gewicht van de mix (gr)
        switch (gewichtInGrammen) {// code voor drank te vullen met 100gr van elk soort
          case 0 ... 60: //tot welk gewicht moet hij vullen
            drank1_mosfet();//zet de 1 pomp voor drank aan
            break;
          case 61 ... 300:
            drank2_mosfet();
            break;
          default:
            digitalWrite(relayPins[0], LOW);  // Schakel relais 1 uit als het gewicht minder dan 100 gram is
            alles_uit();
            break;
        }
        break;
    // Voeg hier extra cases toe voor elke optie
      case 3:
        desired_weight = 281; //gewenste gewicht van de mix
        switch (gewichtInGrammen) {// code voor drank te vullen met 100gr van elk soort
          case 0 ... 60: //tot welk gewicht moet hij vullen
            drank1_mosfet();//zet de 1 pomp voor drank aan
            break;
          case 61 ... 281:
            drank2_mosfet();
            break;
          default:
            digitalWrite(relayPins[0], LOW);  // Schakel relais 1 uit als het gewicht minder dan 100 gram is
            alles_uit();
            break;
        }
        break;
      case 4:  //te lang mms loopt mis bij hoger geiwht
        desired_weight = 1500; //gewenste gewicht van de mix
        switch (gewichtInGrammen) {// code voor drank te vullen met 100gr van elk soort
          case 0 ... 99: //tot welk gewicht moet hij vullen
            drank1_mosfet();//zet de 1 pomp voor drank aan
            break;
          case 100 ... 199:
            drank2_mosfet();
            break;
          case 200 ... 299:
            drank3_mosfet();
            break;
          case 300 ... 399:
            drank4_mosfet();
            break;
          case 400 ... 499:
            drank5_mosfet();
            break;
          case 500 ... 599:
            drank6_mosfet();
            break;
          case 600 ... 699:
            drank7_mosfet();
            break;
          case 700 ... 799:
            drank8_mosfet();
            break;
          case 800 ... 899:
            drank9_mosfet();
            break;
          case 900 ... 999:
            drank10_mosfet();
            break;
          case 1000 ... 1099:
            drank11_mosfet();
            break;
          case 1100 ... 1199:
            drank12_mosfet();
            break;
          case 1200 ... 1299:
            drank13_mosfet();
            break;
          case 1300 ... 1399:
            drank14_mosfet();
            break;
          case 1400 ... 1499:
            drank15_mosfet();
            break;
          default:
            digitalWrite(relayPins[0], LOW);  // Schakel relais 1 uit als het gewicht minder dan 100 gram is
            alles_uit();
            break;
        }
        break;

      default:
      alles_uit();
      }
  }
  delay(100);
  alles_uit();
  lcd.clear();
  lcd.print("drankje is klaar");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("kies drankje:");
  lcd.setCursor(0, 3);
  lcd.print("plaats nieuw glas");
  lcd.setCursor(0, 1);
  lcd.print(optionNames[selectedOption - 1]);
}


NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module