Transcript Day 4
PHY 235 Robotics Workshop
Day 4
Robotic Behaviors,
Light Sensing, Voltage Dividers,
LCD-MOD
Robot Behavior
• Open Loop Behavior: In our first challenge,
we programmed our robot to do a set of
specific, deterministic behaviors (move
forward, emit a sound, print a message).
Each time our program runs, the robot
(hopefully) does the same behavior. This is
called Open Loop Behavior.
Robot Behavior
• Closed Loop Behavior: For most real-world
activity, open-loop behavior is not agile
enough to be successful. The robot must be
able to adapt, or change, its behavior
depending on input from sensors. This is
called Closed Loop Behavior. (Or feed-back)
We worked on one example of this kind of
behavior in our touch sensing navigation
program using whiskers. (like a cat)
Light Sensing with Photoresistors
• Another type of closed-loop behavior can be
achieved by using a light sensor
(photoresistor).
• A Photoresistor is a light sensitive resistor.
Light Sensing with Photoresistors
• Photoresistors are easy to use and
inexpensive.
• As the name implies, a photoresistor is a
resistor that reacts to light. The active
ingredient Cadmium Sulfide (CdS) allows
electrons to flow more easily when light
energy hits it, thus lowering it resistance
(opposition to current flow).
• The brighter the light the lower the resistance.
Light Sensing Voltage Divider
• A voltage divider can be used to determine the state
of a photoresistor.
Vin
Vout
Vout = Vin * R2/(R1 + R2)
Light Sensing Voltage Divider
• As the sensor darkens, the resistance of the
photoresistor will increase – thus, the voltage at p8
will increase.
p8
Light Sensing Voltage Divider
• In our circuit, the 220 Ohm resistor (current
limiter) can be ignored compared to the other
resistors. We get:
• Vo = Vdd * 2K/(2K+R)
= 5V * 2K/(2K+R)
(p8)
• 1K ≤ R ≤ 25K
(Indoor light)
• 3.3 ≥ Vo ≥ 0.4
Analog VS Digital Signals
• Some of the I/O Pins on the ZX-24a
microcontroller can read the analog value of
an input signal.
• A digital signal can only be high (5volts or 1)
or low (0 volts or 0).
• Analog signals can be any value between high
and low.
• Pins p8-p15 (ZX-24 programming pin numbers
13-20) can read analog input signals.
Analog Input Function
• GetADC(pin) - this is a ZBasic function. A
function is like a subroutine, but a function
returns a value. In this case, GetADC returns
an integer that represents the analog voltage.
• If integer adcVal is the return value of
GetADC, then the formula for conversion to
the actual voltage seen by the pin is
Vo = Vdd * adcVal/1024
Or,
adcVal = int((Vo/Vdd) * 1024)
Light Sensing Voltage Divider
• Voltage at Pin: 0.4 ≤ Vo ≤ 3.3
• Pin Value:
82 ≤ adcVal ≤ 675
• Light Value: Dark -Bright
Light Sensing Voltage Divider
• Testing:
– Do ACTIVITY #1: BUILDING AND TESTING
PHOTORESISTOR CIRCUITS in our Boebot
textbook. In the circuit shown in the text, use pins
8 and 9 instead of pins 3 and 6. Also, substitute
the program on the next slide for the test
program. (This is called “lightSensor” on the code
web page)
– Run the program for testing. Shine a bright light
on the photoresistors and then cover them up to
see what the printed values are.
Light Sensing Program
const p8 as byte = 13
const p9 as byte = 14
dim p8value as integer
dim p9value as integer
' only pins 8-15 can be ADC inputs
' variable to store pin8 analog value
' variable to store pin9 analog value
Sub Main()
do
p8value = GetADC(p8) ' Get value of photoresistor
p9value = GetADC(p9) ' Get value of photoresistor
debug.print "p8 = "; p8value; " p9 = "; p9value
call delay(0.5)
loop
End Sub
The Breadboard and Circuit
Breadboard Area
Construction
A Breadboard is an electrical testing area for
prototyping by quickly connecting components.
• The rows are electrically
connected to make connections
between devices.
• Headers are provided
on 2 sides for:
–
–
–
–
I/O connections (P0-P15)
Vdd: + 5 Volts
Vss: 0 Volts
Vin: Supply Voltage from battery
The Breadboard and Circuit
Construction
Example connections of devices. Do Not Build.
Note how the rows of sockets make complete
paths of current between devices and from
I/O headers and Vdd or Vss.
LCD Terminal AppMod
• The LCD Terminal AppMod provides a simple
and convenient method of adding a standard
character LCD and 4 user-input buttons to
BASIC Stamp projects.
LCD Terminal AppMod
• The LCD Display is added to the BOE board by
means of the 2x10 AppMod
Header socket on the BOE
• The LCD comes with a male
2x10 pin header. Plug one end
of this into the BOE socket.
LCD Terminal AppMod
• Plug the other end of the male header into
the underside of the LCD board, as shown.
LCD Terminal AppMod
• To help make a secure platform for the LCD
board, we can also put a metal hexagonal post
between the LCD board and
the BOE board as shown.
Attach with a screw at the
base and a nut at the top.
LCD Test Code
• Now, we will see how to write code using the LCD
display. Since the buttons on the display use I/O
pins 4,5,6,and 7, we must make sure we
disconnect any sensors attached to those pins on
the BOE.
• On the course Code page you will find a zipped
ZBasic project called LCDAPP_MOD. Download
this project, unzip the files, and open the project
in the ZBasic IDE.
Multiple Source Files
• Note that this project uses two source files:
– LCD_DEMO.BAS
• This is the code that does the various tests of the LCD
terminal’s functionality
– LCD_APPMOD.BAS
• This is a “module” created by the maker of the ZX-24a. It
consists of a set of subroutines and functions that allow us
to access the various capabilities of the LCD terminal
• The LCDTest project consists of these two files.
When run, all code in both files is downloaded to
the ZX-24a and run.
LCD Test Code
• Note that there is just one source file
“LCD_DEMO.bas” that has the Main() subroutine.
• Run the project to see how the LCD is used.
Team Tasks – LCD Monitor Program
• Write a program that will 1) find the IO values
of the light sensors and 2) write these values
to the LCD display.
• Test your program by disconnecting the USB
cord and letting the Boe-Bot roam while you
check the output on the LCD display.
Team Tasks
• Modify your program so that output of sensor
values is under the control of the buttons on the
LCD terminal.
• When Button 1 (farthest left) is pushed, the
display should show the value of the left light
sensor. When Button 2 (next left) is pushed, the
display should show the value of the right sensor.
Team Tasks
• For the remainder of our time, work on the
following task: Create a program that will have
the Boe-bot follow a bright light shone on the
floor in front of it.
• You can read through ACTIVITY #5: FLASHLIGHT
BEAM FOLLOWING BOE-BOT in the Boe-bot text
for more info on how to orient the
photoresistors. The code will be different, but
the basic idea of the program will be the same
in ZBasic.
Team Tasks
• If you want more of a challenge, you can try
Projects 1 and 2 on pages 228-229 in the BoeBot text.