Two Key Piano Bug Hunt! Test your Troubleshooting.
This code will run, but it has some unexpected behavior. Can you find the error without using the hint?
//Buggy: Beep a tone when you press button 2 on Code Piano, and a different tone for button 3
void setup(){
pinMode(10,OUTPUT); //Set speaker as an OUTPUT
pinMode(2,OUTPUT); //Button 2 as an INPUT_PULLUP
pinMode(3,INPUT_PULLUP); //Button 3 as an INPUT_PULLUP
}
void loop(){
if(digitalRead(2)==LOW){ //if button 2 is sending a low signal...
tone(10,500); //...play a tone of 500 hertz on the speaker
}
else if(digitalRead(3)==LOW){ //if button 3 is sending a low signal...
tone(10,600); //...play a tone of 600 hertz on the speaker
}
else{ //in all other cases (button is not sending a low signal)...
noTone(10); //send a noTone command to the speaker
}
}
// (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
Heads up! You need our Chrome App to run this code.
Success!
Compile errors:
Serial port monitor:
Input:
Need a hint? Click here.
The pinMode command is very important to how your code works. It sets up each component on the Code Piano to either 'listen for' signals or send signals. If you weren't 'listening for' a signal on one of your buttons, it wouldn't be able to activate when it was pressed. In fact, it might be activated all the time!
Check your pinMode commands in the void setup.