Piano Variable Keys Bug Hunt! Test your Troubleshooting.
This code will run on Code Piano, but something is wrong. Find and fix the sly error in this bug hunt.
/* Create a full range of notes on your piano using all of the buttons
* Use variables to stand in for number values to make your program easier to read
* You can find the conversion of hertz to notes online or on your purple cards
*/
int A = 440; //An A note is equal to 440 hertz. Now 'A' will stand in for 440 in your loop
int B = 494;
int C = 523;
int D = 587;
int E = 659;
int F = 99;
int G = 784;
int highA = 880;
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(){
if(digitalRead(2)==LOW){ //if button 2 is sending a low signal...
tone(10,A); //...play a tone of 500 hertz on the speaker
}
else if(digitalRead(3)==LOW){ //if button 3 is sending a low signal...
tone(10,B); //...play a tone of 600 hertz on the speaker
}
else if(digitalRead(4)==LOW){
tone(10,C);
}
else if(digitalRead(5)==LOW){
tone(10,D);
}
else if(digitalRead(6)==LOW){
tone(10,E);
}
else if(digitalRead(7)==LOW){
tone(10,F);
}
else if(digitalRead(8)==LOW){
tone(10,G);
}
else if(digitalRead(9)==LOW){
tone(10,highA);
}
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:
Need a hint? Click here.
Each of your notes in the void loop is referencing a variable value created above the void setup. One of your note variables has an incorrect starting value.
Think about how you 'hunted down' this bug. Did you scan the code and look for something out of place? Did you run the code and find that something was wrong through your exprience, then trace it back to the source? There are many right ways to bug hunt!