/* Make each button on the base of your Transformer play a different note when pressed */
void setup(){
pinMode(6,INPUT_PULLUP);//button
pinMode(7,INPUT_PULLUP);//button
pinMode(A5,OUTPUT); //speaker
}
void loop(){
if(digitalRead(6)==LOW){ //if button 6 is pressed
tone(A5,500); //play a tone of 500 hertz on the speaker
}
else if(digitalRead(7)==LOW){ //otherwise if button 7 is pressed
tone(A5,800); //play a tone of 700 hertz
}
else{ //otherwise if none of the if-else if statements are true
noTone(A5); //stop any tones playing on the speaker
}
}
// (c) 2024 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
It looks like your computer isn't set up to code yet.
Success!
Compile errors:
Serial port monitor:
Input:
Walkthrough Video
Watch the video for a line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!
Challenges
Can you complete the challenges? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!