10 key piano

Hi everyone.

My group for engineering projects has a code that can play one note but we were wondering how to make it so it could play 10 different notes.

Here's the code we're using:

#include "pitches.h"
const int buttonPin = 2; // the number of the pushbutton pin
int note1 = NOTE_C4; // define note sound

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// sound tone
tone(8, note1);
}
else {
//turn off sound
noTone(8);
}
}

We're not sure how to add more notes to this.

Thanks in advanced :slight_smile:

Read more buttons, with different notes for each one.

void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// sound tone
tone(8, note1);
}
else {
//turn off sound
noTone();
}
buttonState2 = digitalRead(buttonPin2);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// sound tone
tone(8, note2);
}
else {
//turn off sound
noTone();
}
// etc. for buttons 3-10
// then read up on doing this with arrays!
}

for (x=1 to 10; x<11; x=x+1){
    if ( digitalRead[x] == HIGH) {
         // sound tone
         tone(8, note[x]);
    }
else {
//turn off sound
noTone([x]);  // or 8?
}
}

//something along these lines anyway
// not sure if you can have tone on any pin? Or just 1 note at a time but always on the same pin.

Thank you for the fast response! We figured it out! :slight_smile: