Digital Sandbox Experiment Guide
6: Number Storage with Variables
The herky-jerky fading from experiment four accomplished the task, but just think of all the values we were missing! How do we make the LED fade smoothly? You can whip out 256 minutely different Set analog pin blocks, or you can reduce it to one, using variables.
Variables are like storage containers for numbers. We can put any number in a variable, and either recall it and put it to use, or manipulate it to change the value it stores. Anywhere you stick a literal number (like "0" or "255") you can instead use a variable.
There are a few rules when it comes to creating a variable. They can be any word, but they must begin with a letter, and they can't have spaces (use "_" instead). They are case sensitive, so a variable named "fade" isn't the same variable as "Fade." Try to keep variables short, but use descriptive words to keep your code legible.
Thanks to variables, here are all the blocks we need to create a smooth fade:
There are a few new blocks to familiarize yourself with this time:
- Number Variable Name: These blocks are about the same size and shape as the literal number blocks we've been using. But, instead of writing a number in these blocks, you type in the name for your variable (make sure it's spelled the same in every place you want to reference it). You can find this block under the Variables/Constants bin on the left.
- Set Number Variable or Set Integer Variable: This block, also found under the Variables/Constants bin, is used to set a variable to a specific value. Two blocks snap to this one -- a variable name on top, and the value you want to set that variable to on the bottom. The value can be a literal number, another variable, or the result of a mathematical operator.
- Math operator block: If you click on the Math Operators bin, and look at the first four entries, you should see some very familiar symbols: +, −, ×, and ÷. These math operators can be used to do math on a pair of variables or numbers, or a combination of the two.
Add the first Set Variable Number block, which will include a blank variable and a value. Click into the number variable name and write "fade" into it. The "fade" variable will keep track of the the brightness of our LED. The Set Variable Number block in the setup area of the program should set the "fade" variable to zero.
You should be plenty familiar with Set Analog Pin and Delay Milliseconds; grab those blocks and stick them in the loop in either order.
We'll need to throw away the value block that comes with Set Analog Pin (drag it over to the left side of the window) and replace it with a variable. To add a variable to your sketch, drag over the Number Variable Name block and type your variable's name into it. Alternatively, once you've made one variable, you can right-click it and clone it to get more of the "fade" variables you'll need.
Finally, add another Set Number Variable block, and replace the 0 value it includes with a + operator. Modify it so it adds a 1 to "fade," and plug it into the value part of the Set Number Variable block. Then stick that block group at the end of the loop.
Whew! Let's see what all that work was for by uploading the drawing. The LED on pin 5 should satisfyingly and smoothly flow from fully off to fully on.
- Does it matter what order you have the loop blocks in?
- Can you make other LEDs fade? How about more than one fading at the same time?
- Can you make the LED fade from HIGH to LOW? (Hint: You may need to change the setup value of "fade," and change the + to a −.)