polyphonic piano with arduino??

/*
Piano

Using 8 push buttons attached to pins 2 thou 9 and a speaker
attached to pin 13. Create a C minor piano. The button on pin
2 will play a low C and the button on pin 9 will play middle c.

The Circuit.

  • push buttons attached to pins 2 thou 9 from ground
  • speaker attached to pin 13 from ground

With this basic setup you can easily add 3 more notes. You can
also go in and change the frequency to get the notes you desire.

created 20 Jan 2010
by digimike

*/

boolean button[] = {2, 3, 4}; //create array for button pins
boolean speakerpin = 13; //sets speaker pin
boolean buttonset = 0; //variable for reading button push
int tone = 0;
void setup() {
// set all pins to output
pinMode(speakerpin, OUTPUT);
for(int x=0; x<8; x++) {
pinMode(button[x], OUTPUT);
}
// buttons are in the high position
for(int x=0; x<8; x++) {
digitalWrite(button[x], HIGH);
}
}

void loop()
{
// checks the state of each button
for(int x=0; x<8; x++) {
buttonset = digitalRead(button[x]);

if(buttonset == LOW && button[x] == 2){ // if button on pin 2 is pressed
tone = 1050; //stores the note's wavelength to be played.
}
if(buttonset == LOW && button[x] == 3) {
tone = 940;
}
if(buttonset == LOW && button[x] == 4) {
tone = 837;
}

}
while(tone > 0) { // as long as there is a not to be played
digitalWrite(speakerpin, HIGH); // turn on 5V to speaker
delayMicroseconds(tone); // delay the designated wavelength in ms
digitalWrite(speakerpin, LOW); // turn off speaker
delayMicroseconds(tone); // delay the designated wavelength in ms
tone = 0; // resets tone variable to 0 so the tone dosn't play constantly
}

}

The above is the sketch, how can I hold/save the key whenever they are held down. Also, how can I hold/save tones then output to 4 channels. I want to produce a simple polyphonic elec. piano only, so i don't want to use "DAC" method. Please dive me some directions, thanks to all..

how can I hold/save the key whenever they are held down.

This code:

buttonset = digitalRead(button[x]);

Is reading the current state of the switch on pin button[x]. The switch is being held down if it's state is the same as the last time you read it.

This means that you need to save the state of the button, in an array, and compare the current reading to the previous reading.

Questions: You are reading the state of 8 pins, whose numbers are stored in an array of length 3. Why does the array length not match the number of pins to read? Why is the type of the array boolean? That is not compatible with the data being stored in the array.

Why are speakerpin and buttonset booleans? The values being stored in these variables is not true or false.

SOrry Pauls...

I want to store states of more than one keys. Then output to different channels. Just like a polyphonic elec. piano.
eg.

Suppose 3 keys are connected to Arduino,

Key 1 (held down)~>Store
(keep on scanning)
Key 2 (Normal state)~>ignore
(keep on scanning)
Key 3 (held down)~>store

Play tones which were mapped to different keys

Then output to first channel and third channel.

I want to implement my product likes above.
Can someone help me?

The first thing you need to do is use variables of the correct type. The pin numbers are NOT boolean. If size is an issue, you can use byte. If not, use int.

The second thing you need to do is make the loops that access array data match the size of the array being accessed. Don't try to access button[5] when button is declared an array of size 3.

The third thing you need to do is to create another array to store the state of the switches.

byte switchState[3];

In the loop that reads the state of button[x], compare the value read (buttonset) to the previous state, in switchState[x]. If they are different the switch was pressed and is now released or it was released and is now pressed. It appears that you want to ignore these cases. So, what's left is the cases where the previous switch state is the same as the current state. That happens when the switch is pressed and was pressed or when the button was released and is released.

It appears as though you only want to handle the first case, so check that the states are the same and either is LOW.

At the end of the loop, store the current state of the switch (buttonset) in the array (switchState[x]).

Pauls

Pardon me....

Can you give me some examples about it. Actually, I don't understand all the things you said.

I'm a newbie in Arduino.

In your code, change these three lines:

boolean button[] = {2, 3, 4};  //create array for button pins
boolean speakerpin = 13;  //sets speaker pin
boolean buttonset = 0;  //variable for reading button push

to:

byte button[] = {2, 3, 4};  //create array for button pins
byte speakerpin = 13;  //sets speaker pin
byte buttonset = 0;  //variable for reading button push
byte switchState[3];

Change all of these lines (there are at least 3):

for(int x=0; x<8; x++) {

to:

for(int x=0; x<3; x++) {

Change this:

for(int x=0; x<8; x++) {
   buttonset = digitalRead(button[x]);
 
   if(buttonset == LOW && button[x] == 2){ // if button on pin 2 is pressed
     tone = 1050;    //stores the note's wavelength to be played.
   }
   if(buttonset == LOW && button[x] == 3) {
     tone = 940;
   }
   if(buttonset == LOW && button[x] == 4) {
     tone = 837;
   }
   
 }

to this:

tone = 0;
for(int x=0; x<8; x++)
{
   buttonset = digitalRead(button[x]);
 
   if(buttonState[x] == buttonset)
   {
      if(buttonset == LOW)
      {
         if(x == 0)
         { // if button on pin 2 is pressed
            tone = 1050;    //stores the note's wavelength to be played.
         }
         if(x == 1)
         {
            tone = 940;
         }
         if(x == 2)
         {
            tone = 837;
         }
      }
   }

   buttonState[x] = buttonset;
 }

At the end of this loop, tone will be 0 if no switch that was pressed last time is still pressed. Otherwise tone will have a value based on the highest numbered pin that is still being held down.

Notice that the value of tone no longer depends on the pin number. Instead, it depends on the order of pins defined in the array. You can change the pins in the array, and the tone will now follow.

Before, if you moved the switches to different pins, you'd need to change the array and the if statements to match.

you said byte switchState[3]; is store the 3 tones by using 8 bits.
When I add

switch (buttonset);
byte switchState[3];

at the bottom of loop, bow can I get back the tones :sunglasses: ?
Then output them separately to 3 ch. .

Sorry I want to ask you more. Does this code output square wave or others? I think it can't be polyphonic.

you said byte switchState[3]; is store the 3 tones by using 8 bits.

No. I said that the switchState array is to store the state of the switches.

If you want to note whether more than one button is held down, you need an array of tones.

int tones[3];

In the loop, set tones[x] to 0. Then check the now/then status, and, if a tone is to be played, set tones[x] to that value.

You don't need a while loop to play the tone. A simple if is sufficient. If you want to play more than one, you'd use a for loop to cycles through the tones array, and digitalWrite the appropriate pin HIGH. Then, delay, then digitalWrite all the pins LOW.

:-/
tone = 0;
for(int x=0; x<8; x++)
{
buttonset = digitalRead(button[x]);

if(buttonState[x] == buttonset)

Should I int or byte buttonState first?

/*
  Simple Polyphonic Piano

  Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
  attached to pins 11 thou 13.   Use  LED attached to pin 10.
  
The Circuit.
    * push buttons attached to pins 2 thou 9 from ground
    * speakers attached to pin 11,12 and 13 from ground
    * LED attached to pin 10 from ground
    
  With this basic setup you can easily add 3 more notes. You can
  also go in and change the frequency to get the notes you desire.
  
  created 3 Mar 2010
  by Silver

*/
#include <Tone.h>

//define notes
#define NOTE_C4  262 
#define NOTE_D4  294
#define NOTE_E4  330

int button[] = {2, 3, 4}; //button pins array
 //set button states array
byte speakerpin1 = 11; //speaker pin
byte speakerpin2 = 12;
byte speakerpin3 = 13; 

byte buttonset = HIGH;  //set button state high
byte switchState[3]; //an array for storing 3 button states


//variable for setting musical note
int led = 10; //LED pin


void setup() {
  // set all pins to output
  pinMode(speakerpin1, OUTPUT);  
  pinMode(speakerpin2, OUTPUT);
  pinMode(speakerpin3, OUTPUT);
  pinMode(led, OUTPUT);
  for(int x=0; x<3; x++) {
    pinMode(button[x], OUTPUT);
  }
  // buttons are in the high position
  for(int x=0; x<3; x++) {
    digitalWrite(button[x], HIGH);
  }
}

void loop()
{  
int tones[3]; //an array of tones
byte buttonState[] = {HIGH, HIGH, HIGH};   //set button states array
  
for(int x=0; x<3; x++)  // checks the state of each button

{
   buttonset = digitalRead(button[x]);  //put now state into buttonset

   if(buttonState[x] == buttonset)  //compare
           {
           if(x == 0)  //which key held down
           {
            tones[0] = NOTE_C4;  //store the tone in array
                switchState[0] = LOW; //store the state in array
         }
         if(x == 1)
         {
            tones[1] = NOTE_D4;
                switchState[1] = LOW;
         }
         if(x == 2)
         {
            tones[2] = NOTE_E4;
                switchState[2] = LOW;
         }
      }
   }
   for(int tones[3] = 0; tones[3] < 3; tones[3]++){
   if (tones[0] > 0){
     digitalWrite(speakerpin1, HIGH);
     delayMicroseconds(tones[0]);
     digitalWrite(speakerpin1, LOW);
     delayMicroseconds(tones[0]);}
   if (tones[1] > 0){
     digitalWrite(speakerpin2, HIGH);
     delayMicroseconds(tones[1]);
     digitalWrite(speakerpin2, LOW);
     delayMicroseconds(tones[1]);}
   if (tones[1] > 0){
     digitalWrite(speakerpin3, HIGH);
     delayMicroseconds(tones[2]);
     digitalWrite(speakerpin3, LOW);
     delayMicroseconds(tones[2]);}  
buttonState[x] = buttonset;
tones[] = [0, 0, 0];
} 
  
}

}

Dr. Pauls,

i revised the code. the code can be compiled but doesn't work.
I tried to store tones and switch states, is that right?
I added for loop to cycles tones array.
Can you point out some false to me?

Actually I don't know how to use array function and store something into it. Also I don't know how to loop an array....
but I tried .

the code can be compiled but doesn't work.

Well, I doubted that, so I copied and pasted it into a new sketch, and it doesn't compile. There are several errors - some with syntax, some with logic.

Logic issues, first.

            buttonset = digitalRead(button[x]);  //put now state into buttonset
            if(buttonState[x] == buttonset)  //compare
          {

Here you are reading the state of a switch on the pin whose number is stored in the xth position of the button array. Then, you compare that with the state that is stored in the xth position of the buttonState array. OK. But, doesn't it matter if the button is pressed, or not. This was, you are setting up to play a note is the button is pressed, and was pressed last time, or if the button is not pressed and was not pressed last time.

You need an outer if to test if the button is pressed.

      byte buttonState[] = {HIGH, HIGH, HIGH};   //set button states array

Declaring an array, and initializing it is good. Doing it in loop isn't. Every time loop is called, the array will be re-initialized. Oops, there goes the persistence you were trying to achieve. Move this to the top of the sketch (before setup).

                        switchState[0] = LOW; //store the state in array

Why? You never reference switchState again.

I used switchState in all me code, because the thing being pressed isn't a button. It's a switch. Buttons hold shirts closed. Switches pass, or not, trains and electric current. Now, I know that this is simply a matter of semantics, but if I suggest that you create an array called switchState, and you prefer to call it buttonState, that's fine. As long as you change ALL references to switchState to buttonState.

The xth position of switchState (or buttonState, if you prefer) needs to be set to buttonset at the end of the for loop, regardless of what value buttonset has or what value switchState[x] has. Otherwise, on the next pass through loop, we can't compare buttonset against the previous value, because we might not have stored the previous value.

Now, for the syntax errors.

      for(int tones[3] = 0; tones[3] < 3; tones[3]++)

The first part of the for loop statement is an initialization statement. A variable, tones[3] is declared, and initialized to 0. Well, tones[3] is not a valid variable name. It is a valid name for an array that can hold three values, but that is not what you want here. You want:

   for(int t = 0; t < 3; t++)

Next, you have three blocks of code that loop like this:

            if (tones[0] > 0)
            {
                  digitalWrite(speakerpin1, HIGH);
                  delayMicroseconds(tones[0]);
                  digitalWrite(speakerpin1, LOW);
                  delayMicroseconds(tones[0]);
            }

Suppose that the earlier for loop was correct, and that I had been holding two switches down - the 1st and third ones. At the end of the first for loop, tones[0] = NOTE_C4, tones[1] = 0, and tones[2] = NOTE_E4.

Now, you are going to make three passes through this loop. On the first pass, you are going to play notes NOTE_C4 and NOTE_E4, because tones=[0] and tones[2] are not 0. On the second pass, you are going to play the same notes again, because the values in tones haven't changed. On the third pass, you are going to play the same two notes again.

You want one block of code, with t as the array index:

            if (tones[t] > 0)
            {
                  digitalWrite(speakerpin1, HIGH);
                  delayMicroseconds(tones[t]);
                  digitalWrite(speakerpin1, LOW);
                  delayMicroseconds(tones[t]);
            }

Now this presents a problem, because all three notes are going to be played through the same speaker. To solve this, the speaker pins could be put in an array, and references in this for loop. The problem with that approach is that it would play the first note, then the second note, then the third note, if all three switches are held down.

If you want all three notes to play at the same time, you need to keep the code that you have in the for loop, but get rid of the loop. This is still going to play one note, then the second note, then the third note, because you haven't coded them to play simultaneously.

If they are to play simultaneously, you will need to get rid of the delay function calls. The blink without delay example shows how to do it. But, before you go there, consider that you are including the header file for the tone library, but you are not using it. Look at the examples on the Tone library page for how you should be playing the tones.

Finally, you have this code:

            buttonState[x] = buttonset;
            tones[] = [0, 0, 0];

The first statement belongs in the first loop, and corrects one of the problems I mentioned above.

The second is an attempt, I think, to reset the tones array to all 0's. There are two problems with this. First, you can not set all the values of an array in a single statement. You need to use:

tones[0] = 0;
tones[1] = 0;
tones[2] = 0;

Second, you do not want to be resetting tones inside the loop that is trying to play the tones. The elements of the tones array should be set at the top of the first for loop:

tones[x] = 0;

This assures that at the end of the first for loop, the tones array contains a 0 or a frequency.

Considering how the tone library works, though, I think you are going about this all wrong. In loop, you need simply look at whether button[x] is pressed, or not. If it is, and the note is playing, leave it playing. If the note is not playing start it playing. If button[x] is not pressed, stop playing the note.

CHANGED code...by following problems you pointed out..!!
sounds come!!!
But unfortunately...
Note_C4 keeps on plying= = :o
whenever I press 2nd switch, it output higher note,
whenever I press 3rd switch, it output higher note.

/*
  Simple Polyphonic Piano

  Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
  attached to pins 11 thou 13.   Use  LED attached to pin 10.
  
The Circuit.
    * push buttons attached to pins 2 thou 9 from ground
    * speakers attached to pin 11,12 and 13 from ground
    * LED attached to pin 10 from ground
    
  With this basic setup you can easily add 3 more notes. You can
  also go in and change the frequency to get the notes you desire.
  
  created 3 Mar 2010
  by Silver

*/
#include <Tone.h>

//define notes
#define NOTE_C4  262 
#define NOTE_D4  294
#define NOTE_E4  330

int button[] = {2, 3, 4}; //button pins array
int led = 10; //LED pin

byte speakerpin[3] = {11,12,13}; //array of speaker pin
byte buttonset = HIGH;  //set button state high
byte buttonState[3] = {HIGH, HIGH, HIGH};   //set button states array//an array for storing 3 button states

void setup() {
  // set all pins to output
  pinMode(speakerpin[0], OUTPUT);  
    pinMode(speakerpin[1], OUTPUT);
      pinMode(speakerpin[2], OUTPUT);
  pinMode(led, OUTPUT);
  for(int x=0; x<3; x++) {
    pinMode(button[x], OUTPUT);
  }
  // buttons are in the high position
  for(int x=0; x<3; x++) {
    digitalWrite(button[x], HIGH);
  }
}

void loop()
{  
int tones[3];
tones[0] = 0;
tones[1] = 0;
tones[2] = 0;
int buttonState[3];
buttonState[1] = HIGH;
buttonState[2] = HIGH;
buttonState[3] = HIGH;
  
for(int x=0; x<3; x++)  // checks the state of each button

{
   buttonset = digitalRead(button[x]);  //put now state into buttonset

   if(buttonState[x] == buttonset)  //compare
           {
           if(x == 0)  //which key held down
           {
            tones[0] = NOTE_C4;  //store the tone in array
                buttonState[0] = LOW; //store the state in array
         }
         if(x == 1)
         {
            tones[1] = NOTE_D4;
                buttonState[1] = LOW;
         }
         if(x == 2)
         {
            tones[2] = NOTE_E4;
                buttonState[2] = LOW;
         }
      }
   }
   for(int t = 0; t < 3; t++) {
   if (tones[t] > 0)
            {
        digitalWrite(speakerpin[0], HIGH);
      delayMicroseconds(tones[t]);
      digitalWrite(speakerpin[0], LOW);
      delayMicroseconds(tones[t]);
        
        digitalWrite(led, HIGH);
        delayMicroseconds(tones[t]);
        digitalWrite(led, LOW);
      delayMicroseconds(tones[t]);
            }

} 
  
}

I declare speaker array first, but it's useless now..
Change all switchstates to buttonstates...
byte buttonstate[3] =XXXXXXXXXX is moved to header...
tones[1],[2],[3] set to 0 before first for loop...
buttonstate[1][2][3] set to high before 1st for loop...

I now want to output likes you said

play the first note, then the second note, then the third note, if all three switches are held down.

Do this first,

play the first note, then the second note, then the third note, if all three switches are held down.

After that, fix polyphony problems= = :cry:

i read the code again & again...

 buttonset = digitalRead(button[x]);  //put now state into buttonset

   if(buttonState[x] == buttonset)  //compare
           {
           if(x == 0)  //which key held down
           {
            tones[0] = NOTE_C4;  //store the tone in array
                buttonState[0] = LOW; //store the state in array
         }
         if(x == 1)
         {
            tones[1] = NOTE_D4;
                buttonState[1] = LOW;
         }
         if(x == 2)
         {
            tones[2] = NOTE_E4;
                buttonState[2] = LOW;

if no button held down, buttonset is equals to high,
this will have no sound. I can't clearly describe what I think.
But I think this part may have some problems I don't know.
I just copied this part in earlier reply you posted. :-[

You need to post all of your code, again. But the problem is that you are not checking to see if the switch is pressed. You are only looking to see if the switch is in the same state it was last time, but you are not setting that correctly, either.

You are also not resetting tones[x].

Think about the variables involved, for the first button.

buttonState[0] is set to HIGH.
loop starts.
buttonset is determined to be HIGH (the button is not pushed).
buttonset is compared to buttonState[0]. They match, so tones[0] is set to NOTE_C4 and buttonState[0] is set to LOW.
the first for loop ends, and the second begins.
the note is played, because tones[0] is not 0.
loop ends.

loop starts.
buttonset is read. Suppose it is still HIGH (not yet pressed).
buttonset is compared to buttonState[0]. They do not match, so tones[0] and buttonState[0] remain unchanged.
the first for loop ends, and the second begins.
the note is played, because tones[0] is not 0.
loop ends.

loop starts.
buttonset is read. Suppose it is now LOW (pressed).
buttonset is compared to buttonState[0]. They match, so tones[0] and buttonState[0] are set to NOTE_C4 and LOW.
the first for loop ends, and the second begins.
the note is played, because tones[0] is not 0.
loop ends.

The first highlighted problem would not be happening if you checked whether the switch was pressed, before seeing if it's state matches the last state.

The second highlighted problem would not occur if you properly initialized tones in the first for loop (add tones[x] = 0; before the bottonset = analogRead line).

I'm fixing the highlighted problems.
First, can write this in reset tones[x] = 0????

tones[0] = 0;
tones[1] = 0;
tone[2] = 0;

because x is not declared in code.

/*
  Simple Polyphonic Piano

  Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
  attached to pins 11 thou 13.   Use  LED attached to pin 10.
  
The Circuit.
    * push buttons attached to pins 2 thou 9 from ground
    * speakers attached to pin 11,12 and 13 from ground
    * LED attached to pin 10 from ground
    
  With this basic setup you can easily add 3 more notes. You can
  also go in and change the frequency to get the notes you desire.
  
  created 3 Mar 2010
  by Silver

*/
#include <Tone.h>

//define notes
#define NOTE_C4  262 
#define NOTE_D4  294
#define NOTE_E4  330

int button[] = {2, 3, 4}; //button pins array
int led = 10; //LED pin
int tones[3];

byte speakerpin[3] = {11,12,13}; //array of speaker pin
byte buttonset = HIGH;  //set button state high
byte buttonState[3] = {HIGH, HIGH, HIGH};   //set button states array//an array for storing 3 button states

void setup() {
  // set all pins to output
  pinMode(speakerpin[0], OUTPUT);  
    pinMode(speakerpin[1], OUTPUT);
      pinMode(speakerpin[2], OUTPUT);
  pinMode(led, OUTPUT);
  for(int x=0; x<3; x++) {
    pinMode(button[x], OUTPUT);
  }
  // buttons are in the high position
  for(int x=0; x<3; x++) {
    digitalWrite(button[x], HIGH);
  }
}

void loop()
{  

tones[0] = 0;
tones[1] = 0;
tones[2] = 0;

buttonState[0] = HIGH;
buttonState[1] = HIGH;
buttonState[2] = HIGH;
  
for(int A=0; A<3; A++)  // checks the state of each button

{
   buttonset = digitalRead(button[A]);  //put now state into buttonset

   if(buttonState[A] == buttonset == HIGH)  //compare
           {
           if(A == 0)  //which key held down
           {
            tones[0] = 0;  //store the tone in array
                buttonState[0] = HIGH; //store the state in array
         }
         if(A == 1)
         {
            tones[1] = 0;
                buttonState[1] = HIGH;
         }
         if(A == 2)
         {
            tones[2] = 0;
                buttonState[2] = HIGH;
         }
      }
if(buttonState[A] != buttonset)  //compare
           {
           if(A == 0)  //which key held down
           {
            tones[0] = NOTE_E4;  //store the tone in array
                buttonState[0] = LOW; //store the state in array
         }
         if(A == 1)
         {
            tones[1] = NOTE_D4;
                buttonState[1] = LOW;
         }
         if(A == 2)
         {
            tones[2] = NOTE_C4;
                buttonState[2] = LOW;
         }
      }
   }
   for(int t = 0; t < 3; t++) {
   if (tones[t] > 0)
            {
        digitalWrite(speakerpin[0], HIGH);
      delayMicroseconds(tones[t]);
      digitalWrite(speakerpin[0], LOW);
      delayMicroseconds(tones[t]);
        
        digitalWrite(led, HIGH);
        delayMicroseconds(tones[t]);
        digitalWrite(led, LOW);
      delayMicroseconds(tones[t]);
            }

} 
  
}

now the result is: press one key, output one note. It looks like monophonic piano. Then, when I press two keys, it outputs another lower note I didnt define. Is that normal?

I can't do play 1st, then play 2nd, then play 3rd if 3 keys are held down...

the above is my result,because of it,
I seriously read your posts and my code again and again><
but I don't think the code has errors, may be I can't see them.
I think you are called Dr. Paul Stoffregen@@

I read the code example you gave me...@@only LED keeps on and off. I understand the concept but how to do it?

SOS!!!!!!!! :-/ :cry: :-/ :cry:

I really think you need to stop changing 47 things between iterations, and then saying you can't do what I suggest you do.

The tone library has functions to start playing a tone and to stop playing the tone. That's all you need to do.

When a button is pressed, start playing the tone. When the button is released, stop playing the tone.

The only thing you need to keep track of, in arrays, are the button pins and speaker pins.

int buttonPins[] = {8, 9, 10}; // Set these to match YOUR buttons
int speakerPins[] = {4, 5, 6}; // Ditto

Tone note1;
Tone note2;
Tone note3;

void setup()
{
   note1.begin(speakerPins[0]);
   note2.begin(speakerPins[1]);
   note3.begin(speakerPins[2]);
}

void loop()
{
   if(digitalRead(buttonPins[0]) == HIGH) // If the button is pressed
      note1.play();
   else
      note1.stop();

   if(digitalRead(buttonPins[1]) == HIGH) // If the button is pressed
      note2.play();
   else
      note2.stop();

   if(digitalRead(buttonPins[2]) == HIGH) // If the button is pressed
      note3.play();
   else
      note3.stop();
}

If your buttons are wired so that LOW is returned when the button is pressed, change HIGH to LOW.

but I followed your suggestions.
reset the tone to 0

reset the buttonstate to HIGH

do not include tone library frist because i didnt comprehend it

3 Speakers for 3 different notes

try to write code to check buttonstate and compare between previous and current

i posted wrong version of my code

/*
  Simple Polyphonic Piano

  Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
  attached to pins 11 thou 13.   Use  LED attached to pin 10.
  
The Circuit.
    * push buttons attached to pins 2 thou 9 from ground
    * speakers attached to pin 11,12 and 13 from ground
    * LED attached to pin 10 from ground
    
  With this basic setup you can easily add 3 more notes. You can
  also go in and change the frequency to get the notes you desire.
  
  created 3 Mar 2010
  by Silver

*/
int C = 262;
int D = 294;
int E = 330;

int button[] = {2, 3, 4}; //button pins array
int led = 10; //LED pin
int tones[3];

byte speakerpin[3] = {11,12,13}; //array of speaker pin
byte buttonset = HIGH;  //set button state high
byte buttonState[3] = {HIGH, HIGH, HIGH};   //set button states array//an array for storing 3 button states

void setup() {
  // set all pins to output
  pinMode(speakerpin[0], OUTPUT);  
    pinMode(speakerpin[1], OUTPUT);
      pinMode(speakerpin[2], OUTPUT);
  pinMode(led, OUTPUT);
  for(int x=0; x<3; x++) {
    pinMode(button[x], OUTPUT);
  }
  // buttons are in the high position
  for(int x=0; x<3; x++) {
    digitalWrite(button[x], HIGH);
  }
}

void loop()
{  

tones[0] = 0;
tones[1] = 0;
tones[2] = 0;

buttonState[0] = HIGH;
buttonState[1] = HIGH;
buttonState[2] = HIGH;
  
for(int A=0; A<3; A++)  // checks the state of each button

{
   buttonset = digitalRead(button[A]);  //put now state into buttonset

   if(buttonState[A] == buttonset == HIGH)  //compare
           {
           if(A == 0)  //which key held down
           {
            tones[0] = 0;  //store the tone in array
                buttonState[0] = HIGH; //store the state in array
         }
         if(A == 1)
         {
            tones[1] = 0;
                buttonState[1] = HIGH;
         }
         if(A == 2)
         {
            tones[2] = 0;
                buttonState[2] = HIGH;
         }
      }
if(buttonState[A] != buttonset)  //compare
           {
           if(A == 0)  //which key held down
           {
            tones[0] = 262;  //store the tone in array
                buttonState[0] = LOW; //store the state in array
         }
         if(A == 1)
         {
            tones[1] = 294;
                buttonState[1] = LOW;
         }
         if(A == 2)
         {
            tones[2] = 330;
                buttonState[2] = LOW;
         }
      }
   }
   for(int t = 0; t < 3; t++) {
   if (tones[t] > 0){
     if(tones[t] == 262){
        digitalWrite(speakerpin[0], HIGH);
      delayMicroseconds(tones[t]);
      digitalWrite(speakerpin[0], LOW);
      delayMicroseconds(tones[t]);}
     if(tones[t] == 294){
        digitalWrite(speakerpin[1], HIGH);
      delayMicroseconds(tones[t]);
      digitalWrite(speakerpin[1], LOW);
      delayMicroseconds(tones[t]);}
     if(tones[t] == 330){
        digitalWrite(speakerpin[2], HIGH);
      delayMicroseconds(tones[t]);
      digitalWrite(speakerpin[2], LOW);
      delayMicroseconds(tones[t]);}



        digitalWrite(led, HIGH);
        delayMicroseconds(tones[t]);
        digitalWrite(led, LOW);
      delayMicroseconds(tones[t]);}

} 
  
}

output: 1 note to 1 speaker
(when I press 2 keys, lower note comes from that 2 speakers)
(when I press 3 keys, lower note comes from that 3 speakers)

OK. We can keeping flogging this dead horse. I don't mind.

Start by explaining this:

           if(A == 0)  //which key held down
           {
            tones[0] = 0;  //store the tone in array
                buttonState[0] = HIGH; //store the state in array
         }
         if(A == 1)
         {
            tones[1] = 0;
                buttonState[1] = HIGH;
         }
         if(A == 2)
         {
            tones[2] = 0;
                buttonState[2] = HIGH;
         }

Wouldn't this work just as well:

  tones[A] = 0;
  buttonState[A] = HIGH;

followings are setting tone[0]to[2] to 0 and also setting ButtonState[0]to[2] to HIGH if NO key pressed.
I guess you know that

           if(A == 0)  //which key held down
           {
            tones[0] = 0;  //store the tone in array
                buttonState[0] = HIGH; //store the state in array
         }
         if(A == 1)
         {
            tones[1] = 0;
                buttonState[1] = HIGH;
         }
         if(A == 2)
         {
            tones[2] = 0;
                buttonState[2] = HIGH;
         }

I try to change that to yours.