void loop() {
int potVal = map(analogRead(A2), 0, 1024, 0, 3);
for (int i = 0; i < numberOfButtons; i++) {
checkButton(potVal,i);
}
}
void checkButton(int scaleNum,int buttonNum)
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin[buttonNum]);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin[buttonNum], HIGH);
tone(speaker, notes[scaleNum][buttonNum]); // play the note
delay(100); // wait for 1/10th of a second
} else {
digitalWrite(ledPin[buttonNum], LOW);
noTone(speaker); // stop playing the note
}
}