I'm trying to make a jack and coke maker. I am very new to coding and struggling to learn, especially the millis function. I had ChatGPT write most of this code. When an option is selected pump1 turns on and stays on. I need the pumps to run for the specified time and then shut off. The option "Add Jack" works fine. The other 3 options are the problem. What is wrong here?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the I2C address to match your LCD
int upButtonPin = 2; // Up button
int downButtonPin = 3; // Down button
int selectButtonPin = 10; // Select button
int cancelButtonPin = 9; // Cancel button
int selectedOption = 0; // Track the selected option
// Define pump control pins
int pump1Pin = 4;
int pump2Pin = 5;
unsigned long startTime; // To store the start time for dispensing
enum DispensingState {
NOT_DISPENSING,
DISPENSING_PUMP1,
DISPENSING_PUMP2,
};
DispensingState dispensingState = NOT_DISPENSING;
// Variables to store pump active times
unsigned long pump1ActiveTime;
unsigned long pump2ActiveTime;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(">Jack and Coke 1");
lcd.setCursor(0, 1);
lcd.print(" Jack and Coke 2");
lcd.setCursor(0, 2);
lcd.print(" Shot of Jack");
lcd.setCursor(0, 3);
lcd.print(" Add Jack");
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(selectButtonPin, INPUT_PULLUP);
pinMode(cancelButtonPin, INPUT_PULLUP);
pinMode(pump1Pin, OUTPUT);
pinMode(pump2Pin, OUTPUT);
}
void loop() {
// Read button presses and update the selected option
if (digitalRead(upButtonPin) == HIGH) {
selectedOption--;
if (selectedOption < 0) selectedOption = 3;
updateLCD();
delay(200); // Debounce delay
}
if (digitalRead(downButtonPin) == HIGH) {
selectedOption++;
if (selectedOption > 3) selectedOption = 0;
updateLCD();
delay(200); // Debounce delay
}
// Implement the logic for dispensing the selected option here
if (digitalRead(selectButtonPin) == HIGH) {
dispense(selectedOption);
}
// Implement the logic for canceling dispensing here
if (digitalRead(cancelButtonPin) == HIGH) {
cancelDispensing();
}
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
switch (selectedOption) {
case 0:
lcd.setCursor(0, 0);
lcd.print(">Jack and Coke 1");
lcd.setCursor(0, 1);
lcd.print(" Jack and Coke 2");
lcd.setCursor(0, 2);
lcd.print(" Shot of Jack");
lcd.setCursor(0, 3);
lcd.print(" Add Jack");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print(" Jack and Coke 1");
lcd.setCursor(0, 1);
lcd.print(">Jack and Coke 2");
lcd.setCursor(0, 2);
lcd.print(" Shot of Jack");
lcd.setCursor(0, 3);
lcd.print(" Add Jack");
break;
case 2:
lcd.setCursor(0, 0);
lcd.print(" Jack and Coke 1");
lcd.setCursor(0, 1);
lcd.print(" Jack and Coke 2");
lcd.setCursor(0, 2);
lcd.print(">Shot of Jack");
lcd.setCursor(0, 3);
lcd.print(" Add Jack");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print(" Jack and Coke 1");
lcd.setCursor(0, 1);
lcd.print(" Jack and Coke 2");
lcd.setCursor(0, 2);
lcd.print(" Shot of Jack");
lcd.setCursor(0, 3);
lcd.print(">Add Jack");
break;
}
}
void dispense(int option) {
// Implement the logic to control the peristaltic pumps for dispensing here
// Activate the peristaltic pump(s) to dispense the appropriate amounts of liquids
// You might need to calculate the time or revolutions needed for the desired amount
// Implement safety measures to stop dispensing when the desired amount is reached
unsigned long currentTime = millis();
switch (option) {
case 0: // Jack and Coke 1
if (dispensingState == NOT_DISPENSING) {
lcd.clear();
lcd.print("Pouring");
startTime = currentTime;
dispensingState = DISPENSING_PUMP1;
pump1ActiveTime = 2000; // 2 seconds
}
if (dispensingState == DISPENSING_PUMP1 && (currentTime - startTime) < pump1ActiveTime) {
digitalWrite(pump1Pin, HIGH);
} else if (dispensingState == DISPENSING_PUMP1) {
digitalWrite(pump1Pin, LOW);
dispensingState = DISPENSING_PUMP2;
startTime = currentTime;
pump2ActiveTime = 4000; // 4 seconds
}
if (dispensingState == DISPENSING_PUMP2 && (currentTime - startTime) < pump2ActiveTime) {
digitalWrite(pump2Pin, HIGH);
} else if (dispensingState == DISPENSING_PUMP2) {
digitalWrite(pump2Pin, LOW);
dispensingState = NOT_DISPENSING;
lcd.clear();
lcd.print("ENJOY!!");
delay(2000);
updateLCD();
}
break;
case 1: // Jack and Coke 2
if (dispensingState == NOT_DISPENSING) {
lcd.clear();
lcd.print("Pouring");
startTime = currentTime;
dispensingState = DISPENSING_PUMP1;
pump1ActiveTime = 4000; // 4 seconds
}
if (dispensingState == DISPENSING_PUMP1 && (currentTime - startTime) < pump1ActiveTime) {
digitalWrite(pump1Pin, HIGH);
} else if (dispensingState == DISPENSING_PUMP1) {
digitalWrite(pump1Pin, LOW);
dispensingState = DISPENSING_PUMP2;
startTime = currentTime;
pump2ActiveTime = 8000; // 8 seconds
}
if (dispensingState == DISPENSING_PUMP2 && (currentTime - startTime) < pump2ActiveTime) {
digitalWrite(pump2Pin, HIGH);
} else if (dispensingState == DISPENSING_PUMP2) {
digitalWrite(pump2Pin, LOW);
dispensingState = NOT_DISPENSING;
lcd.clear();
lcd.print("ENJOY!!");
delay(2000);
updateLCD();
}
break;
case 2: // Shot of Jack
if (dispensingState == NOT_DISPENSING) {
lcd.clear();
lcd.print("Pouring");
startTime = currentTime;
dispensingState = DISPENSING_PUMP1;
pump1ActiveTime = 1000; // 1 second
}
if (dispensingState == DISPENSING_PUMP1 && (currentTime - startTime) < pump1ActiveTime) {
digitalWrite(pump1Pin, HIGH);
} else if (dispensingState == DISPENSING_PUMP1) {
digitalWrite(pump1Pin, LOW);
dispensingState = NOT_DISPENSING;
lcd.clear();
lcd.print("ENJOY!!");
delay(2000);
updateLCD();
}
break;
case 3: // Add Jack
if (dispensingState == NOT_DISPENSING) {
lcd.clear();
lcd.print("Pouring");
digitalWrite(pump1Pin, HIGH);
dispensingState = DISPENSING_PUMP1;
}
// Continuous dispensing while the button is held
if (digitalRead(selectButtonPin) == LOW) {
dispensingState = NOT_DISPENSING;
digitalWrite(pump1Pin, LOW);
lcd.clear();
lcd.print("ENJOY!!");
delay(2000);
updateLCD();
}
break;
}
}
void cancelDispensing() {
if (dispensingState != NOT_DISPENSING) {
// Stop both pumps and return to the main screen
digitalWrite(pump1Pin, LOW);
digitalWrite(pump2Pin, LOW);
dispensingState = NOT_DISPENSING;
lcd.clear();
updateLCD();
}
}