3_10 Four Note Piano with LEDs

Four Note Code Piano with LEDs

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* Create a 4 note piano where each of the 4 buttons plays a different tone and corresponds to an LED lighting up */ void setup(){ pinMode(4,INPUT_PULLUP);//button pinMode(5,INPUT_PULLUP);//button pinMode(6,INPUT_PULLUP);//button pinMode(7,INPUT_PULLUP);//button pinMode(9,OUTPUT); //one leg of the RGB LED pinMode(10,OUTPUT); //one leg of the RGB LED pinMode(11,OUTPUT); //one leg of the RGB LED pinMode(12,OUTPUT); //single color LED pinMode(2,OUTPUT); //speaker } void loop(){ if(digitalRead(4)==LOW){ //if button 4 is pressed tone(2,500); //play a tone of 500 hertz on the speaker digitalWrite(9,HIGH); //turn on LED color 9 (one of the RGB LED colors) } else if(digitalRead(5)==LOW){ //otherwise if button 5 is pressed tone(2,700); //play a tone of 700 hertz digitalWrite(10,HIGH); //turn on LED color 10 } else if(digitalRead(6)==LOW){ //otherwise if button 6 is pressed tone(2,900); //play a tone of 900 hertz digitalWrite(11,HIGH); //turn on LED color 11 } else if(digitalRead(7)==LOW){ //otherwise if button 7 is pressed tone(2,1100); //play a tone of 1100 hertz digitalWrite(12,HIGH); //turn on LED color 12 } else{ //otherwise if none of the if-else if statements are true noTone(2); //stop any tones playing on the speaker //Turn off all the LED ports digitalWrite(9,LOW); digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_9_1 Bug Hunt: Four Note Piano

Bug Hunt! Test your Troubleshooting.

Prefer listening over reading? Click the play button in the audio bar.

Here’s how Bug Hunts work:

We’ve taken the example code from the last project and inserted errors into it. These errors might be spelling problems, syntax, or we might have changed up how the program works altogether!

The goal of the Bug Hunt is to find and fix the errors so that when you upload the code, it runs just like the example code did on the project page.

Try these steps in order to solve a Bug Hunt:

1) Look over the code closely first, looking for anything that is out of place. We recommend you do this first to build up your ‘bug hunting instincts’.

2) Press ‘Upload the Code’ with your board plugged in and the circuit built. The code won’t work, but it will show you the errors that the computer found - these will pop up in a box just below the code editor. These clues are sometimes vague or confusing, but they may help you find out where the problem is.

3) Check your code references, especially for the functions that have turned red when you hit “Upload”. Compare the syntax of your reference book to the syntax in your code editor here.

After you’ve fixed the bug and the code is working again, hit ‘Restore’ so you can see the code how it was before you fixed it. Is the bug obvious to you now? Reviewing like this will make it easier to find bugs in future programs!

Get stuck? Click on ‘Need a Hint?’ below. It will provide a clue about what’s wrong in this program.

/* Create a 4 note piano where each of the 4 buttons plays a different tone */ void setup(){ pinMode(4,INPUT_PULLUP);//button pinMode(5,INPUT_PULLUP);//button pinMode(6,INPUT_PULLUP);//button pinMode(7,INPUT_PULLUP);//button pinMode(2,OUTPUT); //speaker } void loop(){ if(digitalRead(4)==LOW){ //if button 4 is pressed tone(2,500); //play a tone of 500 hertz on the speaker } else{ //otherwise if none of the if-else if statements are true noTone(2); //stop any tones playing on the speaker } else if(digitalRead(5)==LOW){ //otherwise if button 5 is pressed tone(2,700); //play a tone of 700 hertz } else if(digitalRead(6)==LOW){ //otherwise if button 6 is pressed tone(2,900); //play a tone of 900 hertz } else if(digitalRead(7)==LOW){ //otherwise if button 7 is pressed tone(2,1100); //play a tone of 1100 hertz } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 



When you have lots of if-else if-else statements in your code, it's important to keep them straight, because the code checks each condition from top to bottom. The 'else' statement comes last and that's the code that runs if none of the 'if' or 'else if' conditions are true.

If the if-else if-else statements got out of order, the program might not be able to check all of the conditions in the code! Make sure the 'else' statement is in the right place.

3_9 Four Note Piano

Four Note Code Piano

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* Create a 4 note piano where each of the 4 buttons plays a different tone */ void setup(){ pinMode(4,INPUT_PULLUP);//button pinMode(5,INPUT_PULLUP);//button pinMode(6,INPUT_PULLUP);//button pinMode(7,INPUT_PULLUP);//button pinMode(2,OUTPUT); //speaker } void loop(){ if(digitalRead(4)==LOW){ //if button 4 is pressed tone(2,500); //play a tone of 500 hertz on the speaker } else if(digitalRead(5)==LOW){ //otherwise if button 5 is pressed tone(2,700); //play a tone of 700 hertz } else if(digitalRead(6)==LOW){ //otherwise if button 6 is pressed tone(2,900); //play a tone of 900 hertz } else if(digitalRead(7)==LOW){ //otherwise if button 7 is pressed tone(2,1100); //play a tone of 1100 hertz } else{ //otherwise if none of the if-else if statements are true noTone(2); //stop any tones playing on the speaker } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_8 Turn Knob Digital Motor Switch

Twist the Knob to Buzz the Motor

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


//Use the turn knob to turn on and off the motor void setup(){ pinMode(3,OUTPUT); //Motor pinMode(A0,INPUT); //Turn Knob } void loop(){ if(digitalRead(A0)==HIGH){ //If the turn knob reading is HIGH (knob turned more than halfway on) digitalWrite(3,HIGH); //turn on the motor } else{ digitalWrite(3,LOW); //otherwise, turn off the motor } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_7 Turn Knob Digital Light Switch

Twist the Knob to Light an LED

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


//Use the turn knob to turn on and off an LED light void setup(){ pinMode(4,OUTPUT); //LED pinMode(A0,INPUT); //Turn Knob } void loop(){ if(digitalRead(A0)==HIGH){ //If the turn knob reading is HIGH (knob turned more than half way) digitalWrite(4,HIGH); //turn on the LED light } else{ digitalWrite(4,LOW); //otherwise, turn off the LED } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_6_1 Bug Hunt: RGB Color Mixer

Bug Hunt! Test your Troubleshooting.

Prefer listening over reading? Click the play button in the audio bar.

Here’s how Bug Hunts work:

We’ve taken the example code from the last project and inserted errors into it. These errors might be spelling problems, syntax, or we might have changed up how the program works altogether!

The goal of the Bug Hunt is to find and fix the errors so that when you upload the code, it runs just like the example code did on the project page.

Try these steps in order to solve a Bug Hunt:

1) Look over the code closely first, looking for anything that is out of place. We recommend you do this first to build up your ‘bug hunting instincts’.

2) Press ‘Upload the Code’ with your board plugged in and the circuit built. The code won’t work, but it will show you the errors that the computer found - these will pop up in a box just below the code editor. These clues are sometimes vague or confusing, but they may help you find out where the problem is.

3) Check your code references, especially for the functions that have turned red when you hit “Upload”. Compare the syntax of your reference book to the syntax in your code editor here.

After you’ve fixed the bug and the code is working again, hit ‘Restore’ so you can see the code how it was before you fixed it. Is the bug obvious to you now? Reviewing like this will make it easier to find bugs in future programs!

Get stuck? Click on ‘Need a Hint?’ below. It will provide a clue about what’s wrong in this program.

/* Set up 3 buttons to correspond to each color from the RGB LED Pressing any single button shows one color. Pressing multiple buttons mixes colors! */ void setup() { pinMode(4,INPUT_PULLUP); //button pinMode(5,INPUT_PULLUP); //button pinMode(6,INPUT_PULLUP); //button pinMode(9,OUTPUT); //one leg of the RGB LED pinMode(10,OUTPUT); //one leg of the RGB LED pinMode(11,OUTPUT); //one leg of the RGB LED } void loop() { if(digitalRead(4)==LOW){ //if button 4 is pressed... digitalWrite(9,HIGH); //turn on the LED color 9 } else{ //otherwise, if button 4 is not pressed... digitalWrite(9,LOW); //turn off LED color 9 } if(digitalRead(5)==HIGH){ //if button 5 is pressed... digitalWrite(10,HIGH); //LED color 10 is on } else{ //otherwise.... digitalWrite(10,LOW); //LED color 10 is off } if(digitalRead(6)==LOW){ //if button 6 is pressed... digitalWrite(11,HIGH); //LED color 11 is on } else{ //otherwise.... digitalWrite(11,LOW); //LED color 11 is off } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 



The 'if' statements are checking the reading from the buttons to 'decide' which lights to turn on in this program. Remember that a port set to pinMode 'INPUT_PULLUP' send a LOW signal when pressed and a HIGH signal when not pressed.

Double-check the 'if' statements and make sure the digitalRead functions are checking for the correct signal from the buttons.

3_6 RGB Color Mixer

Mix RGB LED Colors with Buttons

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* Set up 3 buttons to correspond to each color from the RGB LED Pressing any single button shows one color. Pressing multiple buttons mixes colors! */ void setup() { pinMode(4,INPUT_PULLUP); //button pinMode(5,INPUT_PULLUP); //button pinMode(6,INPUT_PULLUP); //button pinMode(9,OUTPUT); //one leg of the RGB LED pinMode(10,OUTPUT); //one leg of the RGB LED pinMode(11,OUTPUT); //one leg of the RGB LED } void loop() { if(digitalRead(4)==LOW){ //if button 4 is pressed... digitalWrite(9,HIGH); //turn on the LED color 9 } else{ //otherwise, if button 4 is not pressed... digitalWrite(9,LOW); //turn off LED color 9 } if(digitalRead(5)==LOW){ //if button 5 is pressed... digitalWrite(10,HIGH); //LED color 10 is on } else{ //otherwise.... digitalWrite(10,LOW); //LED color 10 is off } if(digitalRead(6)==LOW){ //if button 6 is pressed... digitalWrite(11,HIGH); //LED color 11 is on } else{ //otherwise.... digitalWrite(11,LOW); //LED color 11 is off } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_5 Serial Print Button Press

Serial Print when a Button is Pressed

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* "Print" a message on the serial monitor when the button is pressed Don't forget to hit the 'monitor' button on the code editor in the top toolbar to see the serial monitor after you've uploaded your code. */ void setup() { Serial.begin(9600); pinMode(4,INPUT_PULLUP); //button } void loop() { if(digitalRead(4)==LOW){ //if button 4 is pressed... Serial.println("I'm Pressed!"); //print the message on a new line } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_4_1 Bug Hunt: One Note Piano

Bug Hunt! Test your Troubleshooting.

Prefer listening over reading? Click the play button in the audio bar.

Here’s how Bug Hunts work:

We’ve taken the example code from the last project and inserted errors into it. These errors might be spelling problems, syntax, or we might have changed up how the program works altogether!

The goal of the Bug Hunt is to find and fix the errors so that when you upload the code, it runs just like the example code did on the project page.

Try these steps in order to solve a Bug Hunt:

1) Look over the code closely first, looking for anything that is out of place. We recommend you do this first to build up your ‘bug hunting instincts’.

2) Press ‘Upload the Code’ with your board plugged in and the circuit built. The code won’t work, but it will show you the errors that the computer found - these will pop up in a box just below the code editor. These clues are sometimes vague or confusing, but they may help you find out where the problem is.

3) Check your code references, especially for the functions that have turned red when you hit “Upload”. Compare the syntax of your reference book to the syntax in your code editor here.

After you’ve fixed the bug and the code is working again, hit ‘Restore’ so you can see the code how it was before you fixed it. Is the bug obvious to you now? Reviewing like this will make it easier to find bugs in future programs!

Get stuck? Click on ‘Need a Hint?’ below. It will provide a clue about what’s wrong in this program.

/* Buggy:Press a button to play a tone on the speaker */ void setup() { pinMode(4,OUTPUT); //button pinMode(2,OUTPUT); //speaker } void loop() { if(digitalRead(4)==LOW){ //if button 4 is pressed... tone(2,500); //play a tone of 500 hertz on the speaker } else{ //otherwise (if button 4 is not pressed)... noTone(2); //turn off the tone from the speaker } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 



The pinMode function is really important- it makes sure that all of the ports you are using are set up correctly. If a different pinMode (INPUT, OUTPUT, INPUT_PULLUP) was used for one of your components, it could cause the whole program to act strangely!

This bug is also tricky because the computer doesn't see a problem with it - it's technically OK code. But since you know what the program is supposed to do, you'll immediately see (or hear) an issue!

Remember that you can review your code reference book to see the pinMode that each component should use!

3_4 One Note Piano

One Note Piano

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* Press a button to play a tone on the speaker */ void setup() { pinMode(4,INPUT_PULLUP); //button pinMode(2,OUTPUT); //speaker } void loop() { if(digitalRead(4)==LOW){ //if button 4 is pressed... tone(2,500); //play a tone of 500 hertz on the speaker } else{ //otherwise (if button 4 is not pressed)... noTone(2); //turn off the tone from the speaker } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_3 Hold for Motor Buzz

Hold a Button Down to Buzz the Motor

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


//Press a button to turn on Code Lab Mini's motor. Release the button to turn it off. void setup(){ pinMode(3,OUTPUT); //Motor pinMode(5,INPUT_PULLUP); //Button } void loop(){ if(digitalRead(5)==LOW){ //If the button reading is LOW (the button is pressed) digitalWrite(3,HIGH); //turn on the motor } else{ digitalWrite(3,LOW); //otherwise (if the button reading isn't LOW), turn off the motor } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_2 Hold for Light

Hold a Button to Keep the LED Lit

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


//Press a button to turn on an LED light. Release the button to turn off the light void setup(){ pinMode(8,OUTPUT); //LED light pinMode(4,INPUT_PULLUP); //Button } void loop(){ if(digitalRead(4)==LOW){ //If the button reading is LOW (the button is pressed) digitalWrite(8,HIGH); //turn on the LED light } else{ digitalWrite(8,LOW); //otherwise (if the button reading isn't LOW), turn off the LED light } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_1 Press For Sound

Press a Button to Start a Tone

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


//Press a button to play a tone void setup(){ pinMode(2,OUTPUT); //Speaker pinMode(4,INPUT_PULLUP); //Button } void loop(){ if(digitalRead(4)==LOW){ //If the button reading is LOW (the button is pressed) tone(2,600); //Play a tone of 600 hertz } //You have to unplug Code Lab Mini to get the tone to stop! } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!

3_0 Press For Light

Press a Button to Light an LED

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


//Press a button to turn on an LED light void setup(){ pinMode(8,OUTPUT); //LED light pinMode(4,INPUT_PULLUP); //Button } void loop(){ if(digitalRead(4)==LOW){ //If the button reading is LOW (the button is pressed) digitalWrite(8,HIGH); //turn on the LED light } //You have to unplug Code Lab Mini to get the LED to turn off again! } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!