int speaker = 9; // choose the pin for the speaker
int inPin = 8; // choose the input pin (for a pushbutton or switch)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(speaker, OUTPUT); // declare the output
pinMode(inPin, INPUT); // declare pushbutton or switch as input
}

void loop(){
val = digitalRead(inPin); // read input value
if (val == LOW) { // check if the input is LOW (button released)
tone(speaker,400); // turn tone ON
} else {
noTone(speaker); // turn tone OFF