What is wrong with this piece of code?

This section of the code is for the alarm function on a binary clock. (For the full code, please see the attached .ino file)

I suspect that the problem is related to alarmRepeats++ not resetting and remembering the accumulated value.

When alarm starts playing, (goes off) it has been designed so that the owner can snooze it with pressing of any button. When snoozed, alarm should play again after 5 minutes. This is OK. Or the owner can switch alarm off by LongPressing Alarm button. Also is working fine.

But if alarm is let to run the scheduled 10 minutes without owner input, the alarm switches itself off automatically -- then, something happens in the system and if you make a new alarm time, and the alarm time is reached, alarm starts playing and switches itself OFF immediately within 2 seconds. Doesn't even allow the alarm to run. It is as if, it remembers to switch itself off immediately. Something is broken there. It only happens if alarm is let to run 10 minutes and switch itself off. Cycling the power to the clock, fixes that.

Any idea why it is so? (How to change the code??)

if (playAlarm) {
    alarmPlaying = true;
    effectsPlaying = true;
    effectsStage = 0;
    nextEffectMillis = millis();
    playAlarm = false;
  }

  if (alarmPlaying && effectsPlaying) {
    if (millis() > nextEffectMillis) {
      effectsStage++;
      switch (effectsStage) {
        case 1:
          effectDisplayClock = false;
          ledsOn();
          tone(speakerPin, 1865);
          nextEffectMillis += 500;
          break;

        case 2:
          effectDisplayClock = true;
          noTone(speakerPin);
          nextEffectMillis += 500;
          break;

        case 3:
          effectDisplayClock = false;
          ledsOn();
          tone(speakerPin, 1865);
          nextEffectMillis += 1000;
          break;

        case 4:
          noTone(speakerPin);
          effectDisplayClock = true;
          nextEffectMillis += 1000;
          alarmRepeats++;
          if (alarmRepeats < 200) {   // 3 seconds per sequence; 200x2 = 600 seconds = 10 minutes.
            effectsStage = 0;
          }
          break;

        default:
          alarmPlaying = false;
          effectDisplayClock = false;
          effectsPlaying = false;
          clockMode = MODE_CLOCK;
      }
    }
  }

arduinoware_sketch.ino (27.2 KB)

The code seems very over-complicated. There are soooooo many bool variables and your conditional statements have way too many conditions to follow.

I'd suggest stripping down the code...

I think you need to set alarmRepeats to 0 in the if statement at the beginning.

dekutree64:
I think you need to set alarmRepeats to 0 in the if statement at the beginning.

Thanks.

I was sort of thinking that too. But not sure where exactly. Which one of the if statements do you mean? Did you get a chance to see the full .ino file?

Thank you.

lucasdefunes:
This section of the code is for the alarm function on a binary clock. (For the full code, please see the attached .ino file)

As your program is 27k long I presume you have a recent earlier version that does work properly. What are the differences between that working version and the non-working version?

...R

Robin2:
As your program is 27k long I presume you have a recent earlier version that does work properly. What are the differences between that working version and the non-working version?

...R

Actually, the version which I have attached is the only version I have.

lucasdefunes:
Actually, the version which I have attached is the only version I have.

Then I guess you must not be its author? Maybe the author can help you?

...R

Robin2:
Then I guess you must not be its author? Maybe the author can help you?

...R

The author is too busy. I have already asked for paid help. He said he was too busy.

It's a big ask to expect someone to figure out 27k of code and tell you how to fix it for free if the author won't do it for money.

I'm certainly much too lazy to try.

To be honest it might actually be quicker to write a completely new program from scratch.

...R

Robin2:
It's a big ask to expect someone to figure out 27k of code and tell you how to fix it for free if the author won't do it for money.

I'm certainly much too lazy to try.

To be honest it might actually be quicker to write a completely new program from scratch.

...R

Yes. It is a lot easier to not do anything. You are right :))

lucasdefunes:
Yes. It is a lot easier to not do anything. You are right :))

That is not what I said.

...R