CS 110 UHART Arduino And Input From Components Lab

Arduino Labs – Lab 5 v.2University of Hartford CS 110
Lab 5 – Arduino and input from components
Discussion
Input Just as we could send signals to the electrical
components from the Arduino, the Arduino can
receive input from certain components as well. As
we discussed in the last lab, each of the digital pins
must be defined to the Arduino as either input or
output, which we’ll do through the program. Keep in
mind that in order to read some voltage from the
electrical components, the circuit must somehow
actually have a 5V electrical source. Thus, we’ll
generally be using the Arduino to output the voltage
on at least one pin that we’ll read elsewhere in the
circuit on another Arduino pin (as input). You don’t
need to concern yourself with this, but when reading
the Arduino pins, it treats voltages greater than 3
volts as HIGH, and voltages less than 3 volts as
LOW. Because we’re constructing digital (not
analog) circuits, we don’t really care about exactly how much voltage appears around the circuit; that is,
we only care about whether it is LOW/HIGH, which is similar to OFF/ON, or 0/1 in binary.
Buttons If I send some electrical current through a circuit, won’t it come out the other side? The answer
is yes, as long as the circuit is complete (i.e. all the wires are connected properly). There are devices that
we sometimes use to “break” the circuit – the most common of which is called a switch. You see these all
of the time (duh!!! Turn on the lights much?). A standard (not dimmable) lightswitch has two states – on
or off. When it is on, it connects the electrical circuit, and the light gets power, so it turns on. When the
switch is off, the circuit is broken, so the light doesn’t get power, so it doesn’t turn on. We’re going to do
the same thing, but with a different kind of switch, called a pushbutton switch (which we used in Lab 3).
The buttons in our kit are “normally open”, which means that it must be pushed in order to connect the
circuit.
Reading Just as we used the pinMode() function in the last lab to define some of the pins as output,
we’re going to use the same function to define some as input. Also, we used digitalWrite() to write
to the pins (after we defined them as output); now we’re going to use digitalRead() to read from the
pins (after we define them as input). For example, digitalRead(3) looks at Pin 3 and determines
whether it is HIGH or LOW.
Latching Sometimes we want our program to “remember” that the button was pressed, at least until it is
pressed again. This is called “latching”. We’re going to use a variable to remember whether the button
was pushed or not.
Objective
Use Arduino to read input from pushbuttons.
Page 1
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
What you need
1) Arduino software running on computer, with computer connected to Elegoo UNO R3 via USB
cable. Don’t forget to configure the port to which the UNO is connected (Tools Menu →
Port → Choose port) if you didn’t already do this today.
2) Three N.O. pushbuttons
3) Red, green, and blue LEDs.
4) three 220 resistors (red-red-black-black-brown).
5) Protoboard
6) A bunch of wires
Outline
1)
2)
3)
4)
Read one pushbutton
Latching with one pushbutton
Read three pushbuttons, and latch
Challenge: Reset latch after some time
Page 2
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
Directions
1) Read one pushbutton
We’re going to connect one LED (with its resistor) to the Arduino board with a pushbutton such
that the LED will light when the button is pushed; we’ll also run a program that reads whether or
not the button has been pressed.
a) Connect a 220 resistor and the longer lead
(anode) of the blue LED into one 5-hole row on
the protoboard.
b) Connect the shorter lead (cathode) of the red LED
along with one end of a black wire to one of the
25-hole (negatively labeled) rows on the
protoboard.
c) Connect a pushbutton switch across the middle
section of the protoboard.
d) Connect the other end of the resistor along with
one end of a white wire to the 5-hole row on the
protoboard that has one of the pushbutton pins in
it.
e) Connect a red wire to the 5-hole row on the same side of
the protoboard that has the other pushbutton pin. You
must use the pin on the same side of the protoboard for
which we connected to the button’s other pin (from step
d).
f) Connect the other end of the black wire to GND on the
protoboard, the other end of the red wire to pin 2 on the
protoboard, and the other end of the white wire to pin 3
on the protoboard.
g) Create a program in the Arduino editor with the following code:
Page 3
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
Look at each line of the program and from what we’ve already learned, try to understand what
each command is doing.
h) Run the program and look at the Serial Monitor. The Serial Monitor should be displaying
“UP” over and over, and the LED should be off.
i) When you press the button, the text in the Serial Monitor should change to “DOWN” and the
LED should turn on, until you release the button.
2) Latching with one pushbutton
Now we’re going to change the program to “latch” when the push button is pressed once, and
“unlatch” when it is pressed again.
a) In order to track whether the button was pushed (or not) we need a variable to maintain that
state. Add this to the top of the program with the pin declarations.
Page 4
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
b) Change the loop() function as below.
c) Any idea what the exclamation mark means? It means “NOT”. In other words, we use that to
switch the latched variable between true and false.
d) Compile and upload the program, and then while watching the Serial Monitor, press the
button. Does it latch as expected? What’s the issue with the way it works? What you should
notice is that while you’re pressing the button it is cycling back and forth between “Latched”
and “Unlatched” because it loops very quickly.
e) Realistically, people press buttons for more than a fraction of a second (the time it takes to
loop), so we need to find a way to make it realize if the button is still being pressed, or if it’s
being pressed again. Add this declaration with the others at the top of the program.
f) Now update the loop() function as follows:
Page 5
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
g) When you compile and upload the program, watch the Serial Monitor. When you press the
button (for as long as you want) it should only switch between Latched and Unlatched once,
until you press it again.
3) Read three pushbuttons, and latch.
Let’s add three latching pushbuttons, each with their own LED and resistor. In the code, we’ll
refer to the pushbuttons by the LED color associated to them, even though they don’t really leave
the LED on.
a) We only need to provide one source
of power to the circuit, so let’s move
the end of the red wire providing
power to the circuit to one of the
positive-labeled 25-hole rows. Now
add an orange wire that connects that
25-hole row to where we just had the
red wire connected to the pushbutton
(before we moved it).
b) Add the green and blue LEDs, with
their resistors, and other pushbuttons
in the same structure as we did with
the red one. Use pin 4 for the input
from the green LED and pin 5 for the
input from the blue LED. Make sure
you put the shorter lead (cathode) of each
LED in the negative-labeled 25-hole row. I
chose orange wires to connect each
pushbutton to the power source (the positivelabeled 25-hole row). I used a green wire to
connect the pushbutton (for the green LED)
to pin 4, and a blue wire to connect the
pushbutton (for the blue LED) to pin 5.
c) Because we are now going to have multiple
“in” pins, each for a different color LED, we
need to update the code so we can support
the additional colors. In the code, change
everywhere you had latched to
redLatched, everywhere you had
buttonStillPressed to
redButtonStillPressed, and
everywhere you had inPin to redPin.
d) In the declarations section at the top, we need to copy the const int redPin = 3; and
paste it, changing the new ones to be for green (pin 4) and blue (pin 5). Also copy and paste
the declarations for redLatched and redButtonStillPressed, altering for the new
colors.
Page 6
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
e) Now, copy everything from if (digitalRead(redPin) == HIGH) through the
closing brace “}” after redButtonStillPressed = false; and paste it twice more
below that block of code. Change one of those new if-else blocks to be for green, and the
other for blue.
f) In order to make the loop() function a little bit shorter, I spiked out separate functions for
printRedStatus(), printGreenStatus(), and printBlueStatus(), and
then call those functions from the end of the loop() function, replacing the existing if-else
block of code printing out button status. See code below. Notice how the blue one uses
println() instead of print().
Page 7
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
g) Your final program should look like this:
h) When you run it, you should see each color print as unlatched. When you press one of the
buttons, that color should latch, and a second press should unlatch again. Try different
combinations of pressing the buttons and looking at the output in the Serial Monitor.
Page 8
Arduino Labs – Lab 5 v.2
University of Hartford CS 110
4) Challenge: Reset the latch after some time.
Try to update the program to automatically unlatch each color after 5 seconds of being latched
(as an alternative to pressing the button again).
Questions
1) Describe a real-life situation that might need buttons that don’t latch. How about buttons that do
latch?
2) What is the difference between digitalRead() and digitalWrite()?
3) Instead of separating out the printRedStatus(), printGreenStatus(), and
printBlueStatus() functions, how could we include their functionality in the loop()
function?
4) How could you add a fourth LED and button? What else would you need to do?
5) Do you think we could keep the LEDs on after latching without changing the circuit, or do we
need to change the circuit? Why?
6) What problems did you encounter with this lab and how did you resolve them?
Page 9

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed with your coursework?
Get quality coursework help from an expert!