Objectives and Overview
This lesson includes a project where you’ll practice your new skills with variables. You’ll revisit the Blink sketch and use variables instead of hard-coded values for the LED pins.
- Use variables to replace hard-coded values in the Blink sketch.
- Understand how variables can make your code more flexible.
Practicing with Variables
Now that you’ve been introduced to variables it’s time to begin implementing them in your code. Variables provide a degree of flexibility and control that will completely level up your programming skills!
After completing this activity you’ll be expected to use variables in all of your Arduino projects (where appropriate).
Variables make your code much more flexible. The purpose of this quick activity is to practice integrating variables into a familiar project before you dive into a completely new concept.
- The same setup and supplies from the Breadboard Blink activity.
- If you haven’t already, go ahead and rewire this circuit and make sure that your sketch is uploaded to the board.
- Create a new sketch and name it “Blink-Variables” or something similar.
- Copy the code from your previous sketch and paste it into the new sketch. You’re going to be altering it but there’s no need to completely rewrite it.
- Using the examples in this lesson, create a
- Make sure to use the correct datatype for this!
- Make sure to make it global
- Replace any of the “hard-coded” pin number entries with your new variable.
- If you have multiple LEDs, you’ll need multiple, uniquely named variables!
- Test your code and debug any errors.