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.