Hi, I've following code, it works as expected but just out of curiosity I wonder how would I do it without needing "else if" statement.
In my code, Arduino jumps to this ISR multiple times a second.
void CheckAlarmCondition()
{
DateTime now = RTC.now();
if(enableAlarm==true && now.hour()== alarmHour && now.minute()>=alarmMinute && now.minute()<alarmMinute+periodMin)
{
alarmActivated=true;
digitalWrite(AlarmOut,LOW);
}
else if(enableAlarm==true && now.hour()==alarmHour && now.minute()==alarmMinute+periodMin && now.second()<periodSec)
{
}
else if(alarmActivated==true)
{
digitalWrite(AlarmOut,HIGH); //Deactivate Relay
lcd.clear();
alarmActivated=false;
}
}
I've set an alarm at 18:30(alarmHour:AlarmMinute)
I've set alarm duration as 3 minutes and 30 seconds(periodMin, periodSec)
I've enable the alarm(enableAlarm==true)
So my first if statement checks if alarm is enabled and it is 18 o'clock and current minute is >=30 and at the same time current minute is < 33;
Then Arduino change alarmActivated bool variable to true and activate(LOW is active in my case) AlarmOut pin.
So this if will be true when the time is 18:30 and 18:31 and 18:32. There is no need to check the seconds value yet so I dont check it in first if statement.
But when it is 18:33 now I need to check if there is periodSec to count. For this case as I set alarm period as 3 minutes 30 seconds so alarm should be still active for 30 more seconds. This condition is checked in "else if" statement. But as this condition might be true only in case the first if statement was true before, I dont need to activate the AlarmOut, as I didnt deactivate it yet.
After 30 seconds "else if" statement is also false, so arduino checks if alarmActivated was true meaning the first "if" statement run before. Then it deactivates AlarmOut.
This code should work, but it also does not make sense to have an else if condition that is actually empty. There should be more logical way to do this. What would be your recommendation?
I believe I need something like this:
if(a and b and c and d are true || or a and b and c and e are true)
{do this}