Duplicate execution

Hello,

I have a piece of code that works most of the time, but occasionally it slips up and executes again before it is supposed to. This is the code:

  if(minute() == 0 || minute() == 15 || minute() 30 || minute() == 45){
    if(minute() != lastMinute){
      flagBit = true;
      lastMinute = minute();
      Serial.println("Quarter Hour...");
      printTime();
    }
  }

I know that this is the code that is setting the flagBit because of the Serial.println(“Quarter Hour…”); that is logged with the time. Whenever it happens it is always right as the minute is changing from the quarter hour minute, the the next minute. Is there a better way to get a function to execute on the quarter hour? I looked at the timeAlarm library, but I wonder if the repeating alarm would shift around the hour due to the time inaccuracy of the arduino even though I re-sync with my local time server every hour. Any ideas/help would be great.

if(minute() == 0 || minute() == 15 || minute() 30 || minute() == 45)

should this not be

if(minute() == 0 || minute() == 15 || minute() == 30 || minute() == 45)

The big problem I see is that minute() might return different values in that lot. Say it is just about to tick over, you compare one value in one place and another in another. Better would be:

 int currentMinute = minute();
 if(currentMinute == 0 || currentMinute == 15 || currentMinute == 30 || currentMinute== 45){
    if(currentMinute != lastMinute){
      flagBit = true;
      lastMinute = currentMinute;
      Serial.println("Quarter Hour...");
      printTime();
    }
  }

Also the test might be more simply written:

if (currentMinute % 15 == 0)   // every 15 minutes

@ chrisnet :: Yes, you’re right. I typed it instead of copy/paste.

@ Nick :: Thank you. That looks like a better way to evaluate the current minute. Thanks, I’ll try that and let it run over the weekend here at work.

Yes, that solved my problem. The project ran without one false report over the weekend. Thank you both.

Why wouldn't the compiler complain about the missing == operator? "minute() 30" doesn't look like it should be valid, although I could be wrong about that.

Jiggy-Ninja: Why wouldn't the compiler complain about the missing == operator? "minute() 30" doesn't look like it should be valid, although I could be wrong about that.

It does complain:

@ chrisnet :: Yes, you're right. I typed it instead of copy/paste.

Please don't do that. We don't need to solve re-typing problems. We need to solve real problems.