What are Variables?
Variables are a way for us to store information in our program that we can change later. There are three parts to a variable: a type, a name, and a value. For example, I could store a variable in my program called “myFavoriteNumber” like this:
int myFavoriteNumber = 8;
After I have declared myFavoriteNumber
, I can use it anywhere in my program. And if my favorite number changes later, I can change my variable to reflect that:
myFavoriteNumber = 21;
About Variable Types
Notice that I only have to tell the compiler what type of variable myFavoriteNumber is when I first declare it. If I change the value later, I don’t have to include the type; the compiler will remember it automatically. myFavoriteNumber
is an int
type variable, which means that it can only be a whole number. If my favorite number was 3.14, this type wouldn’t work! Some other common variable types include double
(for decimal numbers), bool
(for true
or false
values), and char
(for single characters like the letter 'a'
).
Examples
Let’s add a few variables to our code. In your variables_activity.cpp
file, in the variables_exploration
function definition, add the following lines of code:
int currentLed = 1;
int currentNote = NOTE_C4;
int duration = 1000;
More Details
Hopefully it’s obvious what our variables represent based on their names. Notice that we can set our variables equal to other variables! currentNote
has the same value as NOTE_C4
. If we add speaker_play_note(currentNote, duration)
to our code, it’s the same as adding speaker_play_note(NOTE_C4, 1000)
. Middle C will be played for one second.
We’ve now made three variables. Let’s use them to do something interesting. First, let’s light up our current LED. Add the following to your code, and upload it to your board:
Yboard.set_led_color(currentLed, 255, 0, 0);
currentLed++;
Yboard.set_led_color(currentLed, 255, 0, 0);
currentLed++;
Yboard.set_led_color(currentLed, 255, 0, 0);
currentLed++;
Yboard.set_led_color(currentLed, 255, 0, 0);
More Details
As you probably guessed by the lights on your board turning on, adding ++
to our variable name (with a semicolon at the end) increments the number our variable represents by 1. This is exactly the same as if we had written currentLed = currentLed + 1
. It’s nice that we don’t have to hard-code the LED numbers into our code to tell the compiler that we want to light them up. It makes our code a little easier to read.
Exploration
Remember to change main.cpp
before continuing…
📝 NOTE: You will need to go to
main.cpp
and change the comments to call the correct activity function:// sound_activity(); variables_activity();
-
Divide
currentNote
by 2 using the following code snippet:currentNote = currentNote / 2;
Play a sound again. How does our new sound compare to our old one? - See what happens if you multiply or divide
currentNote
by powers of 2 (2, 4, 8, 16) and use it to play a sound. For example, how does the sound compare to our old ones if we writecurrentNote = currentNote * 8;
?Hint
When we divide and multiply the frequency of notes by powers of 2, we get “octaves”. For Middle C, this means we get higher or lower C notes.
- Light up three additional LEDs in the same way we did above.
Challenges
Remember to comment/uncomment the correct function calls…
Remember to comment out the variables_exploration();
call in the variables_activity
function and uncomment the correct challenge function:
variables_exploration();
// variables_challenge1();
// variables_challenge2();
Challenge 1: Light up all the LEDs on the board using the pattern above, then turn them all off again.
Hint
We can use currentNote--;
to decrement our variable by 1. This is the same as writing currentNote = currentNote - 1;
Challenge 2: Use the currentNote
and duration
variables to play notes with random sound lengths and frequencies
Hint
You can use any mathematical expression you like and set your variable equal to it. For example: duration = ((8 * 7) / 3) * 10;
or currentNote = 2 * 2 * 2 + 1034;
IMPORTANT: For your variables_challenge1
and variables_challenge2
functions, you will need to redeclare your variables. Variables live inside functions. If we are in a new function, the variable doesn’t exist any more. Can you think of any other variables you might want to make?