Arduino For Beginners, Chapter 3
Download
Report
Transcript Arduino For Beginners, Chapter 3
Arduino Training For You!
Learn Programming at Your Own Pace!
A course developed from
the text book:
“Introduction to Arduino
A Piece of Cake!”
By Alan G. Smith
Your Instructor is:
Richard T. Vannoy II
[email protected]
Chapter 1
www.TeachElectronics.com
Copyright 2016, Richard T. Vannoy II
Text/Author Information
• Text Book used in this course:
– Title: Introduction to Arduino: A piece of Cake!
– Author: Alan G. Smith
– ISBN: 1463698348
– ISBN-13: 978-1463698348
– Email: [email protected]
– Purchase text: www.amazon.com
– Download the free PDF: www.introtoarduino.com
Instructor Information
• This PowerPoint presentation
– Instructor: Richard T. Vannoy II, M.S.I.T., B.S.E.E.T.
– Email: [email protected]
– Web Site: www.TeachElectronics.com
Instructor’s Note:
Great PowerPoint presentations are supposed to have few words
and lots of white space. A typical presentation is meant to
augment the presenter’s dialog, not contain all of the
presentation material.
I violate this rule here by maximizing the amount of information
on each slide. This allows any student who cannot attend my
presentation to view the slide show, and learn as much as he
could with me presenting.
Chapter 3: Input
Learning Objectives
• At the completion of this lesson, the student will
be able to:
– Explain how “if” is used to ask True/False questions.
– Explain how =, +, -, *, /, % are used.
– List the six Comparison Operators.
– Explain how to use the Comparison Operators to ask
True/False questions.
Schematic
symbol for:
Battery
or
DC Power
Source
Positive
Terminal
Negative
Terminal
Switch
Arm up = Open =
No current flow =
LED off
Arm down =
Closed = current
flowing = LED on
Resistor
Without the
two arrows,
this would be
the symbol for
a diode.
The arrows
mean it is a
Light Emitting
Diode (LED).
The sketch in the
next few slides will
use a switch to turn
the LED on and off.
The Push Button will be the input
and it will be hooked up to Pin 2.
The LED will be the output and the
LED will be hooked up to Pin 9.
Set Pin9 as the INPUT Pin for the Push Button.
Setting the Pin as an OUTPUT Pin DOES NOT
CONNECT the internal Pull-Up resistor on that
Pin!
But, sending a HIGH to an INPUT Pin tells the
Uno to engage the Pull-Up resistor for that Pin.
This command does that.
Set Pin 9 as the OUTPIN Pin for the LED.
“if” says “Here comes a True/False question.”
1. Read Pin 2. (Input from the switch). It will
either be a 1 (5 Volts) or a 0 (0 Volts).
2. Compare the Pin 2 reading with “LOW” (0).
3. Return a True if they are equal. Return a
False if they are not.
If the answer to the True/False question is
True: Do this. (Turn the LED on.)
If the answer to the True/False question is
False: Do this. (Turn the LED off.)
Summary:
This sketch uses a
switch to turn an
LED on or off.
What is Pulse Width Modulation (PWM)?
What is Pulse Width Modulation (PWM)?
The previous circuit
turned an LED on and off.
This is useful, but there
are many applications
when making an LED
dimmer or brighter is
useful. The next slides will
explain PWM.
What is Pulse Width Modulation (PWM)?
If we put a constant zero
volts to an LED it will
remain off.
What is Pulse Width Modulation (PWM)?
If we put a constant zero
volts to an LED it will
remain off.
If we put a constant 5
volts to an LED it will
remain on.
What is Pulse Width Modulation (PWM)?
If we put a constant zero
volts to an LED it will
remain off.
Something really
interesting happens if we
rapidly change the
voltage from 0 to 5 volts
and back to zero volts!
If we put a constant 5
volts to an LED it will
remain on.
What is Pulse Width Modulation (PWM)?
If we turn the 5 volts on
and off at a rate faster
than the human eye can
detect (say 100 times per
second), the LED appears
to be half-way between
being off and being
brightly lit.
What is Pulse Width Modulation (PWM)?
If we adjust the on-off
time so that the 5 volts
appears LESS THAN 50%
of the time, the LED
appears to get dimmer.
What is Pulse Width Modulation (PWM)?
If we adjust the on-off
time so that the 5 volts
appears MORE THAN 50%
of the time, the LED
appears to get brighter.
The next
program is
going to use one
pushbutton to
make the LED
brighter and the
other to make it
dimmer.
Set up two pushbuttons. One on Pin 2, and the other on Pin 3.
And designate Pin 9 for the LED.
By now, you should be able to describe what is going on here.
We will be adjusting the led between off (0 at the OUTPUT Pin)
and on (255 at the OUTPUT Pin). So we might as well start off
with the LED brightness right in the middle (128).
If button 1 is pressed, increase the brightness of the LED by 1.
“ledBrightness++” uses the increment operator (++) to add
one to the variable named.
If button 2 is pressed, decrease the brightness of the LED by
1.
“ledBrightness--” uses the decrement operator (--) to subtract
one from the variable named.
Notice that if neither button is pressed, NOTHING HAPPENS.
Both changes in brightness are only executed if the
statements above them are True.
This is an important statement!
We never want the “brightness value” to be less than zero, or
higher than 255. BUT - - If we hold down button 1, the
brightness number will eventually get to zero, then continue
by going to -1, -2, -3, etc. and we don’t want that.
The “constrain()” function works like this…
1. Go get the number that is in the memory location called
ledBrightness.
The “constrain()” function works like this…
1. Go get the number that is in the memory location called
ledBrightness.
2. If it is less than zero, change the value to zero.
The “constrain()” function works like this…
1. Go get the number that is in the memory location called
ledBrightness.
2. If it is less than zero, change the value to zero.
3. If it is greater than 255, change it to 255.
The “constrain()” function works like this…
1. Go get the number that is in the memory location called
ledBrightness.
2. If it is less than zero, change the value to zero.
3. If it is greater than 255, change it to 255.
This guarantees that the value remains between 0 and 255.
Take the value in ledBrightness and write it to the LED Pin.
If this delay was not here, the computer would go round-and-round
this loop SO FAST that it would appear that the LED goes instantly
bright or instantly off.
“constrain()” is something called a “Built-In” function, meaning that the
programmers who wrote the code for the Arduino IDE decided to write it
for you and save you some coding time.
Without this function, you would have to write some code like the
example below to keep ledBrightness between 0 and 255.
This is a good time to explain how functions work.
This is a good time to explain how functions work.
1. The “int” on the left says: “When I finish doing what I was asked to
do, I will send back (return) an integer to the calling party. (Calling
party = the program of function that asked me to do this task.)
2. Several alphabetic characters followed by a set of parentheses
identifies this as a function.
This is a good time to explain how functions work.
1. The “int” on the left says: “When I finish doing what I was asked to
do, I will send back (return) an integer to the calling party. (Calling
party = the program of function that asked me to do this task.)
2. Several alphabetic characters followed by a set of parentheses
identifies this as a function.
3. My name is “constrain”.
In order to do my job, I need three things from the calling party:
1. A number that I will check to see if it is within the constraints.
In order to do my job, I need three things from the calling party:
1. A number that I will check to see if it is within the constraints.
2. A number that represents the lowest number allowed right now.
In order to do my job, I need three things from the calling party:
1. A number that I will check to see if it is within the constraints.
2. A number that represents the lowest number allowed right now.
3. A number that represents the biggest number allowed right now.
In order to do my job, I need three things from the calling party:
1. A number that I will check to see if it is within the constraints.
2. A number that represents the lowest number allowed right now.
3. A number that represents the biggest number allowed right now.
4. And since I must find a place to store any numbers I use, and I know
the numbers sent are all integers, I will declare these three things as
integers.
The calling party that desires to use “constrain()” needs to create a
“Function Call” to properly send the information needed to constrain,
and must have a place to store the result. Here is that function call as it
might appear in loop():
value = constrain(ledBrightness, 0, 255)
The next sketch will
use a potentiometer
(also called a variable
resistor) to smoothly
vary the brightness of
the LED.
Let’s take these two lines apart.
Read the analog value from the input pin connected to the potentiometer.
Since it is an analog pin, we know the value must be from 0 to 1023, the
ten bit value of the voltage on that pin. 0 = 0 Volts, 1023 = 5 Volts.
Memory contents:
sensorValue = 512
Let’s say the reading is 2.5 Volts which translates to 512 on the 0-1023
scale.
The 512 is assigned to the memory location called sensorValue.
Memory contents:
sensorValue = 512
Map the sensorValue of 512.
Memory contents:
sensorValue = 512
Map the sensorValue of 512.
We are expecting a value from 0 to 1023.
Memory contents:
sensorValue = 512
Map the sensorValue of 512.
We are expecting a value from 0 to 1023.
Convert that number (512) to its proportional equivalent to the scale of
numbers from 0 to 255.
Memory contents:
sensorValue = 512
Since our number (512) is exactly half-way between 0 and 1023, the
“Answer” needs to be half-way between 0 and 255. That number is 127, so
we assign the value 127 to the ledBrightness.
What does this do????
We read a value (0-1023) from
the input potentiometer.
Instead of using the input to
control the LED brightness, we
use it to delay how long the
LED stays lit!
So, the LED
stays lit
somewhere
between 0
milliseconds
(no time at
all), to 1023
milliseconds
(a little more
than one
second.)
We have a potentiometer
hooked up to Analog Pin 0 (A0).
And an LED is hooked up
to digital Pin 9.
Tell Arduino to set
Pin 9 as an
OUTPUT Pin.
(A0 is an INPUT
Pin by default, so
we don’t need to
set that.)
“long” means we need to
store a BIG number (up to
2,147,483,647) .
This will probably be used
to count milliseconds, and
counting 1,000 for every
second means it won’t take
long for this number to get
very big.
Explain why constants
and variables are NOT
inside a function. (Global
Scope.)
Read the pot.
(Expecting 0-1023).
Let’s say the
reading is 700.
Plug in the values
of each thing on
this line by going to
each memory
location and
loading the value.
Plug in the values
of each thing on
this line by going to
each memory
location and
loading the value.
sensorValue = 700
millis() returns the number of milliseconds that have
elapsed since this program started running. Since it just
started, let’s give it a small value like 50 milliseconds.
lastTime was set to 0 on the previous slide.
If( 50 > 0 + 700)
If( 50 > 0 + 700)
Since 50 is NOT greater than 700, this True/False
question evaluates as False. So, the yellow highlighted
lines are NOT executed and the blue highlighted line is
executed, and the LED is turned off.
In this circuit, we will use an
RGB LED. RGB is short for
Red-Green-Blue, meaning that
is has three different colored
LEDs inside it. We will use the
potentiometers to vary the
brightness of each, allowing us
to see thousands of different
colors depending on the mix of
Red, Green and Blue that we
select.
Connect the three
potentiometers to
the first three
Analog Pins,
the Red Pin of the
RGB LED to Pin 6,
the Green Pin of the
RGB LED to Pin 10
and
the Blue Pin of the
RGB LED to Pin 11.
And set the three
Pins of the RGB
LED to OUTPUT.
The End
You Can Learn Arduino Programming!
Your Instructor is:
Richard T. Vannoy II
[email protected]
www.TeachElectronics.com