Careful with the semicolons at the end of the while line. Is that what you want? Wait as long as the pin is high and then issue a sound that will last for 30ms, and go wait for the next button press?
I need me to press the button and there was a sound, but this only works on 4 out of 5 buttons and only once, then I have to restart the program. I do not know how to fix it, but it is very urgent ((((
It may help you to look at your loop function and write out in plain English (or your native language) what you believe each line does. Take into account the hints you've already been given and I'm sure you will find the problem(s).
It should not be necessary to use the noTone() function, since you specify a duration for the tones. The tone() function with duration is non-blocking, it uses a timer to turn off the tone after the proper interval.
What is happening is that, with no buttons pressed, the code executes all the tone() function calls, starting the tone output, but then immediately turns it off with noTone(). When you press a button, it allows the tone immediately prior to the while() to continue for the full 30mS, so you can now hear the tone. The last tone() function call in the loop you will never hear, because it is immediately turned off by the noTone() before the code continues at the start of loop(), so you never hear any output when button A is pressed.
when you press the last button you issue the tone() command so you get a sound going but micro-seconds laters you call noTone() so you won't be able to hear the sound.
the structure of the code is not really good for an arduino piano... you can't press the last button until you have pressed all the other ones...
there are countless examples on line, here are a couple hits
Not urgent enough to consider and respond to my suggestions? I didn't post a specific solution because I don't believe you would learn anything from that. But I am convinced that if you think about what I said, research C, and study the flow of your own program you will be able to see and fix the problem yourself.
#define NOTE_A 262
#define NOTE_B 294
#define NOTE_C 330
#define NOTE_D 392
#define NOTE_E 493
const int SPEAKER = 13;
#define BUT_A 2
#define BUT_B 3
#define BUT_C 4
#define BUT_D 5
#define BUT_E 7
#define BUT_PRESSED HIGH
const byte BUTTONS[] = { BUT_A, BUT_B, BUT_C, BUT_D, BUT_E };
const int NOTES[] = { NOTE_A, NOTE_B, NOTE_C, NOTE_D, NOTE_E };
void setup() {
pinMode(13, OUTPUT);
for (int i = 0; i < sizeof(BUTTONS); i++)
pinMode(BUTTONS[i], INPUT_PULLUP);
}
void loop() {
unsigned int note = 0;
unsigned int divisor = 0;
for (int i = 0; i < sizeof(BUTTONS); i++) {
if (digitalRead(BUTTONS[i]) == BUT_PRESSED) {
note += NOTES[i];
divisor++;
}
}
if (divisor)
note /= divisor;
tone(SPEAKER, note);
}
You might have to change BUT_PRESSED to LOW, can't really see how you wired your buttons. Make sure the buttons are read correctly if it doesn't work (check pin numbers and wiring).
Should also work with multiple button presses.