Stop the buzzer playing melody with button

Hello there

I have this program, which plays different tunes on the buzzer depending on which one is selected on the LCD menu. I have an OK button to play the melody, I want to create a new Stop button to ... stop the melody while its played, or I can use the same button I used to play it to stop it.Anyone can help ?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

int Stop=10;
bool StopLastState=HIGH;
int RIGHT = 8; //This is the selecting button
bool RIGHTLastState = HIGH; //Helps for the menu program
int OK = 13; //You know what this one is for
bool OKLastState = HIGH; //Helps for the menu program
const int menuSize = 4; //how many menus i want
String menuItems[menuSize];  
int currentMenu = 0; //The first menu you get when powering up the arduino
int buzzer = 3; //My buzzer

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
// First menu name
menuItems[0]= "IMPERIAL MARCH" ; 
// Second menu name
menuItems[1]= "PIRATE" ; 
// third menu name
menuItems[2]= "SONG III" ;
// fourth menu name
menuItems[3]= "SONG IV" ; 
lcd.begin(16, 2);
lcd.print(menuItems[currentMenu]);  //Printing the first menu right when we plug in the arduino

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

pinMode(OK, INPUT_PULLUP);
pinMode(Stop, INPUT_PULLUP);
pinMode(RIGHT, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



void loop() {
 if(digitalRead(RIGHT) != RIGHTLastState)
{
    if (currentMenu==menuSize) {
      currentMenu=0;
    
    } else {
      currentMenu++;
      if (currentMenu > 3)
      currentMenu = 0;  
    }
    lcd.clear() ;
   lcd.print(menuItems[currentMenu]);    }
  else {}  
delay(150);

if (digitalRead(OK) != OKLastState) {
switch (currentMenu) {
case 0 : 
imperial();
break ;
case 1 :
pirate();
case 2 :
pirate();
break;
case 3 :
imperial();
break;
}
}
}

Fuzzyzilla:
Though you did not include you functions for actually playing the songs, I may still be able to help...

If your song playing code operates in some sort of loop, like a "while" or "for" loop, you can just have an if statement checking if the button is pressed. If so, have a "return" statement to escape the function, going back to the main function loop.

Again, I have no way of verifying if this will work, as not all code was included here!

Thanks for the response, I thought it would be extra code to share the songs but here they are !

First one :

#define  C0 16.35
#define Db0 17.32
#define D0  18.35
#define Eb0 19.45
#define E0  20.60
#define F0  21.83
#define Gb0 23.12
#define G0  24.50
#define Ab0 25.96
#define LA0 27.50
#define Bb0 29.14
#define B0  30.87
#define C1  32.70
#define Db1 34.65
#define D1  36.71
#define Eb1 38.89
#define E1  41.20
#define F1  43.65
#define Gb1 46.25
#define G1  49.00
#define Ab1 51.91
#define LA1 55.00
#define Bb1 58.27
#define B1  61.74
#define C2  65.41
#define Db2 69.30
#define D2  73.42
#define Eb2 77.78
#define E2  82.41
#define F2  87.31
#define Gb2 92.50
#define G2  98.00
#define Ab2 103.83
#define LA2 110.00
#define Bb2 116.54
#define B2  123.47
#define C3  130.81
#define Db3 138.59
#define D3  146.83
#define Eb3 155.56
#define E3  164.81
#define F3  174.61
#define Gb3 185.00
#define G3  196.00
#define Ab3 207.65
#define LA3 220.00
#define Bb3 233.08
#define B3  246.94
#define C4  261.63
#define Db4 277.18
#define D4  293.66
#define Eb4 311.13
#define E4  329.63
#define F4  349.23
#define Gb4 369.99
#define G4  392.00
#define Ab4 415.30
#define LA4 440.00
#define Bb4 466.16
#define B4  493.88
#define C5  523.25
#define Db5 554.37
#define D5  587.33
#define Eb5 622.25
#define E5  659.26
#define F5  698.46
#define Gb5 739.99
#define G5  783.99
#define Ab5 830.61
#define LA5 880.00
#define Bb5 932.33
#define B5  987.77
#define C6  1046.50
#define Db6 1108.73
#define D6  1174.66
#define Eb6 1244.51
#define E6  1318.51
#define F6  1396.91
#define Gb6 1479.98
#define G6  1567.98
#define Ab6 1661.22
#define LA6 1760.00
#define Bb6 1864.66
#define B6  1975.53
#define C7  2093.00
#define Db7 2217.46
#define D7  2349.32
#define Eb7 2489.02
#define E7  2637.02
#define F7  2793.83
#define Gb7 2959.96
#define G7  3135.96
#define Ab7 3322.44
#define LA7 3520.01
#define Bb7 3729.31
#define B7  3951.07
#define C8  4186.01
#define Db8 4434.92
#define D8  4698.64
#define Eb8 4978.03
// DURATION OF THE NOTES 
#define BPM 120    //  you can change this value changing all the others
#define H 2*Q //half 2/4
#define Q 60000/BPM //quarter 1/4 
#define E Q/2   //eighth 1/8
#define S Q/4 // sixteenth 1/16
#define W 4*Q // whole 4/4


void imperial()
{
  //tone(pin, note, duration)
    tone(3,LA3,Q); 
    delay(1+Q); //delay duration should always be 1 ms more than the note in order to separate them.
    tone(3,LA3,Q);
    delay(1+Q);
    tone(3,LA3,Q);
    delay(1+Q);
    tone(3,F3,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    
    tone(3,LA3,Q);
    delay(1+Q);
    tone(3,F3,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    tone(3,LA3,H);
    delay(1+H);
    
    tone(3,E4,Q); 
    delay(1+Q); 
    tone(3,E4,Q);
    delay(1+Q);
    tone(3,E4,Q);
    delay(1+Q);
    tone(3,F4,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    
    tone(3,Ab3,Q);
    delay(1+Q);
    tone(3,F3,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    tone(3,LA3,H);
    delay(1+H);
    
    tone(3,LA4,Q);
    delay(1+Q);
    tone(3,LA3,E+S);
    delay(1+E+S);
    tone(3,LA3,S);
    delay(1+S);
    tone(3,LA4,Q);
    delay(1+Q);
    tone(3,Ab4,E+S);
    delay(1+E+S);
    tone(3,G4,S);
    delay(1+S);
    
    tone(3,Gb4,S);
    delay(1+S);
    tone(3,E4,S);
    delay(1+S);
    tone(3,F4,E);
    delay(1+E);
    delay(1+E);//PAUSE
    tone(3,Bb3,E);
    delay(1+E);
    tone(3,Eb4,Q);
    delay(1+Q);
    tone(3,D4,E+S);
    delay(1+E+S);
    tone(3,Db4,S);
    delay(1+S);
    
    tone(3,C4,S);
    delay(1+S);
    tone(3,B3,S);
    delay(1+S);
    tone(3,C4,E);
    delay(1+E);
    delay(1+E);//PAUSE QUASI FINE RIGA
    tone(3,F3,E);
    delay(1+E);
    tone(3,Ab3,Q);
    delay(1+Q);
    tone(3,F3,E+S);
    delay(1+E+S);
    tone(3,LA3,S);
    delay(1+S);
    
    tone(3,C4,Q);
    delay(1+Q);
     tone(3,LA3,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    tone(3,E4,H);
    delay(1+H);
    
     tone(3,LA4,Q);
    delay(1+Q);
    tone(3,LA3,E+S);
    delay(1+E+S);
    tone(3,LA3,S);
    delay(1+S);
    tone(3,LA4,Q);
    delay(1+Q);
    tone(3,Ab4,E+S);
    delay(1+E+S);
    tone(3,G4,S);
    delay(1+S);
    
    tone(3,Gb4,S);
    delay(1+S);
    tone(3,E4,S);
    delay(1+S);
    tone(3,F4,E);
    delay(1+E);
    delay(1+E);//PAUSE
    tone(3,Bb3,E);
    delay(1+E);
    tone(3,Eb4,Q);
    delay(1+Q);
    tone(3,D4,E+S);
    delay(1+E+S);
    tone(3,Db4,S);
    delay(1+S);
    
    tone(3,C4,S);
    delay(1+S);
    tone(3,B3,S);
    delay(1+S);
    tone(3,C4,E);
    delay(1+E);
    delay(1+E);//PAUSE QUASI FINE RIGA
    tone(3,F3,E);
    delay(1+E);
    tone(3,Ab3,Q);
    delay(1+Q);
    tone(3,F3,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    
    tone(3,LA3,Q);
    delay(1+Q);
     tone(3,F3,E+S);
    delay(1+E+S);
    tone(3,C4,S);
    delay(1+S);
    tone(3,LA3,H);
    delay(1+H);
    
    delay(2*H);
   
}

second song

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define melodyPin 3
#define melodyPin 3

void pirate()
{
int melody[] = {
NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4,NOTE_E4,NOTE_D4,NOTE_C4,NOTE_B3,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_D4,NOTE_C4,NOTE_B3,NOTE_C4,NOTE_B3,NOTE_A3,NOTE_B3,NOTE_G3,NOTE_A3,0,NOTE_A3,NOTE_B3,NOTE_C4,NOTE_B3,NOTE_C4,NOTE_D4,NOTE_C4,NOTE_D4, NOTE_E4,NOTE_D4,NOTE_C4,NOTE_A3,NOTE_A3,NOTE_B3,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_A3,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_B3,NOTE_A3,NOTE_C4,NOTE_B3,NOTE_A3};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
2,2,3,1,2,2,2,2,2,2,2,4,1,1,3,1,2,2,2,2,3,1,2,2,8,1,1,3,1,2,2,2,2,3,1,2,4,1,1,2,2,2,2,2,2,3,1,2,4,1,1,6 };

for (int thisNote = 0; thisNote < 52; thisNote++) {

int noteDuration = noteDurations[thisNote]*95;
tone(3, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}

Fuzzyzilla:
Though you did not include you functions for actually playing the songs, I may still be able to help...

If your song playing code operates in some sort of loop, like a "while" or "for" loop, you can just have an if statement checking if the button is pressed. If so, have a "return" statement to escape the function, going back to the main function loop.

Again, I have no way of verifying if this will work, as not all code was included here!

Yes my song playing must occur if I press the "play" or OK button as I named it the program, but I'm not sure if I'm answering your question or not, for the IF statement, I tried to put it as the first thing to do before playing any song.
Like : If (digitalRead(Stop) =! HIGH) { noTone(buzzer) }
as a result nothing changes beside the fact that if I press the stop button, and try to play a song, it wont play, like a security mechanism, I also tried to put the if statement inside every switch case, but it didnt work.

Fuzzyzilla:
For the first one, My code would surely not work.
This is, however, a very inefficient way to store songs.

I've actually done a project very similar to what you're doing. The list of notes works much better, I think.

"If (digitalRead(Stop) =! HIGH) { noTone(buzzer) }" Would only stop the buzzer for a brief second, continuing again once the next "tone()" is called. I suggest you use "return" to escape the program completely.

Also, (digitalRead(Stop) =! HIGH) is not a comparison. It's an assignment. What you need is (digitalRead(Stop)==LOW)

I started programming and Arduino for about a month ago, its all new to me, I dont know any other way to store songs.

I still dont really know what to do.

The second song you posted seemed perfect. In there, put
"If (digitalRead(Stop) !=HIGH)return;"

Fuzzyzilla:
The second song you posted seemed perfect. In there, put
"If (digitalRead(Stop) !=HIGH)return;"

I put the code at the end of the song, it has worked fine it stopped the song however after that the menu goes in a mess, showing the different menus without stopping.

The return function is to do what exactly ? in the definition of the web site it says "Terminate a function and return a value from a function to the calling function, if desired."

We want to terminate the song, and return no value, so its stops the code running, but whats next ? from which point in the program in starts ticking again ? the beginning like we just plugged in the USB of the Arduino ? it may seem non-sense and I apologize for it.

The return function goes back to wherever the function was called from.
So, in this case, it will go back into your switch statement.

The glitchy menus seems like you code cannot handle more than one selection.

One issue is that "OKLastState" is never updated, but it also doesn't need to be there at all.

You also never set the LCD's cursor to 0,0, meaning it'll print wherever it left off.

Fuzzyzilla:
The return function goes back to wherever the function was called from.
So, in this case, it will go back into your switch statement.

The glitchy menus seems like you code cannot handle more than one selection.

One issue is that "OKLastState" is never updated, but it also doesn't need to be there at all.

You also never set the LCD's cursor to 0,0, meaning it'll print wherever it left off.

but the code was handling well the selection, I was capable of choosing a song, play it, wait until it ends and then play another song, and since I'm at every selection clearing my LCD why do I need to set the cursor ?

Also, it is true that I can skip the bool data type and directly write if (digitalRead(OK) == LOW) for an example, since there is only two values to take from the button, thanks for pointing at that.