Having trouble halting if statement while alarm is still true.

I have alarm set by hour and minute and dayofweek.
I set the alarms via access point HTML.
When Alarm is triggered it pusles my Remote Fob set number of times.
I put a delay (60000) to stop from looping back and doing the if statement again
Everything works like this.

From what I do know from reading around is delay is frowned upon.
I don't know any other way. I've tried break where delay is, while counter above if statement .
the code is long,

I have a project hub with more details.
https://create.arduino.cc/projecthub/dothedew92/rtc-esp32-multiple-timers-ap-web-interface-remote-start-57ee51

 if ((now.hour () == yourA1H) && (now.minute () == yourA1M ) && (yourA1S == 1) && ((now.dayOfTheWeek() == your1SU)||(now.dayOfTheWeek() == your1M)||(now.dayOfTheWeek() == your1T)||(now.dayOfTheWeek() == your1W)||(now.dayOfTheWeek() == your1TH)|| (now.dayOfTheWeek() == your1F)||(now.dayOfTheWeek() == your1SA))) { //if alarm time is true by hr, min,, and day of week (set as weekdays) sun0 mon1 tue2 wed3 th4 fri5 sat6
    for (A = 1; A <= yourP1; A++){
      digitalWrite(trigger, HIGH);
      delay(yourON);
      digitalWrite(trigger, LOW);
      delay(yourOFF);
      Serial.print("ALARM");
      
    }
    
    delay(6000);
  }

Thank you for any advice.

I'm sure rest of the code needs work also as its mainly snips from other projects and example codes.

It's easy. make a variable

bool AlarmSent = false;

Whenever you set the alarm,

AlarmSent = false;

and add

if (... check the time stuff... && AlarmSent == false) {
// send the alarm...
AlarmSent = true;

If you use the TimeLib, then you can use the TimeAlarms library.

For the DS3231, you need a library that is made to cooperate with the TimeLib library.

When you test the time to the second, then it is active during that second. If other code takes longer than a second, then you could have missed that second.

With fixed times it is easier.
Suppose you want to turn something on at 05:00 and turn off at 11:00, then you need a variable that tells if it is turned on or not. There is even no need to test the minutes and seconds.

bool turnedOn;

void loop()
{
  if( hour == 5 && !turnedOn)
  {
    digitalWrite( ..., HIGH);
    turnedOn = true;
  }

  if( hour == 11 && turnedOn)
  {
    digitalWrite( ..., LOW);
    turnedOn = false;
  }
}

An other way is to convert everything to seconds-since-1970 and remember if the alarm was activated.
Suppose the alarms should sound at 1700000000, then a ">=" can be used.

time_t t;
bool alarmWasActivated = false;
if( t >= 1700000000UL && !alarmWasActivated)
{
  // sound alarm
  digitalWrite( ..., HIGH);
  alarmWasActivated = true;
}

There has to be a moment that the 'alarmWasActivated' is reset to 'false'. Perhaps at the moment that a new alarm was set.

When you want to test for hours, minutes and seconds, then it is almost impossible to make it fail-safe. Suppose the seconds is 59, and due to some delay in the code a second has passed, then it is hard to check if the right moment has passed.

If all of this is too much trouble, then the easiest solution is to use the TimeAlarms library.

Thank you aarg.
The bool seemed promising.
Wouldn't work unless the bool was above void setup. Is that correct ?

(if) looks like this.

if ((now.hour () == yourA1H) && (now.minute () == yourA1M ) && (yourA1S == 1) && (AlarmSent == false)&&((now.dayOfTheWeek() == your1SU)||(now.dayOfTheWeek() == your1M)||(now.dayOfTheWeek() == your1T)||(now.dayOfTheWeek() == your1W)||(now.dayOfTheWeek() == your1TH)|| (now.dayOfTheWeek() == your1F)||(now.dayOfTheWeek() == your1SA))) { //if alarm time is true by hr, min,, and day of week (set as weekdays) sun0 mon1 tue2 wed3 th4 fri5 sat6
    for (A = 1; A <= yourP1; A++){
      digitalWrite(trigger, HIGH);
      delay(yourON);
      digitalWrite(trigger, LOW);
      delay(yourOFF);
      Serial.print("ALARM");
      
    }
    
    AlarmSent = true;
  }

Alarm gets triggered.
(for) statement pulses 5 times determined by the yourP1 SPIFF file.
and no more.
Even if the time is true again. Because AlarmSet is still True.
The alarm is repeating alarm as in same time every day of the week its set to. So how do I set the AlarmSet back to false. if the alarm is already set.

I tried AlarmSet=false in the void setup. nothing different
In the void loop before the (if). it activates alarm, pulses the (for) then finishes the statement, reads all the spiffs files (takes about a second. comes to the (if) again and sees that alarm is true and repeats until the now.minute changes.

I'm using an ESP8266 WHich has no internal hardware-RTC. So I coded a software RTC that gets synched once per day to the time of a NTP-server- This is precise enough for me.

You are using an ESP32. Will the ESP32 be powered from a battery or a wall-plug?
ESP32 do have an internal hardware-RTC. So IMHO there is no need for an external RTC.

And if you keep connected to your WLAN-router you could even use a NTP-server to keep time accurate.

As a general hint you should post your entire sketch. In 98% of all cases the entire sketch is needed for various reasons
like your potential helpers need to know all about your sketch, the error is somewhere else then the OP guessed where the error might be

best regards Stefan

Thanks stepfan.
I didn't know about internal rtc.

Aparently not the same as dedicated rtc. That's okay though ds3231 wasn't to much trouble and cheap enough.

Unfortunately the project will be mobile, not able to get NTP time for majority of the time. It will be staying in the car plugged into the 12v port.

It's a neat project and is currently working. Or doing what I need it to do

Sorry, Half my last post got cut.
I learned a lot with this project. At this point just trying to learn more and make the sketch better.
Soon I'll be trying power management. As long as if it doesn't interfere with WiFi access point.

Full project is linked in first post through project hub.
I'll try to post the full sketch here next time on PC.

Thanks again.
Also to Koepel.

In that case, a RTC with a battery is better.
Is the TimeAlarms library still an option ? This library will work with the TimeLib: GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks.

Which DS3231 module do you have ? Some of the cheap modules have counterfeit/discarded DS3231 chips, some have a charging circuit that will damage the rechargeable battery.

The forum saves "drafts" when you write something.
Try this:

  • click in the upper-right corner on your name/avatar.
  • select "profile"
  • there is a "forum settings edit", click "edit".
  • on the left, click "Show Drafts".
    You found the drafts !

[Update] There is also an option right-below the text field, "More / Modify". With "Modify" you can fix something that was not okay. For example when a picture or a link turned out to be wrong. Sometimes I put [Update] in front of something new.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.