Hi everyone, I have a project to do for my teacher. is to make clock from ESP32-S3 board and now i can make clock online by wifi connection. But my problem is that I don't know how to write a program to be able to set the alarm using 3-4 switches to press the set button. Please help me.
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(115200);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Serial.println("PRESS");
delay(200);
}
}
so now take a piece of paper and draw the program flow of the necessary steps how to enter an alarm time.
The diagram should show
how to enter the mode SETTING
how to enter the HOUR (how to increase / decrease the number, what should happen if the current Number is 0 and you press DOWN, what should happen if the current Number is 23 and you press UP,...)
how to jump to enter the MINUTE
how to enter the MINUTE
how to confirm the settings
please make a clear picture and upload it to the forum.
Every good program starts with a drawing.
Paper is just a fast way to do such kind of drawing.
This will help you to focus your ideas before you start writing code.
If I were you, I would make my life easier and omnit that "press for 3 seconds" actions currently.
Just use single button presses.
Furthermore I think your program has no "end" ... you just have to leave the alarm setting. and return to display the current time.
During the display of the current time you have to check if the Alarm time is met.
I don't know if this state diagram makes sense for you, but imho this represents better what your program should do:
so I would write the program as "finite state machine" (<-- google for it!), using 4 states IDLE, HOUR, MINUTE and ALARM.
The button presses either jump through the states or increase/decrease variables.
Think about it.
Google for finite state machine, even Wikipedia has a nice summary on that topic.
Read this post again.
Share your mind or ask.