#include <LiquidCrystal_I2C.h>
#define RELAY_PIN 8
#define LCD_ADDRESS 0x27
const long phaseDuration1 = 1 * 60 * 1000; // 1 minute for Phase 1
const long phaseDuration2 = 1 * 60 * 1000; // 1 minute for Phase 2
const long phaseDuration3 = 1 * 60 * 1000; // 1 minute for Phase 3
int currentPhase = 1; // Tracks the current phase (1, 2, or 3)
unsigned long phaseStartTime = 0; // Tracks the start time of the current phase
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
Serial.begin(9600); // Set baud rate for serial monitor
lcd.init();
lcd.backlight();
lcd.clear(); // Clear the LCD screen
lcd.print("Phase: ");
phaseStartTime = millis(); // Initialize phase start time
}
void displayPhaseAndElapsedTime(int phase, unsigned long elapsedTime) {
// Display phase and elapsed time on the LCD
lcd.setCursor(0, 0); // First row
lcd.print("Phase: ");
lcd.print(phase);
lcd.print(" "); // Clear leftover characters
lcd.setCursor(0, 1); // Second row
lcd.print("Time: ");
lcd.print(elapsedTime / 1000); // Convert milliseconds to seconds
lcd.print("s "); // Clear leftover characters
// Display phase and elapsed time in the Serial Monitor
Serial.print("Current Phase: ");
Serial.print(phase);
Serial.print(" | Elapsed Time: ");
Serial.print(elapsedTime / 1000); // Convert milliseconds to seconds
Serial.println("s");
}
void loop() {
unsigned long currentMillis = millis();
unsigned long elapsedTime = currentMillis - phaseStartTime;
// Display current phase and elapsed time
displayPhaseAndElapsedTime(currentPhase, elapsedTime);
// Check for phase transitions based on elapsed time
if (currentPhase == 1 && elapsedTime >= phaseDuration1) {
currentPhase = 2; // Transition to Phase 2
phaseStartTime = currentMillis; // Reset phase start time
Serial.println("Transitioning to Phase 2");
} else if (currentPhase == 2 && elapsedTime >= phaseDuration2) {
currentPhase = 3; // Transition to Phase 3
phaseStartTime = currentMillis; // Reset phase start time
Serial.println("Transitioning to Phase 3");
} else if (currentPhase == 3 && elapsedTime >= phaseDuration3) {
Serial.println("Phase control stopped after Phase 3");
lcd.setCursor(0, 1);
lcd.print("Phase End "); // Display Phase End on LCD
while (true) {
// Stop further execution
delay(100); // Add a small delay to avoid freezing
}
}
delay(100); // Add a small delay to avoid flooding the Serial Monitor
}
Welcome to the forum
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
Please post your sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Where in your code is phaseStartTime updated ?
it started in the loop
Hello newbiecoder24
The compiler builds the programme, but generates many warnings that need to be processed.
thank you for the respond what do you think is the solution for it I even thought of using a.i but still not working
its getting compiled in my arduino ide and i get the results in both the serial monitor and the lcd the problem is when the phaseDuration is up it does not change into the next phase
unsigned long elapsedTime = currentMillis - phaseStartTime;
This line of code calculates how long the current phase has been running by subtracting the phase start time from the current time. The problem is that the phase start time never changes in the sketch
There may be other problems with the sketch but at least you need to update phaseStartTime when a new phase starts
5 hours and no results? Perhaps a different approach is called for. Following instructions would be a good start, don't you think?
thanks for the respond again i asked the a.i and it thought of using this phaseStartTime = currentMillis; supposedly it should act as the restart for the next phase but does not work since I already tested it
Post the full sketch where you tested it, using code tags when you do
thanks for the response I'm following the a.i and other instructions that i have search but none seems to be working may i get about your opinion in resolving this thank you in advance
If you don't want to read
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
Then perhaps you could ask you AI friend how to post code on the Arduino Forum using code tags.
Start by doing this
I will not reply until you add code tags
thanks for the response and im sorry im new to the community and just starting out using it would be right?
My opinion is that you are unlikely to get help until you begin following instructions. As pointed out way back in post #2.
And avoiding any semblance of proper sentence structure makes things harder for the people that you are asking for help. That's not a wise thing to do, as it tends to turn people off.
yes im sorry i edited it out im very sorry
im very sorry please forgive me and i edited it out in the main post im really sorry
It looks like each phase does, this is in #1 abive
else if (currentPhase == 2 && elapsedTime > phaseDuration2) {
currentPhase = 3; // Transition to Phase 3
phaseStartTime = currentMillis;
Serial.println("Transitioning to Phase 3");
} else
a7