/* By pressing the pushbutton, a sound is generated, which is audible from
the speaker
https://github.com/ostad-ai/Arduino-Tutorial
*/
// when the pushbutton is pressed, a square wave is generated by tone()
// function, which is heard from the speaker connected to a digital pin.
const int button_pin=12, speaker_pin=4;
int const pitch=262; // the frequency
void setup() {
// put your setup code here, to run once:
pinMode(button_pin, INPUT_PULLUP);
pinMode(speaker_pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int state=digitalRead(button_pin);
if (state==LOW)
tone(speaker_pin,pitch);
else
noTone(speaker_pin);
}