Exit music loop

Hello, I am new to this forum so please pardon me if I make any rookie mistakes. I am also new to Arduino but I study el. engineering so I am not completely lost. Anyway, I am making an alarm clock project that plays Take on me when it rings. The music works but I cant make it to stop. I tried using interrupts but I can't because I am using those pins for the segment display so it would take a lot of re-coding to make that happen so I thought that the following would be simpler but it doesn't work. Can somebody please help me figure it out.


//toggleAlarm is pin 13 set to INPUT_PULLUP

void sound()
{
digitalWrite(ledSet, HIGH);
digitalWrite(ledSetAlarm, HIGH);
digitalWrite(ledAlarm, HIGH);
while(!digitalRead(toggleAlarm==LOW)){
for (int thisNote = 0; thisNote < notes * 2 && !digitalRead(toggleAlarm==LOW); thisNote = thisNote + 2) {
//Display(time);
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration * 0.9);

// Wait for the specief duration before playing the next note.
delay(noteDuration);

// stop the waveform generation before the next note.
noTone(buzzer);
}
}
digitalWrite(ledSet, LOW);
digitalWrite(ledSetAlarm, LOW);
digitalWrite(ledAlarm, LOW);
}

And how do I put the code in the window like I see in other posts? :slight_smile:

  while (!digitalRead(toggleAlarm == LOW))
  {
    for (int thisNote = 0; thisNote < notes * 2 && !digitalRead(toggleAlarm == LOW); thisNote = thisNote + 2)
    {
      //Display(time);
      // calculates the duration of each note
      divider = melody[thisNote + 1];

Some questions/comments

The syntax of the first line is wrong. Look carefully at the parameter of the digitalRead() function

Assuming that you fix that then the code will stay in the while loop until toggleAlarm goes HIGH so it is no good testing it again (wrongly) as you already know that it is HIGH

Both conditions need to be true to end the for loop. Did you mean for it to end if either of them is true ?

What code should the for loop execute ?
What code does it actually execute ?

OMG thank you so much, I spent hours on finding the error. The syntax for the condition in while and for loops was the whole problem (and they were both wrong because I just copied and pasted it).

That solves it.

The for loop plays the notes from an array and the while loop repeats the song over and over. So both should run until the button is pressed.

That's why I put while(button is NOT low) (to stop the song from repeating) and also in the for loop (to stop at immediately at the next note.
The button is INPUT_PULLUP and connected to GND so when it is pressed it will go low and break the loop.

That's why I put while(button is NOT low)

Personally I would have used == HIGH as the NOT operator is easily missed when reading and understanding code.

I suspect that there may still be problems lurking in your program. Please post your fixed code as it is now

waratu:
And how do I put the code in the window like I see in other posts? :slight_smile:

Please read HOW TO USE THIS FORUM
it is a sticky post at the top of every forum.

#7 discusses CODE TAGS
the use of code tags is how you are supposed to post code.

by the way, if you do edit your first post, just before your code, put in a note
(original posted edited to put code in code tags)

that way, following posts will not see out of place.
oh, and to edit, go to the bottom right of your post - more > modify