Hello,
I want to loop between a day of every month. I am doing something like this. The loop should run only once when the calendar hits a certain date
set_day = 4;
new_day = getadatefromNTPserver //the day will be receieved from the ntp server and for a whole24 hours the state will remain the same.
if ( set_day == new_day){
reset loop
}
else if (set_day =! new_day){
do stuff
}
Where if I choose set_day as "4", the loop should run till the 4th of next month. I am syncing my esp32 using the time library to sync NTP time and get the current day.
But the problem is using the above logic, my loop will keep on resetting the whole for the remainder of 4th date, It will only move forward when the NTP updates and gives me the next day.
I can remedy this by using time in hr:mm:ss as another argument in the if statement.
if (set_day == new_day && time == 00:00:00){
reset loop
}
else if (set_day =! new_day){
do stuff
}
where 00:00:00 is when the 4th day starts and as soon as the "second" iterates, the first if statement becomes false.
basically what I want is
what i want is
if ( daytoday == 4 && time = 00:00:00){
do stuff}
else {
do stuff}
void loop()
{
static int last_day = 255;
int new_day = get_new_day(); // however you get the new day...
// See if day has changed
if (new_day != last_day)
{
// We know day has changed. Now check to see if we need to reset.
if (new_day == set_day)
{
// reset loop
}
last_day = new_day;
}
if (set_day != new_day)
{
// do stuff
}
}
Thanks. I did check the example and found that the loop equates the current and former readings
"lastButtonState = buttonState;" but how can i use this info in my code? since for the whole 24hr my state would remain the same?
I read your code and that is precisely what the problem is. See, for the whole 24hr of the "4th day" the while statement will remain true, since when the clock hits night, my day would be the same till the night of the next day.
Look at the code again. You would detect a change of day by comparing the current value of the day with the previous value of the day. When they are different then a new day has started and you check whether the new value matches the required day number and if so you run whatever code you require.
Next time you check for a change of day it won't have happened so you do nothing. This way you only act when the day becomes the value that you are looking for rather than when it is the value that you are looking for
bool stuffDone = false;
int dayOfMonth;
if (dayOfMonth == 4 && !stuffDone)
{
// do stuff here
// will only run once, because at the end...
stuffDone = true;
}
if (dayOfMonth != 4)
{
// the next day, we reset it
stuffDone = false;
}
void function() {
static byte prev_day; //static variable retains its value between calls to function()
const byte set_day = 4;
byte new_day = getadatefromNTPserver //the day will be receieved from the ntp server and for a whole24 hours the state will remain the same.
if ((new_day != prev_day) && (new_day == set_day)) {
//this only runs on set_day, when the day changes from the previous day
//reset loop
} else {
//do stuff
}
prev_day = new_day;
}
Thanks, David. I think you cracked the code. I can't test this right now because I am out of the city. But when I get back and this work, I will mark this as a solution.