Lesson 2:  Play 3 Tones to Speaker

Put two slashes (//) in front of the line noTone(A5); . Now the computer will no longer see this line a code, just a comment.

Result: The tone will never stop. It will play endlessly.

 

Copy all of the code starting with //Send a tone.... to delay(1000); after noTone(A5).

Paste it on the empty line just below void loop();.

Don't move any of the curly braces { }.

Result: The tones will now loop and play over and over forever.

 

Change one of the tone commands to tone(A5,2000); .

Result: The tone will be much higher pitched.

 

Move the speaker to port A0 and Ground on Carrier Board.

How will you change your code to make the program work now?

Result: The tone will no longer play until you replace ‘A5’ with ‘A0’ in every instance in the code.

 

Make all of the delay commands delay(10); .

Result: The tones will play very quickly one after another (100 times faster than with a delay of 1000).

 

Lesson 3: Hold button for LED Light

Add another LED to the code so that the button press turns on both LEDs.

This will require a pinMode() line and two digitalWrite() lines.

Result: When you press the button, both LEDs will come on. To accomplish this, just copy the pinMode() command, press enter at the end of the line of code, and paste it. Then change the port number from 11 to the port you put your new LED in.  Do the same process for both digitalWrite commands.

 

Change the original digitalWrite(11,HIGH); to digitalWrite(11,LOW).

Change the original digitalWrite(11,LOW); to digitalWrite(11,HIGH); .

Result: The light will be on unless the button is pressed. Then it will be off.

 

Add a line delay(2000); to the program after digitalWrite(11,HIGH); .

Result: No matter how long you hold the button, it will stay on for 2 seconds (2000 milliseconds) after you release it.

 

How would you create a program that holds one LED on and another LED off when the button is held?

Result: When the button is held, one LED light is on and the other is off. When the button is released, one LED light is on while the other is off.

To accomplish this, your code will look like:

void setup(){

 pinMode(11, OUTPUT);  //LED pin
 pinMode(12, OUTPUT);  //LED
 pinMode(A5, INPUT_PULLUP); //pushbutton pin    
}


void loop(){

 //if the pushbutton is pressed, hold the LED on

 if (digitalRead(A5) == LOW){
     digitalWrite(11, HIGH);
     digitalWrite(12,LOW); //turn this LED off
     }

 else {
     digitalWrite(11,LOW);   //otherwise, turn LED off
     digitalWrite(12,HIGH); //turn this LED on
 }
}



 

Lesson 4: LED Flashlight

Add:

delay(100);

digitalWrite(12,LOW);

delay(100);

on new lines after digitalWrite(12,HIGH); .
Result: The LED will blink when you press and release the button, rather than stay on.

 

Add another LED to the program so that when you press the button, two lights come on. You'll need pinMode and digitalWrite commands.

Result: To accomplish this, insert a new pinMode() command directly below the existing pinMode() command. Go to the end of the line of code for the existing pinMode() command and press enter. Enter pinMode( and then the number of the port you’re going to put your new LED into, and then OUTPUT).

Inside the curly braces of the  ‘if’ statement, insert a new digitalWrite() command using the pin number you entered in your pinMode command and “HIGH”.

Inside the curly braces of the ‘else’ statement, insert a new digitalWrite() command using the pin number you entered in your pinMode command and “LOW”.

 

Change the name of the variable from pressCount to your own name. There are four places you'll have to update.

Result: The code will work exactly like the default example, but the variable will have a different name. Variables can be named almost anything, but they typically can’t start with numbers or special characters.

 

Move the LED to a new port on Carrier Board. Now update your code so that the program still works as a flashlight.

Result: If you change the pinMode command and both digitalWrite commands, the code will work just like the default example code works.

 

Lesson 5: Cycle LED Colors with Button

"Comment out" the line containing delay(250); by putting two slashes (//) in front of it. Now the computer won't run this line.

Result: The colors will change extremely quickly and the LED may appear white.

 

Add another multi color LED to three new ports on Carrier Board. Try to make the program light up a different color on two LEDs with each press.

Result: Each time you press the button, you’ll have two LEDs on. Each LED will be a different color (and not a mixed color like blue+red for purple).

To accomplish this:

Copy and paste or repeat the three pinMode() commands, but use the ports for your new LED in place of the existing port numbers. There will be 3 new pinMode() commands.

In between the curly braces of the first ‘if’ statement, add a new digitalWrite() command that turns one of the new LED’s port’s ‘HIGH’. On a new line directly below that, digitalWrite() the last pin of your new LED LOW. This is because the code loops, so you must turn off the LED from your final ‘else if’ statement in this first ‘if’ statement.

Repeat this process in each ‘if else’ statement, turning one color of your new LED on and turning the previously ‘HIGH’ port off with two new digitalWrite() commands per ‘else if’ statement.

 

Add more 'else if' statements and mix the colors by using digitalWrite(11,HIGH); and digitalWrite(12,HIGH); at the same time, for example.

Result: You will have multiple ‘else if’ statements that don’t turn off the prior LED port, so the colors will mix, such as blue light and red light appearing purple.

To accomplish this: Press enter after the closing curly brace of the final ‘else if’ statement } . Do not start writing code after the final closing curly brace that ends the loop, or you’ll get an error.

Copy the pattern of ‘else if’ statements that are in the default example code, using a higher ‘pressCount’ for each new condition.

Remember that you have to set pressCount = 0;  only in the final ‘else if’ statement or your variable will constantly be resetting to 0.

 

How would you add another button and another LED so that the new button cycles the colors of the new LED?

Result: You’ll have two buttons and two multicolor LEDs. One button will cycle the colors of one LED and the other button will cycle the colors of the second LED.

To accomplish this: You will need a new variable to ‘count’ with, which you can create by following the pattern of the pressCount variable: int ‘name’ = 0; above the void setup() of the code.

You’ll need new pinMode() commands for each port you’re using: 3 for the LED and one for the button.

You can make an exact copy of all of the code inside the loop ( not a copy of the words void loop(){ ) and then paste it at the bottom of the loop ( but inside the closing curly brace of the loop).

If you copy and paste the structure of the void loop, you’ll change the variable name and the name for each port within the ‘if’ and ‘if else’ statements.

 

Lesson 6: Four Note Piano

Change the tone commands for each button to

(A5,262);

(A5,294);

(A5,330);

(A5,392);

and try to play "Mary had a Little Lamb". You can search the internet for 'notes to hertz' conversions.

Result: The tones will sound like the notes required for this song.

 

Create a variable called "note" to replace the tone value. Then use

tone(A5,note+100);

tone(A5,note+200);

tone(A5,note+300);

to create your own scales.

Write int note=100; above void setup to create the variable.

Result: Your tones will be equal to 200, 300, and 300 hertz.

 

Add delay(1000); on a new line after each tone() command in the program. How does that delay affect your piano?

Result: Each tone will last for 1 second (1000 milliseconds) after you release the button that plays it.

 

Instead of the 'else{ }' statement containing only a noTone() command, insert a series of tone, delay, noTone, delay commands to create a background beat when no buttons are pressed.

Result: By default, your ‘piano’ will play the tones in the ‘else’ statement until a key is pressed. Then the key will trigger the tone related to it.


 

Lesson 7:  Four Note Piano with Synced LED

Add a new LED to the Carrier Board port A0.

Add pinMode(A0,OUTPUT); in the setup.

Change one of the digitalWrite(2,HIGH); commands to digitalWrite(A0,HIGH);

Result: When you press one of the buttons, the LED on port A0 will light instead of the LED on port 2.

 

How many more LEDs can you add?

Result: You can have a different LED light up for each tone, or multiple LEDs light up for each tone, or some combination of the two.

 

After you've added more LEDs, change the else statement so they are all illuminated when no notes are being played.

Result: The LEDs will be on by default if the digitalWrite() commands inside the curly braces of the  ‘else’ statement are “HIGH”.

 

Can you create a multi-note, multi-light show that plays automatically after you press a button to start it?

Result: If you use only one ‘if’ statement and inside of it include tones, digitalWrite() commands, and delays, you can have a light and sound show automatically play when the button is pressed.