// This program plays a tone when a button is pressed
/* define nicknames or aliases for things and values for constants */
#include "pitches.h" // library of tones and their frequencies
#define SPEAKER_PIN 8 // defines which pin of Arduino connected to the loud speaker
#define BUTTON_PIN 6 // defines which pin of Arduino is connected to the push button
void setup() {
//Tell Arduino how each pin should work, as an output or input
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() { //the program code below repeats endlessly
if (digitalRead( BUTTON_PIN) == LOW) {
tone(SPEAKER_PIN, NOTE_C7);
digitalWrite(LED_BUILTIN, HIGH);
}
else{
digitalWrite(LED_BUILTIN, LOW);
noTone(SPEAKER_PIN);
}
}