Bug Hunt! Test your Troubleshooting.

By this point, you've surely made a few mistakes when modifying code. Hopefully, you're keeping track of what's going wrong and getting better at making fixes. This program has more subtle errors- try to find them without the help of the compiler. If you can, you're coming along great!

Four Note Piano with Synced LED Hookup: 1 speaker in pin A5, 1 LED in pin 2, 4 buttons in pins 4,6,9,12. Remember short leg of LED is ground.

Code

/* * Play a tone on a speaker attached to pin A5 and light an LED on * pin 2 when buttons attached to pins 4,6,9,12 are pressed. */ void setup() { pinMode(A5, OUTPUT); //speaker //initialize the buttons as inputs: pinMode(4,INPUT_PULLUP); pinMode(6,INPUT); pinMode(9,INPUT); pinMode(12,INPUT_PULLUP); } void loop() { //if a button is pressed, play the tone (in hertz) and light up the LED. if (digitalRead(4) == LOW) { tone(250, A5); digitalWrite(2, HIGH); } else if (digitalRead(6) == LOW) { tone(A5, 350); digitalWrite(2, HIGH); } else if (digitalRead(9) == LOW) { tone(A5, 450); digitalWrite(2, HIGH); } else if (digitalRead(12) == LOW) { tone(A5, 550); digitalWrite(2, HIGH); } else { digitalWrite(2, LOW); } } // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Looking for answers? Click here to see them.

Videos