#include <Tone.h>
#define SPEAKER 9
#define BUTTON 4
Tone tone1;
int buttonState = 0; //store value of button pin
void setup()
{
pinMode(BUTTON, INPUT);
tone1.begin(SPEAKER);
}
void loop()
{
buttonState = digitalRead(BUTTON);
if (buttonState == HIGH)
{
// play a tone, then wait for a bit before checking the button again
tone1.play(NOTE_A4);
delay(500); // 500 = 500 ms or 1/2 second
tone1.stop();
}
}
I unloaded this code to kit. the result is tone keeps playing= =
SWITCH is attached from pin4 to 5V, and speaker is attached pin9 to 5V.