Playing a melody using a button & a counter

Hi there.

I’m trying to make a menu in an LCD, this menu has 2 buttons, the first one is a selection button and the other one is an “OK” button, what I want is to select a song, and after that when I press “OK”, i’ll get, on the buzzer, a melody played by the buzzer depending on which menu I selected of course.

What I need is a synchronization between the menu I’m actually choosing and the song to be played, what I mean by that is, in my menu, the first option, for example, Queen, must actually play me a Queen song and not any of the other melodies, what I thought might be a good idea is, to make a counter, which will count everytime I press the selection button, I have 4 different bands in my menu, so the counter will go 0 1 2 3 0 1 2 3 0 and therefore, when I’ll press OK, the program will check the counter, lets suppose for instance its equal to 2 and will launch melody 2.

I know this may seem long and pretty basic, but I’m totally new to Arduino & programming so I’m trying to apply basic ideas.

Now, for the program, here is what I wrote :

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
#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


int RIGHT = 8; //This is the selecting button
int OK = 13; //You know what this one is for
bool OKLastState = HIGH; //Helps for the menu program
int count = 0; //Initialising my counter
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
bool RIGHTLastState = HIGH; //Helps for the menu program
int buzzer = 3; //My buzzer


void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// First menu name
menuItems[0]= "QUEEN" ; 
// Second menu name
menuItems[1]= "PINK FLOYD" ; 
// third menu name
menuItems[2]= "MUSE" ;
// fourth menu name
menuItems[3]= "LED ZEPPELIN" ; 

lcd.begin(16, 2);
lcd.print(menuItems[currentMenu]);  //Printing the first menu right when we plug in the arduino
Serial.println(count);
pinMode(OK, INPUT_PULLUP);
pinMode(RIGHT, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}

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

if (digitalRead(OK) != OKLastState)
{
  if (count=0)
  {
   imperial();
   Serial.println("ça marche");
  }
    else {
    }
    }
}


void imperial()
{
    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);
    noTone(150);
}

The result is, the menu is working fine, but when for example on the monitor, the counter is equal to zero, when I press the OK button, the “imperial” song doesnt work. The led on the arduino stops blinking which normally means there is a change of voltage from high to low when pressing the OK button, but the buzzer doesnt play the song…

And which littlebits modules are used in the circuit ?
Are you using the MP3 bit and, or arduino bit ?
Littlebits has no LCD bit :open_mouth:
Uh oh …

How are you hooking up and working with an LCD? I’m trying to get code working similar to the tutorial posted by EEEnthusiast on YouTube, but I can’t seem to be able to output the counts to an LCD.

Hi @PanicStation @JordanB
You know that the littleBits arduino module is based on a Leonardo chip ?
At the moment there’s no LCD bit to connect to.
If you like to play a song by a button press, why don’t you simply use the MP3 bit ? It has also an input to trigger a song. You could use the number bit to display the song selected.
I don’t know exactly, but I think on the inventions page, someone constructed a radio that played a song depending on the card inserted into a slot.
Greetings

Hi @PanicStation @JordanB
I found the invention, 8-bit jukebox

It’s an awesome project and can be easily adapted to your needs.
Greetings