Eight Key Piano Bug Hunt! Test your Troubleshooting.
This code won’t run, but it’s so close to being perfect. Can you find the error without using the hint?
//Create a full range of notes on your piano using all of the buttons
void setup(){
pinMode(10,OUTPUT); //Set speaker as an OUTPUT
pinMode(2,INPUT_PULLUP); //Button 2 as an INPUT_PULLUP
pinMode(3,INPUT_PULLUP); //Button 3 as an INPUT_PULLUP
pinMode(4,INPUT_PULLUP); //Button 4 as an INPUT_PULLUP
pinMode(5,INPUT_PULLUP); //Button 5 as an INPUT_PULLUP
pinMode(6,INPUT_PULLUP); //Button 6 as an INPUT_PULLUP
pinMode(7,INPUT_PULLUP); //Button 7 as an INPUT_PULLUP
pinMode(8,INPUT_PULLUP); //Button 8 as an INPUT_PULLUP
pinMode(9,INPUT_PULLUP); //Button 9 as an INPUT_PULLUP
}
void loop(){
else 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 if(digitalRead(4)==LOW){ //if button 4 is sending a low signal...
tone(10,700); //...play a tone of 700 hertz on the speaker
}
else if(digitalRead(5)==LOW){ //if button 5 is sending a low signal...
tone(10,800); //...play a tone of 800 hertz on the speaker
}
else if(digitalRead(6)==LOW){ //if button 6 is sending a low signal...
tone(10,900); //...play a tone of 900 hertz on the speaker
}
else if(digitalRead(7)==LOW){ //if button 7 is sending a low signal...
tone(10,1000); //...play a tone of 1000 hertz on the speaker
}
else if(digitalRead(8)==LOW){ //if button 8 is sending a low signal...
tone(10,1100); //...play a tone of 1100 hertz on the speaker
}
else if(digitalRead(9)==LOW){ //if button 9 is sending a low signal...
tone(10,1200); //...play a tone of 1200 hertz on the speaker
}
else{ //in all other cases (buttons 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: