Relay not working depending on Sketch

Hello everyone.

I have hit a dead end and struggle to find an explanation for the problem I am facing.

I am working on a larger project, where I mix water with powder. Everything is weighed, measured etc. To test my software while I am working on the hardware in parallel, I use smaller sketches which I then later transfer into the "overall" sketch.

Everything worked fine and I am very close to the finish line, where I face a problem that I have not faced before.

In my "smaller" (in terms of byte and lines) sketch my relays turn on accordingly and everything works.
In my "larger" sketch. I get the same result, however one of the relays gets the signal but does not switch (led on relay is switched on but dimmed). That indicates to me, that it doesn't get enough Voltage. However, it is only one of the relays, the other one works perfectly fine, exchanging them gives me the same result but on the other relay. That proofs that the relays are intact, correctly connected and should be working. When switching to the smaller sketch, they both work fine again. I am using the same setup for both tests, which means all sensors etc. are connected when running both sketches.

I am using a Mega.

Has anyone experienced something similar?

Please see both the sketches below:

#include <FlowSensor.h>
#include <LcdBarGraph.h>
#include <LiquidCrystal.h>
#include "HX711.h"

//Internal NTC Stuff
const long int RT0 = 50000;  // Ω
const int B = 3970;          //  K
const int VCC = 5;           //Supply  voltage
const long int R = 12000;    //R=10KΩ

//Variables
float RT, VR, ln, TX, T0, VRT;

//DC Motor
const int DC_Motor = 6;

//Scale Back Left
#define LOADCELL_DOUT_PIN_1 22
#define LOADCELL_SCK_PIN_1 24
HX711 scale_1;
float calibration_factor_1 = -650;

//Scale Front Left
#define LOADCELL_DOUT_PIN_2 32
#define LOADCELL_SCK_PIN_2 33
HX711 scale_2;
float calibration_factor_2 = -800;

//Scale Back Right
#define LOADCELL_DOUT_PIN_3 25
#define LOADCELL_SCK_PIN_3 23
HX711 scale_3;
float calibration_factor_3 = -700;

//Scale Front Right
#define LOADCELL_DOUT_PIN_4 34
#define LOADCELL_SCK_PIN_4 35
HX711 scale_4;
float calibration_factor_4 = -740;

//Constants
/*
const int One_Cycle = 6000;
const int Scale_Interval = 100;
const int Scale_Pause = One_Cycle - Scale_Interval;
const int Motor_Interval = 4000;
const int Motor_Pause = One_Cycle - Motor_Interval;
*/
const int Desired_Weight = 14; // this is in the variables section in the overall sketch

byte DC_Motor_Status = LOW;
byte Scale_Status = LOW;
byte Powder_Status = LOW;

//Variables
float current_weight = 0;
float last_weight = 0;    // not used in the sketch below
int last_volume;          // not used in the sketch below
int last_temperature;     // not used in the sketch below

unsigned long currentMillis = 0;
unsigned long previous_motor_Millis = 0;
unsigned long previous_scale_Millis = 0;
unsigned long previous_lcd_Millis = 0;


//Thermocouple Stuff
#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO_1 31
#define MAXCS_1 30
#define MAXCLK_1 29


// initialize the Thermocouple
Adafruit_MAX31855 thermocouple_1(MAXCLK_1, MAXCS_1, MAXDO_1);

#define MAXDO_2 26
#define MAXCS_2 27
#define MAXCLK_2 28

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple_2(MAXCLK_2, MAXCS_2, MAXDO_2);

//PID Stuff
#include <PID_v1.h>
#define RELAY_PIN 19
#define RELAY_ON HIGH
#define RELAY_OFF LOW

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp = 30, Ki = 10, Kd = 100;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

//WindowSize DON'T CHANGE FOR NOW!!!
int WindowSize = 250;
int WindowSize_Scale = 500;
int WindowSize_LCD = 300;
unsigned long windowStartTime;
unsigned long windowStartTime_Scale;
unsigned long windowStartTime_LCD;

//LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte LCD_Columns = 20;
byte LCD_Rows = 4;

//Flow Sensor
FlowSensor Sensor(YFS201, 18);
unsigned long timebefore = 0;  // same type as millis(), is not being used again in this sketch


const int Pump = 10;

int variables[2][2] = { { 0, 1 }, { 2, 50 } };  // I want to link to the array in the array that I setup in the Menu file

void count() {
  Sensor.count();
}

void setup() {
  Serial.begin(115200);

  //DC Motor
  pinMode(DC_Motor, OUTPUT);

  //Scale
  scale_1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);
  scale_2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);
  scale_3.begin(LOADCELL_DOUT_PIN_3, LOADCELL_SCK_PIN_3);
  scale_4.begin(LOADCELL_DOUT_PIN_4, LOADCELL_SCK_PIN_4);

  scale_1.set_scale(calibration_factor_1);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale_2.set_scale(calibration_factor_2);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale_3.set_scale(calibration_factor_3);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale_4.set_scale(calibration_factor_4);  // this value is obtained by calibrating the scale with known weights; see the README for details

  scale_1.tare();  // reset the scale to 0
  scale_2.tare();  // reset the scale to 0
  scale_3.tare();  // reset the scale to 0
  scale_4.tare();  // reset the scale to 0

  //PID Stuff
  windowStartTime = millis();
  windowStartTime_Scale = millis();
  windowStartTime_LCD = millis();

  //initialize the variables we're linked to
  Setpoint = 38;  //Temperature is the Setpoint
  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  myPID.SetSampleTime(WindowSize / 10);  // make sure we measure and sample at least 10 times per WindowSize.
  //turn the PID on
  myPID.SetMode(AUTOMATIC);

  //Thermocouple
  thermocouple_1.begin();
  thermocouple_2.begin();

  //NTC
  T0 = 25 + 273.15;

  //Flowmeter
  Sensor.begin(count);

  //Flow Through Heater
  pinMode(RELAY_PIN, OUTPUT);

  //Pump
  pinMode(Pump, OUTPUT);

  //LCD
  lcd.begin(20, 4);

}

void loop() {
  currentMillis = millis();
  Water_Dispensing();
  Powder_Dispensing();
  Update_LCD();
}

void Update_LCD() {

  if (millis() - windowStartTime_LCD > WindowSize_LCD) {  //time to shift the Relay Window
    if (variables[0][0] < variables[1][1] || current_weight > (-1) * Desired_Weight) {
      windowStartTime_LCD += WindowSize_LCD;
      LcdBarGraph lbg0(&lcd, 19, 1, 1);
      lcd.setCursor(6, 0);
      lcd.print("PROGRESS");
      lbg0.drawValue(variables[0][0], variables[1][1]);

      // Volume Display
      // Printing the Vol.: will always be called
      lcd.setCursor(0, 2);
      lcd.print("Vol.: ");
      // Single digit volumes
      if (variables[0][0] < 10) {
        lcd.setCursor(8, 2);
        lcd.print(variables[0][0]);
      }
      // Double digit volumes
      if (variables[0][0] >= 10 && variables[0][0] < 100) {
        lcd.setCursor(7, 2);
        lcd.print(variables[0][0]);
      }
      // Triple digit volumes
      if (variables[0][0] >= 100) {
        lcd.setCursor(6, 2);
        lcd.print(variables[0][0]);
      }

      // Powder Display
      // Printing the Form.: will always be called
      lcd.setCursor(0, 3);
      lcd.print("Form.: ");
      // Single digit Weight
      if (current_weight * (-1) < 10) {
        lcd.setCursor(8, 3);
        lcd.print(current_weight * (-1), 0);
      }
      // Double digit Weight
      if (current_weight * (-1) >= 10 && current_weight * (-1) < 100) {
        lcd.setCursor(7, 3);
        lcd.print(current_weight * (-1), 0);
      }
      // Triple digit Weight
      if (current_weight * (-1) >= 100) {
        lcd.setCursor(6, 3);
        lcd.print(current_weight * (-1), 0);
      }

      // Water Temperature
      // Printing the Temp.: will always be called
      lcd.setCursor(11, 2);
      lcd.print("Temp.: ");
      // Single digit Temp
      if (TX < 10) {
        lcd.setCursor(19, 2);
        lcd.print(TX, 0);
      }
      // Double digit Temp
      if (TX >= 10 && TX < 100) {
        lcd.setCursor(18, 2);
        lcd.print(TX, 0);
      }
      // Triple digit Temp
      if (TX >= 100) {
        lcd.setCursor(17, 2);
        lcd.print(TX, 0);
      }
    } else {
      lcd.clear();
      lcd.setCursor(5, 1);
      lcd.print("PREPERATION");
      lcd.setCursor(8, 2);
      lcd.print("DONE");
    }
  }
}

void Water_Dispensing() {

  if (variables[0][0] < variables[1][1]) {
    digitalWrite(Pump, HIGH);
    Sensor.read();
    variables[0][0] = Sensor.getVolume() * 1000;
    Serial.print("Volume: ");
    Serial.print(variables[0][0]);
    Serial.print("ml --- ");
    //Temperature NTC
    VRT = analogRead(A15);         //Acquisition analog value of VRT
    VRT = (5.00 / 1023.00) * VRT;  //Conversion to voltage
    VR = VCC - VRT;
    RT = VRT / (VR / R);  //Resistance of RT
    ln = log(RT / RT0);
    TX = (1 / ((ln / B) + (1 / T0)));  //Temperature from thermistor
    TX = TX - 273.15;                  //Conversion to Celsius
    Serial.print("NTC Temperature: ");
    Serial.print(TX);
    Serial.print(" [C] --- ");
    //Thermocouple
    double d = thermocouple_2.readCelsius();
    Serial.print("FTH Control Temp. = ");
    Serial.print(d);
    Serial.print("[C]");

    Input = TX;
    myPID.Compute();

    if (millis() - windowStartTime > WindowSize) {  //time to shift the Relay Window
      windowStartTime += WindowSize;
      Serial.println("");
      Serial.print("Relay Window shifted");
      Serial.print(" --- Setpoint: ");
      Serial.print(Setpoint);
      Serial.print(" --- Output: ");
      Serial.print(Output);
    }

    if (Output > millis() - windowStartTime) {
      digitalWrite(RELAY_PIN, RELAY_ON);
      Serial.print(" --- Relay ON ");
      Serial.print(" --- Time: ");
      Serial.print(millis());
      Serial.println(" ms");

    } else {
      digitalWrite(RELAY_PIN, RELAY_OFF);
      Serial.print(" --- Relay OFF");
      Serial.print(" --- Time: ");
      Serial.print(millis());
      Serial.println(" ms");
    }
  } else {
    digitalWrite(Pump, LOW);
    digitalWrite(RELAY_PIN, RELAY_OFF);
  }
}

void Powder_Dispensing() {
  if (millis() - windowStartTime_Scale > WindowSize_Scale) {  //time to read the scales and maybe change the motor status
    windowStartTime_Scale += WindowSize_Scale;
    Serial.println("");
    Serial.print("Current dispensed weight: ");
    current_weight = (scale_1.get_units(1) + scale_2.get_units(1) + scale_3.get_units(1) + scale_4.get_units(1));
    Serial.print(current_weight, 0);
    Serial.print(" [g]");
    if (current_weight > (-1) * Desired_Weight && Powder_Status == LOW) {
      digitalWrite(DC_Motor, HIGH);
      Serial.println(" --- Motor ON");
    } else {
      digitalWrite(DC_Motor, LOW);
      Powder_Status = HIGH;
      Serial.print("Dispensing finished");  //for debugging
      Serial.println(" --- Motor OFF");
    }
  }
}

And not working in this one (warning rather long):

//Included Libraries
//Sensors or Actuators
#include "HX711.h"
#include "Adafruit_MAX31855.h"
#include <SPI.h>
#include <FlowSensor.h>
#include <EncoderButton.h>
//LCD Libraries
#include <LiquidCrystal.h>
#include <LiquidMenu.h>
#include <LcdBarGraph.h>
// Others
#include <EEPROM.h>
#include <PID_v1.h>


//PID
#define RELAY_PIN 19
#define RELAY_ON HIGH
#define RELAY_OFF LOW
//Define Variables we'll be connecting to
//Same for all PIDs
double Kp = 30, Ki = 10, Kd = 100;
//Drink 1
double Setpoint_1, Input_1, Output_1;
PID myPID_1(&Input_1, &Output_1, &Setpoint_1, Kp, Ki, Kd, DIRECT);
double Setpoint_2, Input_2, Output_2;
PID myPID_2(&Input_2, &Output_2, &Setpoint_2, Kp, Ki, Kd, DIRECT);
double Setpoint_3, Input_3, Output_3;
PID myPID_3(&Input_3, &Output_3, &Setpoint_3, Kp, Ki, Kd, DIRECT);

//WindowSize DON'T CHANGE FOR NOW!!!
int WindowSize = 250;
int WindowSize_Scale = 500;
int WindowSize_LCD = 300;
unsigned long windowStartTime;
unsigned long windowStartTime_Scale;
unsigned long windowStartTime_LCD;

//Dispensing Related
bool Powder_Status = LOW;


//Flow Sensor
FlowSensor Sensor(YFS201, 18);
unsigned long timebefore = 0;  // same type as millis(), is not being used again in this sketch


//Pump
const int Pump = 10;


//DC Motor
const int DC_Motor = 6;
unsigned long previous_motor_Millis = 0;

//LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte LCD_Columns = 20;
byte LCD_Rows = 4;
unsigned long previous_lcd_Millis = 0;


//EncoderButton(byte encoderPin1, byte encoderPin2, byte switchPin);
EncoderButton eb1(21, 20, 51);
bool eb1_update_executed = false;
bool switching_focus = true;

enum FunctionTypes {
  increase = 1,
  decrease = 2,
};
/* An enumerator can be used for the callback functions. It sets the number
 * for similar functions. Later when a function is attached it can be passed
 * with this enum rather than a magic number.
 */


//NTC
//Internal NTC Stuff
const long int RT0 = 50000;  // Ω
const int B = 3970;          //  K
const int VCC = 5;           //Supply  voltage
const long int R = 12000;    //R=10KΩ
//Variables
float RT, VR, ln, TX, T0, VRT;


//Thermocouples
//Thermocouple_1
#define MAXDO_1 31
#define MAXCS_1 30
#define MAXCLK_1 29
Adafruit_MAX31855 thermocouple_1(MAXCLK_1, MAXCS_1, MAXDO_1);
//Thermocouple_2
#define MAXDO_2 26
#define MAXCS_2 27
#define MAXCLK_2 28
Adafruit_MAX31855 thermocouple_2(MAXCLK_2, MAXCS_2, MAXDO_2);

//LED
//LED definition
int ledPin = 45;
//LED Variables
bool led_status = 1;
char led_status_text[4];
char led_on[] = " ON";   //There is a syntax error here
char led_off[] = "OFF";  //There is a syntax error here
int ledvalue = 1;
long ledtime = 0;
int ledperiode = 1250;


//Speaker
//Speaker definition
int speakerPin = 13;
// Tone definition
bool tones_status = 1;
int tones_duration = 25;
char tones_status_text[4];
char tones_on[] = " ON";
char tones_off[] = "OFF";
int numTones = 10;
int tones[] = { 261, 277, 294, 311, 330, 349, 370, 392, 415, 440 };
//            mid C  C#   D    D#   E    F    F#   G    G#   A


//Scales
byte Scale_Status = LOW;
unsigned long previous_scale_Millis = 0;
//Scale Back Left
#define LOADCELL_DOUT_PIN_1 22
#define LOADCELL_SCK_PIN_1 24
HX711 scale_1;
float calibration_factor_1 = -650;
//Scale Front Left
#define LOADCELL_DOUT_PIN_2 32
#define LOADCELL_SCK_PIN_2 33
HX711 scale_2;
float calibration_factor_2 = -800;
//Scale Back Right
#define LOADCELL_DOUT_PIN_3 25
#define LOADCELL_SCK_PIN_3 23
HX711 scale_3;
float calibration_factor_3 = -700;
//Scale Front Right
#define LOADCELL_DOUT_PIN_4 34
#define LOADCELL_SCK_PIN_4 35
HX711 scale_4;
float calibration_factor_4 = -740;


//Dispensing and Menu Variables
int variables[14][6] = {
  //      0,  1 ,  2, 3, 4, 5}
  /*0*/ { 0, 250, 10, 5, 0, 0 },   // Drink_1 Actual Water Amount, Max. Water, Min. Water, Increment, Drink_1_Water_ee_address, Current Water Amount
  /*1*/ { 0, 90, 25, 1, 2, 0 },    // Drink_1 Actual Water Temp, Max. Water Temp, Min. Water Temp, Increment, Drink_1_Water_Temp_ee_address, Current Water Temp.
  /*2*/ { 0, 100, 0, 1, 4, 0 },    // Drink_1 Actual Powder Amount, Max. Powder Amount, Min. Powder Amount, Increment, Drink_1_Powder_ee_address, Current Powder Amount
  /*3*/ { 0, 250, 25, 1, 6, 0 },   // Drink_2 Actual Water Amount, Max. Water, Min. Water, Increment, Drink_2_Water_ee_address, Current Water Amount
  /*4*/ { 0, 90, 25, 1, 8, 0 },    // Drink_2 Actual Water Temp, Max. Water Temp, Min. Water Temp, Increment, Drink_1_Water_Temp_ee_address, Current Water Temp.
  /*5*/ { 0, 100, 0, 1, 10, 0 },   // Drink_2 Actual Powder Amount, Max. Powder Amount, Min. Powder Amount, Increment, Drink_2_Powder_ee_address, Current Powder Amount
  /*6*/ { 0, 250, 10, 5, 12, 0 },  // Drink_3 Actual Water Amount, Max. Water, Min. Water, Increment, Drink_2_Water_ee_address, Current Water Amount
  /*7*/ { 0, 90, 25, 1, 14, 0 },   // Drink_3 Actual Water Temp, Max. Water Temp, Min. Water Temp, Increment, Drink_1_Water_Temp_ee_address, Current Water Temp.
  /*8*/ { 0, 100, 0, 1, 16, 0 },   // Drink_3 Actual Powder Amount, Max. Powder Amount, Min. Powder Amount, Increment, Drink_2_Powder_ee_address, Current Powder Amount
  /*9*/ { 0, 1, 0, 0, 18, 0 },     // Tone Settings
  /*10*/ { 0, 1, 0, 0, 20, 0 },    // LED Settings
  /*11*/ { 0, 10, 0, 1, 22, 0 },
  /*12*/ { 0, 10, 0, 1, 24, 0 },
  /*13*/ { 0, 10, 0, 1, 26, 0 },
};

float temp_variables_2_5;

//Variables Tables for quicker reference
//Drink 1
//Variable 0 : Drink 1 Water Amount ------- Array : [0][0]
//Variable 1 : Drink 1 Water Temperature -- Array : [1][0]
//Variable 2 : Drink 1 Powder Amount ----- Array : [2][0]
//Drink 2
//Variable 3 : Drink 2 Water Amount ------- Array : [3][0]
//Variable 4 : Drink 2 Water Temperature -- Array : [4][0]
//Variable 5 : Drink 2 Powder Amount ----- Array : [5][0]
//Drink 3
//Variable 6 : Drink 3 Water Amount ------- Array : [6][0]
//Variable 7 : Drink 3 Water Temperature -- Array : [7][0]
//Variable 8 : Drink 3 Powder Amount ----- Array : [8][0]

//Time Related Variables
unsigned long currentMillis = 0;

//Menu Setup
//Main Menu
LiquidLine start_line(0, 0, "Preparation");
LiquidLine main_line_1(0, 1, "Drink Settings");
LiquidLine main_line_2(0, 2, "Machine Settings");
LiquidScreen smain_1;
LiquidMenu mmain(lcd, smain_1);
//Preparation Menu
LiquidLine drink_1_start_line(0, 0, "Drink 1 Prep.");
LiquidLine drink_2_start_line(0, 1, "Drink 2 Prep.");
LiquidLine drink_3_start_line(0, 2, "Drink 3 Prep.");
LiquidLine mmback_line(0, 3, "Back");
LiquidScreen sstart_1;
LiquidMenu mstart(lcd, sstart_1);
//Drink Menu
LiquidLine sm_drink_1_line_1(0, 0, "Drink 1 Settings");
LiquidLine sm_drink_2_line_2(0, 1, "Drink 2 Settings");
LiquidLine sm_drink_3_line_3(0, 2, "Drink 3 Settings");
LiquidScreen sdrinks;
LiquidMenu mdrink(lcd, sdrinks);
//Drink 1 Menu
//Variable 0 : Drink 1 Water Amount ------- Array : [0][0]
//Variable 1 : Drink 1 Water Temperature -- Array : [1][0]
//Variable 2 : Drink 1 Powder Amount ----- Array : [2][0]
LiquidLine drink_1_line_1(0, 0, "Water Amnt. [ml]", variables[0][0]);
LiquidLine drink_1_line_2(0, 1, "Water Temp.  [C]", variables[1][0]);
LiquidLine drink_1_line_3(0, 2, "Powder Amnt.[g]", variables[2][0]);
LiquidLine drink_1_line_4(0, 3, "Drink 1 Prep.");
LiquidLine drink_back_line(0, 3, "Back");
LiquidScreen sdrink_1;
LiquidMenu mdrink_1(lcd, sdrink_1);
//Drink 2 Menu
//Variable 3 : Drink 2 Water Amount ------- Array : [3][0]
//Variable 4 : Drink 2 Water Temperature -- Array : [4][0]
//Variable 5 : Drink 2 Powder Amount ----- Array : [5][0]
LiquidLine drink_2_line_1(0, 0, "Water Amnt. [ml]", variables[3][0]);
LiquidLine drink_2_line_2(0, 1, "Water Temp.  [C]", variables[4][0]);
LiquidLine drink_2_line_3(0, 2, "Powder Amnt.[g]", variables[5][0]);
LiquidLine drink_2_line_4(0, 3, "Drink 2 Prep.");
LiquidScreen sdrink_2;
LiquidMenu mdrink_2(lcd, sdrink_2);
//Drink 3 Menu
//Variable 6 : Drink 3 Water Amount ------- Array : [6][0]
//Variable 7 : Drink 3 Water Temperature -- Array : [7][0]
//Variable 8 : Drink 3 Powder Amount ----- Array : [8][0]
LiquidLine drink_3_line_1(0, 0, "Water Amnt.  [ml]", variables[6][0]);
LiquidLine drink_3_line_2(0, 1, "Water Temp.   [C]", variables[7][0]);
LiquidLine drink_3_line_3(0, 2, "Powder Amnt. [g]", variables[8][0]);
LiquidLine drink_3_line_4(0, 3, "Drink 3 Prep.");
LiquidScreen sdrink_3;
LiquidMenu mdrink_3(lcd, sdrink_3);
//Machine Menu
LiquidLine tones_line_1(0, 0, "Tone Status ", tones_status_text);
LiquidLine machine_line_2(0, 1, "LED Status  ", led_status_text);
LiquidLine machine_line_3(0, 2, "Test 3.3.  ", variables[11][0]);
LiquidLine machine_line_4(0, 3, "Test 3.4.  ", variables[12][0]);
LiquidLine machine_line_5(0, 4, "Test 3.5.  ", variables[13][0]);
LiquidScreen smachine_1;
LiquidMenu mmachine(lcd, smachine_1);
//Overall Menu
LiquidSystem overall_menu;



//Functions
void count() {
  Sensor.count();
}

void temperature_1() {
  Serial.print("Internal Temp = ");
  Serial.println(thermocouple_1.readInternal());

  double c = thermocouple_1.readCelsius();
  if (isnan(c)) {
    Serial.println("Thermocouple 1 fault(s) detected!");
    uint8_t e = thermocouple_1.readError();
    if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple 1 is open - no connections.");
    if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple 1 is short-circuited to GND.");
    if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple 1 is short-circuited to VCC.");
  } else {
    Serial.print("C = ");
    Serial.println(c);
  }
};

void temperature_2() {
  Serial.print("Internal Temp = ");
  Serial.println(thermocouple_2.readInternal());

  double c = thermocouple_2.readCelsius();
  if (isnan(c)) {
    Serial.println("Thermocouple 2 fault(s) detected!");
    uint8_t e = thermocouple_2.readError();
    if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple 2 is open - no connections.");
    if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple 2 is short-circuited to GND.");
    if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple 2 is short-circuited to VCC.");
  } else {
    Serial.print("C = ");
    Serial.println(c);
  }
};

void onEb1Clicked(EncoderButton& eb) {
  if (switching_focus == true) {
    switching_focus = false;
    Serial.println("switching_focus = false");
    overall_menu.call_function(3);
    overall_menu.update();
    makeSomeNoise();
  } else {
    switching_focus = true;
    Serial.println("switching_focus = true");
  }
}

void onEb1Encoder(EncoderButton& eb) {
  int encoderValue = eb.increment();
  if (encoderValue > 0 && switching_focus == true) {
    overall_menu.switch_focus(true);
    Serial.println("Focus down");
    eb1_update_executed = true;
    makeSomeNoise();
  } else if (encoderValue < 0 && switching_focus == true) {
    overall_menu.switch_focus(false);
    Serial.println("Focus up");
    eb1_update_executed = true;
    makeSomeNoise();
  } else if (encoderValue > 0 && switching_focus == false) {
    overall_menu.call_function(increase);
    Serial.println("Value Increased");
    makeSomeNoise();
  } else if (encoderValue < 0 && switching_focus == false) {
    overall_menu.call_function(decrease);
    Serial.println("Value decreased");
    makeSomeNoise();
  }
}

void makeSomeNoise() {
  if (variables[9][0] == 1) {
    tone(speakerPin, tones[3]);
    delay(tones_duration);
    noTone(speakerPin);
  } else {
  }
}

// Increase a variable's value
void increase_variable(int variable_index) {
  if (variables[variable_index][0] < variables[variable_index][1]) {
    variables[variable_index][0] += variables[variable_index][3];
    Serial.print("Increase ");
    Serial.print(variable_index);
    Serial.print(" called");
    Serial.println();
    //Save Input
    EEPROM.put(variables[variable_index][4], variables[variable_index][0]);
    Serial.print(variable_index);
    Serial.print(" saved: ");
    Serial.print(variables[variable_index][0]);
    Serial.print(" in EEPROM postion ");
    Serial.println(variables[variable_index][4]);
  } else {
    variables[variable_index][0] = variables[variable_index][1];
    Serial.print("Max ");
    Serial.print(variable_index);
    Serial.print(" reached");
    Serial.println();
  }
}

// Decrease a variable's value
void decrease_variable(int variable_index) {
  if (variables[variable_index][0] > variables[variable_index][2]) {
    variables[variable_index][0] -= variables[variable_index][3];
    Serial.print("Decrease ");
    Serial.print(variable_index);
    Serial.print(" called");
    Serial.println();
    //Save Input
    EEPROM.put(variables[variable_index][4], variables[variable_index][0]);
    Serial.print(variable_index);
    Serial.print(" saved: ");
    Serial.print(variables[variable_index][0]);
    Serial.print(" in EEPROM postion ");
    Serial.println(variables[variable_index][4]);
  } else {
    variables[variable_index][0] = variables[variable_index][2];
    Serial.print("Min ");
    Serial.print(variable_index);
    Serial.print(" reached");
    Serial.println();
  }
}

//Increase / Decrease Functions
void increase_variable_0() {
  increase_variable(0);
}

void decrease_variable_0() {
  decrease_variable(0);
}

void increase_variable_1() {
  increase_variable(1);
}

void decrease_variable_1() {
  decrease_variable(1);
}

void increase_variable_2() {
  increase_variable(2);
}

void decrease_variable_2() {
  decrease_variable(2);
}

void increase_variable_3() {
  increase_variable(3);
}

void decrease_variable_3() {
  decrease_variable(3);
}

void increase_variable_4() {
  increase_variable(4);
}

void decrease_variable_4() {
  decrease_variable(4);
}

void increase_variable_5() {
  increase_variable(5);
}

void decrease_variable_5() {
  decrease_variable(5);
}

void increase_variable_6() {
  increase_variable(6);
}

void decrease_variable_6() {
  decrease_variable(6);
}

void increase_variable_7() {
  increase_variable(7);
}

void decrease_variable_7() {
  decrease_variable(7);
}

void increase_variable_8() {
  increase_variable(8);
}

void decrease_variable_8() {
  decrease_variable(8);
}

void increase_variable_11() {
  increase_variable(11);
}

void decrease_variable_11() {
  decrease_variable(11);
}

void increase_variable_12() {
  increase_variable(12);
}

void decrease_variable_12() {
  decrease_variable(12);
}

void increase_variable_13() {
  increase_variable(13);
}

void decrease_variable_13() {
  decrease_variable(13);
}

//Change tones_status
void increase_tones_status() {
  if (variables[9][0] == 0) {
    variables[9][0] = 1;
    Serial.print("Tone switched ON");
    Serial.println();
    strncpy(tones_status_text, tones_on, sizeof(tones_on));
    //Save Input
    EEPROM.put(variables[9][4], variables[9][0]);
    Serial.print("tones_status saved ");
    Serial.print(variables[9][0]);
    Serial.println();
  } else {
    variables[9][0] = variables[9][1];
    Serial.println("Max Tone Value reached");
  }
}

void decrease_tones_status() {
  if (variables[9][0] == 1) {
    variables[9][0] = 0;
    Serial.print("Tone switched OFF");
    Serial.println();
    strncpy(tones_status_text, tones_off, sizeof(tones_off));
    EEPROM.put(variables[9][4], variables[9][0]);
    Serial.print("tones_status saved ");
    Serial.print(variables[9][0]);
    Serial.println();
  } else {
    variables[9][0] = variables[9][2];
    Serial.println("Min Tone Value reached");
  }
}

//Change Led_status
void increase_led_status() {
  if (variables[10][0] == 0) {
    variables[10][0] = 1;
    Serial.print("LED switched ON");
    Serial.println();
    strncpy(led_status_text, led_on, sizeof(led_on));
    //Save Input
    EEPROM.put(variables[10][4], variables[10][0]);
    Serial.print("LED_status saved ");
    Serial.print(variables[10][0]);
    Serial.println();
  } else {
    variables[10][0] = variables[10][1];
    Serial.println("Max LED Status reached");
  }
}

void decrease_led_status() {
  if (variables[10][0] == 1) {
    variables[10][0] = 0;
    Serial.print("LED switched OFF");
    Serial.println();
    strncpy(led_status_text, led_off, sizeof(led_off));
    EEPROM.put(variables[10][4], variables[10][0]);
    Serial.print("LED_status saved ");
    Serial.print(variables[10][0]);
    Serial.println();
  } else {
    variables[10][0] = variables[10][2];
    Serial.println("Min LED Status reached");
  }
}

//Change LED Power
void LEDPower() {
  if (variables[10][0] == 1) {
    analogWrite(ledPin, 0);  //This Values have been changed to make the LED work according to the value in the Menu
  } else {
    analogWrite(ledPin, 255);  //This Values have been changed to make the LED work according to the value in the Menu
  }
}

//Fade LED
void fadeLED() {
  if (variables[10][0] == 1) {
    if (switching_focus == false) {
      ledtime = ledtime + 50;
      ledvalue = 128 + 127.5 * sin(2 * PI / ledperiode * ledtime);
      analogWrite(ledPin, ledvalue);
    } else {
      bool ledMessage = false;
      while (ledvalue != 255) {
        if (!ledMessage) {
          Serial.println("fadeLED closed");
          ledMessage = true;
        }
        ledtime = ledtime + 50;
        ledvalue = 128 + 127.5 * sin(2 * PI / ledperiode * ledtime);
        analogWrite(ledPin, ledvalue);
      }
    }
  } else {
  }
}

//Drink 1 Starting Function
void drink_1_start_function() {
  //Serial.println("Drink_1_start_function called");
  while (variables[0][5] < variables[0][0] || variables[2][5] > (-1) * variables[2][0]) {
    //Serial.println("Drink_1_start_function WHILE function called");
    currentMillis = millis();
    Drink_1_Water_Dispensing();
    Drink_1_Powder_Dispensing();
    Drink_1_Update_LCD();  //Updated according to the variables in this sketch
  }
}

void Drink_1_Update_LCD() {
  //Serial.println("Drink_1_Update_LCD called");
  if (millis() - windowStartTime_LCD > WindowSize_LCD) {                                  //time to update the LCD Screen
    if (variables[0][5] < variables[0][0] || variables[2][5] > (-1) * variables[2][0]) {  //if the current water amount is smaller than the actual or the current weight is smaller than the desired weight
      windowStartTime_LCD += WindowSize_LCD;
      LcdBarGraph lbg0(&lcd, 19, 1, 1);
      lcd.setCursor(6, 0);
      lcd.print("PROGRESS");
      lbg0.drawValue(variables[0][5], variables[0][0]);

      // Volume Display
      // Printing the Vol.: will always be called
      lcd.setCursor(0, 2);
      lcd.print("Vol.: ");
      // Single digit volumes
      if (variables[0][5] < 10) {
        lcd.setCursor(8, 2);
        lcd.print(variables[0][5]);
      }
      // Double digit volumes
      if (variables[0][5] >= 10 && variables[0][5] < 100) {
        lcd.setCursor(7, 2);
        lcd.print(variables[0][5]);
      }
      // Triple digit volumes
      if (variables[0][5] >= 100) {
        lcd.setCursor(6, 2);
        lcd.print(variables[0][5]);
      }

      // Powder Display
      // Printing the Form.: will always be called
      lcd.setCursor(0, 3);
      lcd.print("Form.: ");
      // Single digit Weight
      if (variables[2][5] * (-1) < 10) {
        lcd.setCursor(8, 3);
        lcd.print(variables[2][5] * (-1));
      }
      // Double digit Weight
      if (variables[2][5] * (-1) >= 10 && variables[2][5] * (-1) < 100) {
        lcd.setCursor(7, 3);
        lcd.print(variables[2][5] * (-1));
      }
      // Triple digit Weight
      if (variables[2][5] * (-1) >= 100) {
        lcd.setCursor(6, 3);
        lcd.print(variables[2][5] * (-1));
      }

      // Water Temperature
      // Printing the Temp.: will always be called
      lcd.setCursor(11, 2);
      lcd.print("Temp.: ");
      // Single digit Temp
      if (TX < 10) {
        lcd.setCursor(19, 2);
        lcd.print(TX, 0);
      }
      // Double digit Temp
      if (TX >= 10 && TX < 100) {
        lcd.setCursor(18, 2);
        lcd.print(TX, 0);
      }
      // Triple digit Temp
      if (TX >= 100) {
        lcd.setCursor(17, 2);
        lcd.print(TX, 0);
      }
    } else {
      lcd.clear();
      lcd.setCursor(5, 1);
      lcd.print("PREPERATION");
      lcd.setCursor(8, 2);
      lcd.print("DONE");
    }
  }
}

void Drink_1_Water_Dispensing() {
  //Serial.println("Drink_1_Water_Dispensing called");
  Setpoint_1 = variables[1][0];
  if (variables[0][5] < variables[0][0]) {
    digitalWrite(Pump, HIGH);
    Sensor.read();
    variables[0][5] = Sensor.getVolume() * 1000;
    Serial.print("Volume: ");
    Serial.print(variables[0][5]);
    Serial.print("ml --- ");
    //Temperature NTC
    VRT = analogRead(A15);         //Acquisition analog value of VRT
    VRT = (5.00 / 1023.00) * VRT;  //Conversion to voltage
    VR = VCC - VRT;
    RT = VRT / (VR / R);  //Resistance of RT
    ln = log(RT / RT0);
    TX = (1 / ((ln / B) + (1 / T0)));  //Temperature from thermistor
    TX = TX - 273.15;                  //Conversion to Celsius
    Serial.print("NTC Temperature: ");
    Serial.print(TX);
    Serial.print(" [C] --- ");
    //Thermocouple
    double d = thermocouple_2.readCelsius();
    Serial.print("FTH Control Temp. = ");
    Serial.print(d);
    Serial.print("[C]");

    Input_1 = TX;
    myPID_1.Compute();

    if (millis() - windowStartTime > WindowSize) {  //time to shift the Relay Window
      windowStartTime += WindowSize;
      Serial.println("");
      Serial.print("Relay Window shifted");
      Serial.print(" --- Setpoint: ");
      Serial.print(Setpoint_1);
      Serial.print(" --- Output: ");
      Serial.print(Output_1);
    }

    if (Output_1 > millis() - windowStartTime) {
      digitalWrite(RELAY_PIN, RELAY_ON);
      Serial.print(" --- Relay ON ");
      Serial.print(" --- Time: ");
      Serial.print(millis());
      Serial.println(" ms");

    } else {
      digitalWrite(RELAY_PIN, RELAY_OFF);
      Serial.print(" --- Relay OFF");
      Serial.print(" --- Time: ");
      Serial.print(millis());
      Serial.println(" ms");
    }
  } else {
    digitalWrite(Pump, LOW);
    digitalWrite(RELAY_PIN, RELAY_OFF);
  }
}

void Drink_1_Powder_Dispensing() {
  //Serial.println("Drink_1_Powder_Dispensing called");
  if (millis() - windowStartTime_Scale > WindowSize_Scale) {  //time to read the scales and maybe change the motor status
    windowStartTime_Scale += WindowSize_Scale;
    Serial.println("");
    Serial.print("Current dispensed weight: ");
    temp_variables_2_5 = (scale_1.get_units(1) + scale_2.get_units(1) + scale_3.get_units(1) + scale_4.get_units(1));
    variables[2][5] = int(temp_variables_2_5);
    Serial.print(variables[2][5]);
    Serial.println(" [g]");
    if (variables[2][5] > (-1) * variables[2][0] && Powder_Status == LOW) {
      digitalWrite(DC_Motor, HIGH);
      Serial.println(" --- Motor ON");
    } else {
      digitalWrite(DC_Motor, LOW);
      Powder_Status = HIGH;
      Serial.print("Dispensing finished");  //for debugging
      Serial.println(" --- Motor OFF");
    }
  }
}

//Drink 2 Starting Function
void drink_2_start_function() {

  //replace with the Drink 1 Starting function and adjust once done
  Serial.print("Drink 2 Start selected");
  Serial.println();
  lcd.clear();
  LcdBarGraph lbg0(&lcd, 16, 0, 1);
  byte i0 = 0;
  for (byte i0 = 0; i0 < 250; i0++) {
    switching_focus = false;
    fadeLED();
    lcd.setCursor(4, 0);
    lcd.print("Drink 2 Prep.");
    lbg0.drawValue(i0, 250);
    delay(10);
  }
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Drink 3 Starting Function
void drink_3_start_function() {
  Serial.print("Drink 3 Start selected");
  Serial.println();
  lcd.clear();
  LcdBarGraph lbg0(&lcd, 16, 0, 1);
  byte i0 = 0;
  for (byte i0 = 0; i0 < 250; i0++) {
    switching_focus = false;
    fadeLED();
    lcd.setCursor(4, 0);
    lcd.print("Drink 3 Prep.");
    lbg0.drawValue(i0, 250);
    delay(10);
  }
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Function to switch to the Preparation Menu
void goto_mstart() {
  overall_menu.change_menu(mstart);
  Serial.println("Go To Preparation Menu");
  mstart.set_focusedLine(0);
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Function to switch to the Drink Menu
void goto_mdrink() {
  overall_menu.change_menu(mdrink);
  Serial.println("Go To Drinks Menu");
  mdrink.set_focusedLine(0);
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Function to switch to the Drink_1 Menu
void goto_sdrink_1() {
  overall_menu.change_menu(mdrink_1);
  Serial.println("Go To Drinks 1 Menu");
  mdrink_1.set_focusedLine(0);
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Function to switch to the Drink_2 Menu
void goto_sdrink_2() {
  overall_menu.change_menu(mdrink_2);
  Serial.println("Go To Drinks 2 Menu");
  mdrink_2.set_focusedLine(0);
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Function to switch to the Drink_3 Menu
void goto_sdrink_3() {
  overall_menu.change_menu(mdrink_3);
  Serial.println("Go To Drinks 3 Menu");
  mdrink_3.set_focusedLine(0);
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Function to switch to the Machine Menu
void goto_mmachine() {
  overall_menu.change_menu(mmachine);
  Serial.println("Go To Machine Menu");
  mmachine.set_focusedLine(0);
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Callback function that will be attached to back_line to Main Menu
void mm_go_back() {
  overall_menu.change_menu(mmain);
  Serial.println("Go To Main Menu");
  switching_focus = true;
  Serial.println("switching_focus = true");
}

//Callback functiont that brings you back to the Drings Settings Menu
void drink_go_back() {
  overall_menu.change_menu(mdrink);
  Serial.println("Go To Drinks Menu");
  switching_focus = true;
  Serial.println("switching_focus = true");
}


void setup() {

  Serial.begin(115200);

  windowStartTime = millis();
  //initialize the variables we're linked to
  //Setpoint = 70;  //Temperature is the Setpoint
  //tell the PID to range between 0 and the full window size
  myPID_1.SetOutputLimits(0, WindowSize);
  myPID_1.SetSampleTime(WindowSize / 10);  // make sure we measure and sample at least 10 times per WindowSize.
  myPID_1.SetMode(AUTOMATIC);


  //Thermocouple
  thermocouple_1.begin();
  thermocouple_2.begin();


  //Scales
  windowStartTime_Scale = millis();

  scale_1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);
  scale_2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);
  scale_3.begin(LOADCELL_DOUT_PIN_3, LOADCELL_SCK_PIN_3);
  scale_4.begin(LOADCELL_DOUT_PIN_4, LOADCELL_SCK_PIN_4);

  scale_1.set_scale(calibration_factor_1);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale_2.set_scale(calibration_factor_2);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale_3.set_scale(calibration_factor_3);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale_4.set_scale(calibration_factor_4);  // this value is obtained by calibrating the scale with known weights; see the README for details

  scale_1.tare();  // reset the scale to 0
  scale_2.tare();  // reset the scale to 0
  scale_3.tare();  // reset the scale to 0
  scale_4.tare();  // reset the scale to 0


  //Flowmeter
  Sensor.begin(count);


  //Flow Through Heater
  pinMode(RELAY_PIN, OUTPUT);


  //Pump
  pinMode(Pump, OUTPUT);


  //Print all Sensor Values and test all actuators in the Serial Monitor
  Serial.println("Sensor Values on Start up:");

  //Print Temperatures
  Serial.print("Temperatures: ");

  //Thermocouple 1
  Serial.print("TC Internal Temp. 1 = ");
  Serial.print(thermocouple_1.readInternal());
  Serial.print("[C] --- ");
  double c = thermocouple_1.readCelsius();
  Serial.print("FTH Control Temp. = ");
  Serial.print(c);
  Serial.print("[C] --- ");

  //Thermocouple 2
  Serial.print("TC Internal Temp. 2 = ");
  Serial.print(thermocouple_2.readInternal());
  Serial.print("[C] --- ");
  double d = thermocouple_2.readCelsius();
  Serial.print("Water Outlet Control Temp. = ");
  Serial.print(d);
  Serial.print("[C] --- ");

  //NTC in FTH
  T0 = 25 + 273.15;
  VRT = analogRead(A15);         //Acquisition analog value of VRT
  VRT = (5.00 / 1023.00) * VRT;  //Conversion to voltage
  VR = VCC - VRT;
  RT = VRT / (VR / R);  //Resistance of RT
  ln = log(RT / RT0);
  TX = (1 / ((ln / B) + (1 / T0)));  //Temperature from thermistor
  TX = TX - 273.15;                  //Conversion to Celsius
  Serial.print("NTC : ");
  Serial.print(TX);
  Serial.println(" [C]");

  //Scales
  Serial.print("Scales : ");
  Serial.print("Scale 1: ");
  Serial.print(scale_1.get_units(), 1);
  Serial.print("[g]");
  Serial.print(" --- Scale 2: ");
  Serial.print(scale_2.get_units(), 1);
  Serial.print("[g]");
  Serial.print(" --- Scale 3: ");
  Serial.print(scale_3.get_units(), 1);
  Serial.print("[g]");
  Serial.print(" --- Scale 4: ");
  Serial.print(scale_4.get_units(), 1);
  Serial.println("[g]");

  //Relays
  Serial.print("Pump, Motor, FTH Test:");
  digitalWrite(Pump, HIGH);
  Serial.print(" Pump ON --- ");
  digitalWrite(DC_Motor, HIGH);
  Serial.print("Motor ON --- ");
  digitalWrite(RELAY_PIN, HIGH);
  Serial.print("FTH ON --- ");
  delay(100);
  digitalWrite(Pump, LOW);
  Serial.print("Pump OFF --- ");
  digitalWrite(DC_Motor, LOW);
  Serial.print("Motor OFF ---");
  digitalWrite(RELAY_PIN, LOW);
  Serial.println("FTH OFF");

  //Flow Sensor
  Serial.print("Flow Sensor Test: ");
  Sensor.read();
  int actual_volume = Sensor.getVolume() * 1000;
  Serial.print(actual_volume);
  Serial.println(" [ml]");
  //Start Power LED
  //LEDPower();


  //Link the encoder event(s) to your function
  eb1.setClickHandler(onEb1Clicked);
  eb1.setEncoderHandler(onEb1Encoder);

  //Set Focus Symbol
  mmain.set_focusSymbol(Position::CUSTOM, 0);

  //Set Focus for each line
  start_line.set_focusPosition(Position::CUSTOM, 19, 0);
  drink_1_start_line.set_focusPosition(Position::CUSTOM, 19, 0);
  drink_2_start_line.set_focusPosition(Position::CUSTOM, 19, 1);
  drink_3_start_line.set_focusPosition(Position::CUSTOM, 19, 2);
  main_line_1.set_focusPosition(Position::CUSTOM, 19, 1);
  main_line_2.set_focusPosition(Position::CUSTOM, 19, 2);
  mmback_line.set_focusPosition(Position::CUSTOM, 19, 3);
  drink_back_line.set_focusPosition(Position::CUSTOM, 19, 3);
  tones_line_1.set_focusPosition(Position::CUSTOM, 19, 0);
  machine_line_2.set_focusPosition(Position::CUSTOM, 19, 1);
  machine_line_3.set_focusPosition(Position::CUSTOM, 19, 2);
  machine_line_4.set_focusPosition(Position::CUSTOM, 19, 3);
  machine_line_5.set_focusPosition(Position::CUSTOM, 19, 3);
  sm_drink_1_line_1.set_focusPosition(Position::CUSTOM, 19, 0);
  sm_drink_2_line_2.set_focusPosition(Position::CUSTOM, 19, 1);
  sm_drink_3_line_3.set_focusPosition(Position::CUSTOM, 19, 2);
  drink_1_line_1.set_focusPosition(Position::CUSTOM, 19, 0);
  drink_1_line_2.set_focusPosition(Position::CUSTOM, 19, 1);
  drink_1_line_3.set_focusPosition(Position::CUSTOM, 19, 2);
  drink_1_line_4.set_focusPosition(Position::CUSTOM, 19, 3);
  drink_2_line_1.set_focusPosition(Position::CUSTOM, 19, 0);
  drink_2_line_2.set_focusPosition(Position::CUSTOM, 19, 1);
  drink_2_line_3.set_focusPosition(Position::CUSTOM, 19, 2);
  drink_2_line_4.set_focusPosition(Position::CUSTOM, 19, 3);
  drink_3_line_1.set_focusPosition(Position::CUSTOM, 19, 0);
  drink_3_line_2.set_focusPosition(Position::CUSTOM, 19, 1);
  drink_3_line_3.set_focusPosition(Position::CUSTOM, 19, 2);
  drink_3_line_4.set_focusPosition(Position::CUSTOM, 19, 3);

  //Read the values recorded in the EEPROM
  for (int j = 0; j < 15; j++) {
    EEPROM.get(variables[j][4], variables[j][0]);
    Serial.print("The EEPDROM value for ");
    Serial.print(j);
    Serial.print(" is ");
    Serial.println(variables[j][0]);
  }

  if (variables[9][0] == true) {
    strncpy(tones_status_text, tones_on, sizeof(tones_on));
  } else {
    strncpy(tones_status_text, tones_off, sizeof(tones_off));
  }

  if (variables[10][0] == true) {
    strncpy(led_status_text, led_on, sizeof(led_on));
  } else {
    strncpy(led_status_text, led_off, sizeof(led_off));
  }

  //Start LCD Screen
  windowStartTime_LCD = millis();

  lcd.begin(LCD_Columns, LCD_Rows);
  lcd.setCursor(12, 3);
  lcd.print("V.T.1.4.");
  delay(2000);
  lcd.clear();

  //Attach lines to the main menu
  smain_1.add_line(start_line);
  smain_1.add_line(main_line_1);
  smain_1.add_line(main_line_2);

  //Attach lines to the preparation menu
  sstart_1.add_line(drink_1_start_line);
  sstart_1.add_line(drink_2_start_line);
  sstart_1.add_line(drink_3_start_line);
  sstart_1.add_line(mmback_line);

  //Attach lines to the drinks menu
  sdrinks.add_line(sm_drink_1_line_1);
  sdrinks.add_line(sm_drink_2_line_2);
  sdrinks.add_line(sm_drink_3_line_3);
  sdrinks.add_line(mmback_line);

  //Attach lines to the drink 1 menu
  sdrink_1.add_line(drink_1_line_1);
  sdrink_1.add_line(drink_1_line_2);
  sdrink_1.add_line(drink_1_line_3);
  sdrink_1.add_line(drink_1_line_4);  //might need to be a different drink_1 start line
  sdrink_1.add_line(drink_back_line);

  //Attach lines to the drink 2 menu
  sdrink_2.add_line(drink_2_line_1);
  sdrink_2.add_line(drink_2_line_2);
  sdrink_2.add_line(drink_2_line_3);
  sdrink_2.add_line(drink_2_line_4);  //might need to be a different drink_2 start line
  sdrink_2.add_line(drink_back_line);

  //Attach lines to the drink 3 menu
  sdrink_3.add_line(drink_3_line_1);
  sdrink_3.add_line(drink_3_line_2);
  sdrink_3.add_line(drink_3_line_3);
  sdrink_3.add_line(drink_3_line_4);  //might need to be a different drink_3 start line
  sdrink_3.add_line(drink_back_line);

  //Attach lines to the machine meu
  smachine_1.add_line(tones_line_1);
  smachine_1.add_line(machine_line_2);
  smachine_1.add_line(machine_line_3);
  smachine_1.add_line(machine_line_4);
  smachine_1.add_line(machine_line_5);
  smachine_1.add_line(mmback_line);

  //Attaching a function to the lines is required for scrolling to work
  //Main
  start_line.attach_function(3, goto_mstart);
  main_line_1.attach_function(3, goto_mdrink);
  main_line_2.attach_function(3, goto_mmachine);
  mmback_line.attach_function(3, mm_go_back);

  //Preparation
  drink_1_start_line.attach_function(3, drink_1_start_function);
  drink_2_start_line.attach_function(3, drink_2_start_function);
  drink_3_start_line.attach_function(3, drink_3_start_function);

  //Drink
  sm_drink_1_line_1.attach_function(3, goto_sdrink_1);
  sm_drink_2_line_2.attach_function(3, goto_sdrink_2);
  sm_drink_3_line_3.attach_function(3, goto_sdrink_3);

  //Drink 1
  drink_1_line_1.attach_function(increase, increase_variable_0);
  drink_1_line_1.attach_function(decrease, decrease_variable_0);
  drink_1_line_2.attach_function(increase, increase_variable_1);
  drink_1_line_2.attach_function(decrease, decrease_variable_1);
  drink_1_line_3.attach_function(increase, increase_variable_2);
  drink_1_line_3.attach_function(decrease, decrease_variable_2);
  drink_1_line_4.attach_function(3, drink_1_start_function);
  drink_back_line.attach_function(3, drink_go_back);

  //Drink 2
  drink_2_line_1.attach_function(increase, increase_variable_3);
  drink_2_line_1.attach_function(decrease, decrease_variable_3);
  drink_2_line_2.attach_function(increase, increase_variable_4);
  drink_2_line_2.attach_function(decrease, decrease_variable_4);
  drink_2_line_3.attach_function(increase, increase_variable_5);
  drink_2_line_3.attach_function(decrease, decrease_variable_5);
  drink_2_line_4.attach_function(3, drink_2_start_function);

  //Drink 3
  drink_3_line_1.attach_function(increase, increase_variable_6);
  drink_3_line_1.attach_function(decrease, decrease_variable_6);
  drink_3_line_2.attach_function(increase, increase_variable_7);
  drink_3_line_2.attach_function(decrease, decrease_variable_7);
  drink_3_line_3.attach_function(increase, increase_variable_8);
  drink_3_line_3.attach_function(decrease, decrease_variable_8);
  drink_3_line_4.attach_function(3, drink_3_start_function);

  //Machine
  tones_line_1.attach_function(increase, increase_tones_status);
  tones_line_1.attach_function(decrease, decrease_tones_status);
  machine_line_2.attach_function(increase, increase_led_status);
  machine_line_2.attach_function(decrease, decrease_led_status);
  machine_line_3.attach_function(increase, increase_variable_11);
  machine_line_3.attach_function(decrease, decrease_variable_11);
  machine_line_4.attach_function(increase, increase_variable_12);
  machine_line_4.attach_function(decrease, decrease_variable_12);
  machine_line_5.attach_function(increase, increase_variable_13);
  machine_line_5.attach_function(decrease, decrease_variable_13);

  // Set the number of lines the display has in the certain Menu
  smain_1.set_displayLineCount(LCD_Rows);
  sstart_1.set_displayLineCount(LCD_Rows);
  sdrinks.set_displayLineCount(LCD_Rows);
  sdrink_1.set_displayLineCount(LCD_Rows);
  sdrink_2.set_displayLineCount(LCD_Rows);
  sdrink_3.set_displayLineCount(LCD_Rows);
  smachine_1.set_displayLineCount(LCD_Rows);

  mmain.add_screen(smain_1);
  mstart.add_screen(sstart_1);
  mdrink.add_screen(sdrinks);
  mdrink_1.add_screen(sdrink_1);
  mdrink_2.add_screen(sdrink_2);
  mdrink_3.add_screen(sdrink_3);
  mmachine.add_screen(smachine_1);

  overall_menu.add_menu(mmain);
  overall_menu.add_menu(mstart);
  overall_menu.add_menu(mdrink);
  overall_menu.add_menu(mdrink_1);
  overall_menu.add_menu(mdrink_2);
  overall_menu.add_menu(mdrink_3);
  overall_menu.add_menu(mmachine);

  overall_menu.set_focusedLine(0);

  overall_menu.update();
}

void loop() {
  //Power Indicator LED
  LEDPower();
  fadeLED();
  eb1.update();
  //digitalWrite(DC_Motor, HIGH);
  if (eb1_update_executed) {
    overall_menu.update();
    eb1_update_executed = false;
  }
  delay(50);
}

this sounds like a commercial systems, one that probably doesn't use relays that can be driven by an Arduino (or maybe it does)

it would be better, safer to drive the relays indirectly.

a common approach to driving devices that operate at different voltages is to use an open collector which simply connects the device to ground where the other terminal can be a much higher (or lower) voltage. a uln2803c can be driven by an arduino output.

when the arduino output goes HIGH it enabled the transistor in the uln2803 which grounds the relay turning it on. the uln2803c also has flyback resistors for inductive loads

1 Like

This line: "pinMode(DC_Motor, OUTPUT);" does not appear in the second listing, why?

1 Like

Oh wow.........

I have looked everywhere and into everything but missed the basics...

Thank you very much, that was the missing line!

That's a forehead slapper for sure.

Just for fun put the error back in , well I mean remove the fix, and measure the voltage on the "output" pin to see what the weak pull-up was (not) doing for you drive-wise.

My A, B, C… list would have made that test sooner, I think.

There are relay drivers that might have "worked" with such a weak signal, how that woukd be most unfortunate if the case, as it might have tested out fine but been marginal and fail half way to Julpiter.

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.