Homework 3: A blinking machine for PitTraCo
due at the start of class 4, September 6th
review from today
In class we learned about how to use the analogRead()
function to read a potentiometer’s position. To review: we initialize the potentiometer pin by running this line inside the setup
:
pinMode(A0, INPUT);
We can later perform an analogRead()
of that pin at any time we’d like to find its current value. This could look like this inside the loop
:
int potVal; // create a variable with a name that's short for 'potentiometer value'
potVal = analogRead(A0); // perform an analogRead on pin A0 and then save that value into the variable potVal
In the above case, the variable potVal
will contain the most recently found value, so we should expect it to change all the time.
what you can expect from analogRead()
We did not mention this in class, but the way the analogRead()
function runs on our Uno R3 hardware, we are guaranteed it will always return a value between 0 and 1,023, inclusive. This number is a linear approximation of the voltage present on the pin you are measuring. To be clear:
voltage present at a pin ÷ 5V =
analogRead()
of that pin ÷ 1,023
A few common values to illustrate the above equation:
volts | analogRead() value |
---|---|
0 | 0 |
1 | 205 |
2.5 | 512 |
5 | 1023 |
Your two-part work assignment from the Pittsburgh Train Company
The Pittsburgh Train Company (PitTraCo) needs a new light-blinking system, and they think you’re the right person for the job. They use signal lights to communicate with the train engineers, and want to replace the 80 year old system they’ve been using with new electronically controlled hardware.
First, you’ll build a prototype system, just to prove to them that you are able to use a potentiometer to change the rate at which some lights blink:
Part 1
Input: one potentiometer.
Output: two LEDs.
Behavior: the two LEDs alternate blinking at a steady pace at all times; adjusting the position of the potentiometer changes the rate at which they blink: 🔴🔴
Once you show them that prototype, they’ll definitely be impressed. Then, you’ll need to make it a little better: you’ll have to upgrade it to a system that can change blinking modes, so they can are able to send two different kinds of signals to the train engineers. You need to keep the same hardware in place for both of these systems! You can’t switch any wires in your demo to change systems—only upload new code.
Part 2
Input: two potentiometers.
Output: two LEDs.
Behavior: potentiometer #1 acts like a “mode switch,” and potentiometer #2 acts to change the rate of the LEDs blinking.
When potentiometer #1 is turned all the way clockwise, the hardware behaves exactly as described in sketch #1 above, including that potentiometer #2 changes the rate of blinking: 🔴🔴
However, when the “mode switch” potentiometer is turned all the way counterclockwise, the two LEDs blink both on at the same time, both off at the same time, instead of alternating: 🔴🔴 (Potentiometer #2 can still be used to change the rate of blinking.)
Hints please!
Hint #1: make some good variables, with good names, to help keep track of those potentiometers. Perhaps something like:
int pot1Val;
int pot2Val;
pot1Val = analogRead(A0);
pot2Val = analogRead(A1);
Hint #2: how do you check to see if a potentiometer is in a particular position? By running an if
test on its latest observed value. For instance, after running the code above:
if (pot1Val == 0){
// code in between these curly brackets will run only if pot1Val is equal to 0
}
else {
// code in between these curly brackets will run only if pot1Val is *not* equal to 0
}
Perhaps you’re not familiar with this whole if
thing? In that case, head over to the Arduino language reference page for if
and do some reading. Understanding how to use if
will open huge new vistas for you in CS!
Hint #3: how can you change the rate of blinking? Well, remember how we put a number into our delay()
statements, like delay(1000)
or delay(20)
, etc.? Instead of putting a number into the parentheses, you can put a variable in there—and whatever value the variable has, will be the length of the delay in milliseconds.
Collaboration and help
Just the same as Homework #2: You are welcome to collaborate with your classmates, friends, etc., on this homework assignment. Look up help in any source that you find useful, including the web, reference books, whatever you’d like to use.
However:
-
You must type out the code you use. That means that while you can copy parts of it from another source, you have to type every single letter yourself! We’re building muscle memory. (If you work with a partner, you can both have identical code, but you must both type it out yourself.)
-
You must build your own circuit. You can make the same circuit your friend does, but you have to actually use your own hands to make yours.
Questions? Concerns? Email the instructor or TA and ask! We’re here to help.