62 3. EMBEDDED SYSTEMS DESIGN
will be powered by a 9 VDC battery which is fed to a 5 VDC voltage regulator. The details of the
interface electronics are provided in a later chapter. To save on battery expense, it is recommended
to use a 9 VDC, 2A rated inexpensive, wall-mount power supply to provide power to the 5 VDC
voltage regulator. A power umbilical of braided wire may be used to provide power to the robot while
navigating about the maze.
Structure chart: The structure chart for the robot project is provided in Figure 3.5.
UML activity diagrams: The UML activity diagram for the robot is provided in Figure 3.6.
Arduino UNO R3 Program: We will develop the entire control algorithm for the Arduino
UNO R3 board in the Application sections in the remainder of the book. We get started on the
control algorithm in the next section.
3.4 APPLICATION: CONTROL ALGORITHM FOR THE
BLINKY 602A ROBOT
In this section, we provide the basic framework for the robot control algorithm.The control algorithm
will read the IR sensors attached to the Arduino UNO R3 ANALOG IN (pins 0 – 2). In response
to the wall placement detected, it will render signals to turn the robot to avoid the maze walls.
Provided in Figure 3.7 is a truth table that shows all possibilities of maze placement that the robot
might encounter. A detected wall is represented with a logic one. An asserted motor action is also
represented with a logic one.
The robot motors may only be moved in the forward direction. We review techniques to
provide bi-directional motor control in an upcoming chapter. To render a left turn, the left motor is
stopped and the right motor is asserted until the robot completes the turn. To render a right turn,
the opposite action is required.
The task in writing the control algorithm is to take the UML activity diagram provided in
Figure 3.6 and the actions specified in the robot action truth table (Figure 3.7 and transform both
into an Arduino sketch. This may seem formidable but we take it a step at a time.The sketch written
in the Applications section of the previous chapter will serve as our starting point.
The control algorithm begins with Arduino UNO R3 pin definitions. Variables are then
declared for the readings from the three IR sensors. The two required Arduino functions follow:
setup() and loop(). In the setup() function, Arduino UNO R3 pins are declared as output.The loop()
begins by reading the current value of the three IR sensors. Recall from the Application section in
the previous chapter, the 512 value corresponds to a particular IR sensor range. This value may
be adjusted to change the range at which the maze wall is detected. The read of the IR sensors is
followed by an eight part if-else if statement.The statement contains a part for each row of the truth
table provided in Figure 3.7. For a given configuration of sensed walls, the appropriate wall detection
LEDs are illuminated followed by commands to activate the motors (analogWrite) and illuminate
the appropriate turn signals.The analogWrite command issues a signal from 0 to 5 VDC by sending
a constant from 0 to 255 using pulse width modulation (PWM) techniques. PWM techniques will
be discussed in an upcoming chapter.The turn signal commands provide to actions: the appropriate
3.4. APPLICATION: CONTROL ALGORITHM FOR THE BLINKY 602A ROBOT 63
IR sensor
left
IR sensor
middle
IR sensor
right
male
header
pins
5 VDC
5 VDC 5 VDC
Sensor connection:
- Red: 5 VDC
- Yellow: Signal output
- Black: Ground
M
+
-
240
1N4001
1N4001
left motor/wheel
interface
2N2222
3 VDC
at 100 mA
5V
Gnd
ANALOG IN
012345
DIGITAL
Arduino
UNO R3
5 VDC
220
10K
2N2222
5 VDC
220
10K
2N2222
5 VDC
220
10K
2N2222
5 VDC
220
10K
2N2222
5 VDC
220
10K
2N2222
A0
A1 A2
D11
D10
D2
D3
D4 D5 D6
left turn
signal
wall
left
wall
center
wall
right
right turn
signal
M
3 VDC
at 100 mA
+
-
5 VDC
1N4001
1N4001
1N4001
right motor/wheel
interface
240
voltage
dropping
diodes
protection
diode
motor
current
2N2222
1N4001
5 VDC
3
1
2
1
1
1
0
1
98 76543210
Figure 3.4: Robot circuit diagram.
64 3. EMBEDDED SYSTEMS DESIGN
ADC
ADC
Initialize
ReadADC
ch for
conv
conv
data
left
IR sensor
right
IR sensor
middle
IR sensor
determine_robot
_action
sensor
data
robot
action
PWM_left
left
motor
PWM_right
right
motor
desired
motor
action
motor_control
digital
input/output
left
turn
signal
right
turn
signal
wall
detect
LEDS
Figure 3.5: Robot structure diagram.
turns signals are flashed and a 1.5 s total delay is provided. This provides the robot 1.5 s to render a
turn. This delay may need to be adjusted during the testing phase.
//*************************************************************************
//analog input pins
#define left_IR_sensor 0 //analog pin - left IR sensor
#define center_IR_sensor 1 //analog pin - center IR sensor
#define right_IR_sensor 2 //analog pin - right IR sensor
//digital output pins
//LED indicators - wall detectors
#define wall_left 3 //digital pin - wall_left
#define wall_center 4 //digital pin - wall_center
#define wall_right 5 //digital pin - wall_right
//LED indicators - turn signals
#define left_turn_signal 2 //digital pin - left_turn_signal
3.4. APPLICATION: CONTROL ALGORITHM FOR THE BLINKY 602A ROBOT 65
include files
global variables
function prototypes
initialize ports
initialize ADC
initialize PWM
while(1)
illuminate LEDs
- wall detected
issue motor
control signals
read sensor outputs
(left, middle, right)
determine robot
action
illuminate LEDs
- turn signals
delay
a) UML for C programming
loop()
illuminate LEDs
- wall detected
issue motor
control signals
read sensor outputs
(left, middle, right)
determine robot
action
illuminate LEDs
- turn signals
- delay
setup()
- configure pins for output
define global variables
b) UML for Arduino programming
Figure 3.6: Robot UML activity diagram.

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.