Try this and check the differences with what you have:
#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 put the delay in there, but you don't necessarily need it. Without it, the tone1.play() method will just be called really often when the button is pressed.
(Note: I've made some coding style differences - these are just my own personal preferences.)
b