hi guys, need some help please, i have a relay controlling a steamer, i need it to shut down after the time is finished and i cant seem to achieve it, i am running 2 seperate programs and the programs are running the temp control is working but it is not shutting down after the time has elapsed please help
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0X27, 20, 4); // Adjust the LCD size to 20x4
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // Update interval in milliseconds
#define COOKER 6
#define PROGRAM_PIN 4 // Pin to select program (HIGH: Program 1, LOW: Program 2)
#define bt_set 9
#define bt_up 10
#define bt_down 11
#define bt_start 12
bool exitProgram = false;
unsigned int gourmetTime_h = 0; // Set the desired time for Gourmet program
unsigned int gourmetTime_m = 0;
unsigned int gourmetTime_s = 30;
unsigned int magicsTime_h = 0; // Set the desired time for Magics program
unsigned int magicsTime_m = 1;
unsigned int magicsTime_s = 0;
const float gourmetDesiredTemperature = 96.0; // Desired temperature for Gourmet program
const float magicsDesiredTemperature = 60.0; // Desired temperature for Magics program
bool isTimerRunning = false;
unsigned long timerStartTime = 0;
unsigned long currentMillis = 0;
int thermoDO = A0;
int thermoCS = A1;
int thermoCLK = A2;
#define DS18B20_PIN 2 // Change this to the actual pin you're using
OneWire oneWire(DS18B20_PIN);
DallasTemperature ds18b20(&oneWire);
enum ProgramState {
GOURMET,
MAGICS
};
ProgramState currentProgram = GOURMET;
void setup() {
// Initialize setup code here
lcd.init();
lcd.backlight();
ds18b20.begin();
pinMode(bt_set, INPUT_PULLUP); // Set button with internal pull-up resistor
pinMode(PROGRAM_PIN, INPUT_PULLUP); // Set program pin as input with pull-up
pinMode(DS18B20_PIN, INPUT_PULLUP); // Set DS18B20 pin as input with pull-up
pinMode(COOKER, OUTPUT);
lcd.setCursor(0, 1);
lcd.print("Welcome To Pasturizer");
lcd.clear(); // Clear the screen
}
void loop() {
updateProgramState();
ds18b20.requestTemperatures();
float currentTemperature = ds18b20.getTempCByIndex(0); // Assuming only one sensor is connected
lcd.setCursor(0, 0);
lcd.print(currentProgram == GOURMET ? "Gourmet" : "Magics");
lcd.setCursor(0, 3);
lcd.print("Temp: ");
lcd.print(currentTemperature);
lcd.print("C");
// Display the selected program time
lcd.setCursor(0, 1);
lcd.print("Time:");
printSelectedProgramTime();
currentMillis = millis();
if (digitalRead(COOKER) == HIGH ){
lcd.setCursor(9, 0);
lcd.print("Heat");
}
else{ lcd.setCursor(9,0);
lcd.print("Cool");
}
if (!isTimerRunning && currentTemperature >= getDesiredTemperature(currentProgram)) {
startTimer();
}
if (isTimerRunning) {
unsigned long elapsedMillis = currentMillis - timerStartTime;
unsigned long remainingMillis = 0;
if (currentProgram == GOURMET) {
remainingMillis = (gourmetTime_h * 3600000UL + gourmetTime_m * 60000UL + gourmetTime_s * 1000UL) - elapsedMillis;
} else if (currentProgram == MAGICS) {
remainingMillis = (magicsTime_h * 3600000UL + magicsTime_m * 60000UL + magicsTime_s * 1000UL) - elapsedMillis;
}
lcd.setCursor(0, 2);
lcd.print("Remaining:");
printTime(remainingMillis);
if (remainingMillis == 0) {
stopTimer();
digitalWrite(COOKER, LOW);
// Turn off the cooker when the timer is done
} else {
controlCooker(currentTemperature);
}
}
// Your code to control the timer and heating logic...
}
void updateProgramState() {
if (digitalRead(PROGRAM_PIN) == LOW) {
currentProgram = GOURMET;
} else {
currentProgram = MAGICS;
}
}
float getDesiredTemperature(ProgramState program) {
if (program == GOURMET) {
return gourmetDesiredTemperature;
} else if (program == MAGICS) {
return magicsDesiredTemperature;
}
return 0.0; // Default
}
void startTimer() {
timerStartTime = millis();
isTimerRunning = true;
digitalWrite(COOKER, HIGH); // Turn on the cooker when the timer starts
}
void stopTimer() {
isTimerRunning = false;
stopTimer();
digitalWrite(COOKER, LOW); // Turn off the cooker when the timer stops
exit(0);
}
void controlCooker(float currentTemperature) {
float desiredTemp = getDesiredTemperature(currentProgram);
if (!isTimerRunning) {
// If timer is not running, keep the cooker on for warming up
digitalWrite(COOKER, HIGH);
} else if (currentTemperature < desiredTemp) {
// Turn on the cooker if temperature is below the desired temperature
digitalWrite(COOKER, HIGH);
} else {
// Turn off the cooker if temperature is at or above the desired temperature
digitalWrite(COOKER, LOW);
}
}
void printTime(unsigned long timeInMillis) {
unsigned int hours = timeInMillis / 3600000UL;
unsigned int minutes = (timeInMillis % 3600000UL) / 60000UL;
unsigned int seconds = (timeInMillis % 60000UL) / 1000UL;
if (hours < 10) lcd.print("0");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
}
void printSelectedProgramTime() {
unsigned int programHours, programMinutes, programSeconds;
if (currentProgram == GOURMET) {
programHours = gourmetTime_h;
programMinutes = gourmetTime_m;
programSeconds = gourmetTime_s;
} else if (currentProgram == MAGICS) {
programHours = magicsTime_h;
programMinutes = magicsTime_m;
programSeconds = magicsTime_s;
}
if (programHours < 10) lcd.print("0");
lcd.print(programHours);
lcd.print(":");
if (programMinutes < 10) lcd.print("0");
lcd.print(programMinutes);
lcd.print(":");
if (programSeconds < 10) lcd.print("0");
lcd.print(programSeconds);
}