Using the Arduino - Fairfield County Makers Guild

advertisement
Using the Arduino
Lab 3 – Digital Input
Fairfield County Makers' Guild
John Brucker - 2013
Lab Summary
Part 1 - How to hook up a button with a pull-up and a
pull-down resistor and shows how to read the value in
code. If you don't understand how it works that's okay.
Part 2 – Introduces true/false (Boolean) logic and uses
our knowledge of buttons and LEDs to demonstrate
boolean operations. If you get through this, excellent job.
Part 3 – Bonus Problem
Part 1a – Problem Statement
Wire up a button which will then be used to control the
built-in LED on pin 13.
There are many ways to wire and code a solution to the
problem. However, for this part an example board and
schematic with sample code is provided.
It uses what is called a pull-down resistor. Which means
that when the button isn't pressed the Arduino sees 0V
on the pin connected to the button.
Part 1a – Example Board + Schematic
GND
Resistor
10K Ω
Pin 10
Button
+5V
Part 1a – Example Code
const int LED_PIN = 13;
const int BUTTON_PIN = 10;
// LED is on pin 13
// Button is on pin 10
void setup() {
pinMode(LED_PIN, OUTPUT);
// Set pin 13 as an OUTPUT
pinMode(BUTTON_PIN, INPUT); // Set pin 10 as an INPUT
}
void loop() {
int button_state =
digitalRead(BUTTON_PIN);
digitalWrite(LED_PIN,
button_state);
}
// Store the current value
//
that is on pin 10
// Set the value of pin 13
//
to the stored value
Part 1b – Problem Statement
Remove the resistor from Part 1a. What happens?
If the resistor is removed you should notice that the LED
doesn't always stay off when the button isn't pressed.
This is because when the button is pressed the pin sees
5V thus still working properly.
However, when the button is released the pin is no
longer connected to an active voltage, and “floats” at an
unknown and uncontrolled voltage.
Part 1c – Problem Statement
Using the example board and schematic wire up the
board to use a pull-up resistor.
Now when the button isn't pressed the Arduino sees 5V
on the pin connected to the button. Which means that
when the button is pressed we see 0V.
Part 1c – Example Board + Schematic
GND
Button
Pin 10
Resistor
10K Ω
+5V
Part 1d – Problem Statement
Remove the resistor from part 1b. We can also modify
the code to use the INPUT_PULLUP mode in the pinMode
function. It enables an internal resistor for use so we
don't have to wire up a resistor with the button.
We only need to change one line.
From: pinMode(BUTTON_PIN, INPUT);
To:
pinMode(BUTTON_PIN, INPUT_PULLUP);
Part 2 – Problem Statement
Wire up eight LEDs using pins 2 through 9 and two
buttons to pins 10 and 11. Code is provided
demonstrating boolean operations.
After building up the circuit and transferring the code,
play around with the buttons and see if you can
understand what the different operations are doing.
A truth table showing the logic of the boolean operations
is provided.
Part 2 – Schematic
330 Ω
Don't be scared. You can do
this.
It is the single LED from
Lesson 1 in the book copied
8 times. Then adding one
more button from Part 1.
10K Ω
Dots indicate connections
between wires.
Part 2 – Boolean Truth Table
a
b
true
true
true
false
!a
!b
false false
false false
true
a==b
a!=b
a&&b
a||b
true
false
true
true
false
true
false
true
false
true
true
true
false false
true
false false
true
true
false false false
true
Part 2 – Code (1/3)
// Setting up pretty names for each of the pins.
const int LED_A
= 2; // LED for button A is on pin
const int LED_B
= 3; // LED for button B is on pin
const int LED_NOT_A
= 4; // LED for not button A is on
const int LED_NOT_B
= 5; // LED for not button B is on
const int LED_EQUAL
= 6; // LED for == is on pin 6
const int LED_NOT_EQUAL = 7; // LED for != is on pin 7
const int LED_AND
= 8; // LED for && is on pin 8
const int LED_OR
= 9; // LED for || is on pin 9
const int BUTTON_A
= 10; // Button A is on pin 10
const int BUTTON_B
= 11; // Button B is on pin 11
2
3
pin 4
pin 5
Part 2 – Code (2/3)
// Setup the pins to the
void setup() {
pinMode(LED_A,
pinMode(LED_B,
pinMode(LED_NOT_A,
pinMode(LED_NOT_B,
pinMode(LED_EQUAL,
pinMode(LED_NOT_EQUAL,
pinMode(LED_AND,
pinMode(LED_OR,
pinMode(BUTTON_A,
pinMode(BUTTON_B,
}
appropriate INPUT/OUTPUT mode
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
INPUT);
INPUT);
//
//
//
//
//
//
//
//
//
//
Set
Set
Set
Set
Set
Set
Set
Set
Set
Set
pin
pin
pin
pin
pin
pin
pin
pin
pin
pin
2 as an OUTPUT
3 as an OUTPUT
4 as an OUTPUT
5 as an OUTPUT
6 as an OUTPUT
7 as an OUTPUT
8 as an OUTPUT
9 as an OUTPUT
10 as an INPUT
11 as an INPUT
Part 2 – Code (3/3)
void loop() {
// Read and store the HIGH/LOW button pin values
int a = digitalRead(BUTTON_A);
// store value from pin 10
int b = digitalRead(BUTTON_B);
// store value from pin 11
// Remember in computers everything is a number.
//
HIGH/true is 1
//
LOW/false is 0
// So we can use HIGH/LOW values from digitalRead with boolean
//
operations to control the LEDs with digitalWrite
digitalWrite(LED_A,
a);
// pin 2 to value of a
digitalWrite(LED_B,
b);
// pin 3 to value of b
digitalWrite(LED_NOT_A,
!a);
// pin 4 to opposite of a
digitalWrite(LED_NOT_B,
!b);
// pin 5 to opposite of b
digitalWrite(LED_EQUAL,
a==b); // pin 6 if a equals b
digitalWrite(LED_NOT_EQUAL, a!=b); // pin 7 if a does not equal b
digitalWrite(LED_AND,
a&&b); // pin 8 if a and b
digitalWrite(LED_OR,
a||b); // pin 9 if either a or b
}
Part 3 – Problem Statement
Try writing some code that will light the LEDs from Part 2.
When 1 button is pressed increase the number of LEDs
lit, and when the other is pressed decrease the number
of LEDs lit.
Download