Arduino Piano

Hello people I am new to arduino and need some assistance

The Project: Make a 5 button “piano” that makes different sounds depending on which button is pressed.

The problem: I have got the first 3 buttons working but cannot get the last 2 to work.

What I have so far: Wiring:

The Code: #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

int buttons[6];
// set up an array with 6 integers

// give the first element of the array the value 2

int notes = {262,294,330,349};
void setup () {
Serial.begin(9600);
}
void loop() {
int keyVal = analogRead(A0);
Serial.println(keyVal);
if(keyVal == 1023){
tone(8, notes[0]);
}

else if(keyVal >= 990 && keyVal <= 1010){
tone(8, notes[1]);
}
else if(keyVal >= 5&& keyVal <=515){
tone(8, notes[2]);
}
else if(keyVal >=5 && keyVal <= 10){
tone (8, notes[3]);
}
else if(keyVal >=5 && keyVal <= 10){
tone (8, notes[4]);
}
else if(keyVal >=5 && keyVal <= 10){
tone (8, notes[5]);
}
else if(keyVal >=5 && keyVal <= 10){
tone (8, notes[6]);
}
else if(keyVal >=5 && keyVal <= 10){
tone (8, notes[7]);
}
else{
noTone(8);
}
}

Any and all help is appreciated thank you

Load up the example AnalogReadSerial and check that each button produces a distinct analog reading. Then change your if() statements to pick out those readings.

If two buttons produce the same analog number (within the tolerance of the analog noise) then you will need to change your wiring - change a resistor.

MorganS: Any and all help is appreciated thank you

As a first step I'd load a sketch and debug the value that each button generates:

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A0));
  delay(250);
}

Watch the serial monitor while pressing each button!

Write down the values you get: - no button pressed? - button-1 pressed? - button-2 pressed? - button-3 pressed? - button-4 pressed? - button-5 pressed?

If each button generates a typical value that is different from other buttons, you can do further work.

If any button produces very much "changing values" or "same value as other button", you'd have to rework your hardware setup.

Or you create a duplicate of the resistor ladder like used in the "LCD Keypad Shield" as a circuit:

               A0
               |
        2K     |  330      620       1K      3.3K
       ____    |  ____     ____     ____     ____
5V  --|____|---+-|____|-+-|____|-+-|____|-+-|____|-+
               |        |        |        |        |
               |        |        |        |        |
                /        /        /        /        /
               |        |        |        |        |
               |        |        |        |        |
GND -----------+--------+--------+--------+--------+

In that case the "LCD Keypad Shield" example code for reading the buttons should work. But also in that case I'd first check if the hardware wiring is correct and each button provides a reasonable analog value by using the test sketch.

int notes[] = {262,294,330,349};

Only the first three buttons work because you have only given values for indexes 0, 1, 2, and 3 (arrays start with index 0) and you are using 1, 2, 3, 4, 5, and 6. You should be calling your buttons 0, 1, 2, 3, 4, and 5. You should then add two more notes to your list of notes so button 4 and button 5 produce notes.

i fixed my code and have gotten the fifth button running. FOURTH ONE STILL WON'T WORK :((

hasaamb: i fixed my code and have gotten the fifth button running. FOURTH ONE STILL WON'T WORK :((

In reply #2 I posted a simple test sketch for the button values to show up in the serial monitor.

Which values do you get from each button?

thanks for the help but im not sure how to do that

hasaamb: thanks for the help but im not sure how to do than

Follow the instructions in reply #2.

jurs: In reply #2 I posted a simple test sketch for the button values to show up in the serial monitor.

Which values do you get from each button?

No Button: 0 Button 1: 8 Button 2: 514 Button 3: 1002 Button 4: 0 Button 5: 1023

hasaamb: No Button: 0 Button 1: 8 Button 2: 514 Button 3: 1002 Button 4: 0 Button 5: 1023

"No Button" and "Button 4" are exactly the same value.

"Button 1" is hardly any different than the two above.

And finally "Button 3" and "Button 5" are nearly the same value.

So I'd say: Your circuit is wrong!

Either wrong by design, or wrong by cabling.

In a correct circuit, the values should have "equal distance" in the analogRead values. At least, the value of one button should be 100 or more different than the values of the other buttons.

I have posted a working circuit schematics in reply #2, including the resistor values needed.

Which is the circuit you wanted to build (if not that)?

hey, thanks again and i wasn't sure about the circuit as this is my first arduino project. I will try to build yours if i can