At-Bot E120703 Small

Download as pdf or txt
Download as pdf or txt
You are on page 1of 162

Robo-Creator : AT-BOT ctivity book 1

AT-BOT
The 4-wheel autonomous robot
programmed with C/C++ language
Activity book

2Robo-Creator : AT-BOT activity book

AT-BOT activity book


All rights reserved under the Copyrights Act B.E. 2537
Do not copy any part of this book without our permission

Who should use this handbook?


1. Students and other people who are interested in applying to microcontrollers for
testing working process of automatic system or people who are fascinated in learning
and examining the microcontrollers in new approaches such as using an autonomous
robot as a form of an interactive media.
2. Academic institutes such as schools, colleges and universities, where provide
electronic subjects or electronic and computing engineering departments.
3. Lecturers and teachers who would like to study and prepare lesson plans for
microcontroller courses, including applied science which focuses on integrating
electronics, microcontrollers, computer programming and scientific examination in high
school education, vocational education, and bachelors degree.

Published and distributed by


Innovative Experiment Co.,Ltd.
108 Soi Sukumvit 101/2 Sukumvit Road, Bangna, Bangkok 10260 THAILAND

Details and illustrations used in this handbook are thoroughly and carefully to provide
the most accurate and comprehensive information under the conditions and time we
have before publishing. Innovative Experiment Co.,Ltd. shall not be responsible for
any damages whatsoever resulting from the information of this book as constant
revisions and updates will be published after this edition.

Robo-Creator : AT-BOT ctivity book 3

Clarification from writer/


complier team
All Illustrations and Technical information found in this handbook are in our best
interest to simplify working processes and equipment principles so that it could be easily
understood by any user interested in robotics.
Therefore, The translation from THAI language to English and the usage of technical
terms may not follow the provision of the Royal Academy where they are many words
not described officially. Our team would be allowed to produce new technical terms.
The main reason of this explanation comes from data collection of equipment in
embedded computer system and robotic technology. Thai language is quite hard to
translate into English and thus our writer team gathered the required data and
investigated to make sure that the understanding in working processes has the limited
error.
When we com pose the information in Thai, many technical terms have
complicated m eanings. Definition of vocabul ary occurred from the practice
coordinated with linguistic meaning. If there are any errors or mistakes shown, the team
of writer will accept and if we get explanation or suggestion from any expert, we will
clarify and improve those errors as soon as possible.
In order to develop the academic media especially with new technological
knowledge, it will be able to proceed continually under the participation of experts in
all fields.

Innovative Experiment Co.,Ltd.

4Robo-Creator : AT-BOT activity book

Table of contents
Chapter 1

AT-BOT : The 4-wheel autonomous robot......................................5

Chapter 2

AT-BOT Development tools...............................................................17

Chapter 3

Wiring IDE introduction.....................................................................23

Chapter 4

ATX Library file....................................................................................35

Chapter 5

The ATX controller board hardware experiment..........49

Chapter 6

AT-BOT movement........................................................79

Chapter 7

Object avoiding by contact........93

Chapter 8

AT-BOT Line tracking..............................................................105

Chapter 9

AT-BOT with touchless object avoiding............................................147

Chapter 10

AT-BOT with Servo motor ......................................................155

Robo-Creator : AT-BOT activity book 5

Chapter 1
AT-BOT : The 4-wheel autonomous robot
AT-BOT (All-Terrain mobile robot) is an autonomous robot performed by DC motors
with the set of 4 DC motor gearboxes come with spiked wheels in order to aid passing
through rough surface more efficiently. Also, it can move up on the slope in the level of 0
to 25 degree and if it is necessary to stop immediately to change the direction of motion,
it can do. The possibility is that AT-BOT is capable of supporting any mission in either a
smooth competition court with lines appeared for determining directions of motion or
rough court with barriers or participating with the World RoboCup Junior-Rescue.

Programming development of AT-BOT robots uses C/C++ language in the open


source software are named Wiring (www.wiring.org.co).
AT-BOT controller boad is called ATX board, which is able to drive 6 of DC motors
and 6 of Servo motors together. It has many ports for interfacing with both digital and
analog sensors, basic digital outpuit port and using the data communication via 2 lined
bus system called I2C bus and the standard UART serial bus.
The main driving system is consisted of 4 DC motor gearboxes with spiked wheels.
This results the AT-BOT with four-wheel automatic robot has a high driving power and high
speed. It identifies that this is a presentation of the different robot driving system from the
old system for studying.
AT-BOT is one of the robotic activities of Robo-Creator, the robotic kit for creative
education.

6 Robo-Creator : AT-BOT activity book

1.1 AT-BOT part list


1. ATX controller board
2. USB-miniB cable
3. Light reflector (ZX-03R) 4 sets
4. Touch sensor (Switch input boards) 2 sets
5. BO-1 DC motor gearbox 48:1 ratio with mounting and cable 4 sets
6. Standard servo motor x 1
7. Spike wheel sets (diameter 65mm., width 26mm. also include the hub for BO-1
gearbox) 4 sets
8. 5-AA battery holder with wire
9. Plasitc joiner set and Strip joiner set
10. Right angle metal shaft set
11. Nuts and screws set
12. CD-ROM (software, example code and documentations)
13. Activity manual and construction sheet
14. Line tracking demonstration paper

1.2 ATX controller board features


In the figure 1-1, it is shown components of the ATX controller board and there are
significant technical features as follows:
Main microcontroller is Atmels ATmega128. It features 8-ch 10-bit Analog to

Digial Converter, 128-KByte Flash memory , 4-KByte EEPROM, 4-KByte RAM. Operated with
16MHz clock from external crystal
Define all ports compatible with Wiring I/O standard hardware (www

.wiring.org.co). The number of port are 0 to 50.


13 programmable port in JST connector type. Includes Digital I/O port (2 :

port 14 and 15), A/D port (7 : port 40/ADC0 to port 46/ADC6), Two-wire interface or TWI (2
: port 0/SDA and port 1/SCL) and UART serial port communication (2 : port 2/RX1 and port
3/TX1). Both TWI and UART ports can config to digital input/output port for more I/O
applications.
Analog input (ADC0 to ADC6) supports 0 to +5Vdc input. The converter

resolution is 10-bit. The result value is 0 to 1,023 range.

Robo-Creator : AT-BOT activity book 7

One variable resistor is connected with the Analog input ADC7 of main

microcontroller for simple ADC experiment.


2 of button switches with resistor pull-up are connected with port 49 and 50 of

the Wiring I/O controller board for simple digital input experiment.
One LED with a current limited resistor. It is connected with port 48
One piezo speaker at port 4
16x2 characters LCD for monitoring
On-board digital compass; HMC6352 from Honeywell. It is interfaced by I2C

bus or TWI
UART port for interfacing serial module device such as camera module (ZX-

CCD, CMUCAM1, CMUCAM2, mCAM), servo controller board (Parallax servo controller,
ZX-SERVO16U), Real-time clock (ZX-17 : serial real-time clock moduel)
6-ch DC motor driver with indicators. Support 4.5V to 9V DC motor. Maximum

current output 3A and 1.2A continuous.


6-RC servo motor output; support 4.8 to 7.2V standard and continuous servo

motor types.
Motor driver power indicator; nomally turned on. It will off when motor is short-circuit.

42 ADC2

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

43 ADC3

START

C r e a t o r e > >> > > > > >


3

c o n t r o l l er R b o a r d
USB DATA

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

SW1

49

SW2

50

48

ADC7

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

KNOB

RESET

7.2-9V BATT.

14

PC6

15 PC7

MOTOR

Figure 1-1 : ATX controller board layout

8 Robo-Creator : AT-BOT activity book

5-status battery level monitor circuit :

- Last left yellow LED displays that the input supply voltage is 6.75V. When the
battery voltage is lower than 6.75V, this LED will start to blink.
- Next yellow LED displays the input supply voltage at 7.0V.
- First green LED displays the input supply voltage at 7.25V.
- Second green LED displays the input supply voltage 7.5V.
- Last right green LED displays that the input supply voltage is higher than at 7.75V.
Download and interface with computer via USB port by using USB to UART

converter chip; FT232RL. USB connection indicator is available.


Set the operation by Mode/Reset switch
Supply voltage range +7.2 to +9V 2400mA for 4 motor loads.
2 voltage regulator on-board; +5Vdc for microcontroller and all digital circuit,

+6Vdc for all motor driver circuits. By using voltage regulator; the motor driver circuit can
drives DC motor with constant speed when battery voltage is full and reduce to 60%. It
features constant speed without effect by battery voltage until it is lower 60% of full.

Robo-Creator : AT-BOT activity book 9

1.3 Output device features


1.3.1 DC motor gearbox
DC motor gearbox of Robo-Creator kit is the BO1 model. The technical features are as
follows :
Requires the supply voltage +4.8 to +9Vdc 130mA @6V and no load
Gear ratio 48:1
Speed 250 round per minute @6V and no load
Weight 30 gram
Torque 0.5kg.-cm.

Driver gear (2) 9 teeth


Driver gear (1) 8 teeth

Driver gear (4) 17 teeth

Driver gear (3) 17 teeth

Follower gear (3)


Follower gear (1)
28 teeth
36 teeth
Follower gear (4)
Follower gear (2)
28 teeth
36 teeth

Figure1-2 : Details and gear diagram of BO-1 DC motor gearbox

10 Robo-Creator : AT-BOT activity book

1.3.2 Standard servo motor


Servo motor has 3 wires; Signal (S), Supply voltage(+V) and Ground (G) The technical
features is as follows :
Requires the supply voltage +4.8 to +6Vdc
Weight 45 gram
Torque 3.40kg-cm. or 47 oz-inches.
Size (width x length x height) 40.5 x 20 x 38 mm. or 1.60 x 0.79 x 1.50 inches.

(A)

(B)

(C)

Figure 1-3 : Details of standard RC servo motor of Robo-128


(A) outside body

(B) Gear system

(C) Electronic circuit board

1.4 Sensor features


1.4.1 Touch sensor/Switch input board
The circuit is shown in the figure 1-4 including a switch with a LED and considered
output as logic 0 when switch is pressed.
If the switch is pressed : the logic 0 will be sent and the red LED is on.
If no press : LED is off and the logic is 1.

LED1

+V
R2
10k

R1
510

S1
Switch

R3
220

DATA

GND

Figure 1-4 : The touch sensor or Switch input board picture and
schematic diagram

Robo-Creator : AT-BOT activity book 11

Output port

LED

LED1

+V
OUT

SFH310

GND

10k

220

Light reflection sensor

Figure 1-5 : Light reflector sensor layout andshcematic diagram

1.4.2 The light reflection sensor : ZX-03R


The circuit and layout of this sensor are displayed in the figure 1-5. The circuit is used
to detect the reflected lights from surface or lines.
During apply the power supply, the red LED is bright at all the time. Meanwhile, the
light receptor is SFH310 photo-transistor and it will get red lights from the reflection of objects
or surface. The amount of the reflected light will be more or less depending whether there is
an obstacle or not and how well the object can reflect red light. The reflection of red lights
is based on the surface texture and colour of objects. It is said that the white smooth objects
are able to reflect light well so the infrared receptor gets a lot of reflected light and the
output voltage will be high. As black objects reflect less light, the light receptor sends low
voltage. With such features, the sensor circuit board is often used to trap the reflected light
on the surface and lines. It is necessary to install the circuit at the lower part of a robot.
With the use of red light as a main light to detect, this allows the sensor to measure
the colour difference on the surface printed by IR or UV resistant ink.
Due to ZX-03R light detection circuit gives the result as DC voltage, applying on ATBOT robot has to connect the signal to 7 channels of analog input on the ATX controller
board, from ADC0 to ADC6. After reading the analog signal value, use this value to check
the value on reflected light detection circuit and then apply to detect lines.

1.4.3 GP2D120 module detecting distance with Infrared


GP2D120 is a module that detects distance with Infrared and with the packet of 3
extension parts, including 3 pins; +Vcc, GND and Vout. Reading voltage value from GP2D120
must do after the preparation period of the module, which takes 32.7 to 52.9 millisecond (1
millisecond equal 0.001 second). So reading the value should wait for the suitable time as
mentioned above and shown the basic data in the figure 1-6.
Output voltage of GP2D120 at the distance of 30 centimetres, the power supply at
+5V as in the range of 0.25 to 0.55V has the mean as 0.4V and the range of output voltage
change at the distance of 4 centimetres is 2.25V0.3V.

12 Robo-Creator : AT-BOT activity book

Infrared LED transmitter

Output voltage (V)

Infrared Receiver

2.8
2.4
2.0

GP2D120

1.6
1.2

Vout GND

Vcc
0.8
0.4

Supply

0
38.39.6 ms

12

16

20

24

28

32

Distance (cm)
Measurement

Vout

1st measure

2nd measure

Not stable

1st output

n measure

2nd output

n output

5 ms

* Use Kodak R-27 gray-white paper.


The white side has a 90%
reflection rate, made from a
material that reflects light for range
measurement.

How to measure distance


The infrared light is sent out from a transmitter to the object

Object

in front, by passing through a condense lens so that the


light intensity is focused on a certain point. Refraction
occurs once the light hits the surface of the object. Part of
the refracted light will be sent back to the receiver end, in

which another lens will combine these lights and determine


A

the point of impact. The light will then be passed on to an


array of photo-transistors. The position in which the light falls
can be used to calculate the distance (L) from the
transmitter to the obstacle using the following formula:

L F

A X

F
Transmit LED

GP2D120

Therefore, L equals

Photo array

FA
X

Thus, the distance value from the phototransistors will be


sent to the Signal Evaluation Module before it is changed
to voltage, resulting in a change of voltage according to
the measured distance.

Figure 1-6 : shown shape, arrangement of pins, diagram of time of


operation, and graph shown the operation of GP2D120 sensor

Robo-Creator : AT-BOT activity book 13

1.5 Mechanical compoments


1.5.1 Plastic wheels for BO-1 DC motor gearbox and rubber tire
A circular wheel has the diameter of 65 millimeters. It is able to fit with the axis of BO1 gear motor directly without any additional modification and tighten with a 2 mm tapping
screw. A wheel tire is made up of rubber and its texture has treads in order to enhance in
sticking over the surface.

1.5.2 Spike wheel


The wheels are unique. The wheel surface is rubber and has triangular buttons with
rounded end and thorn-like protrusions to aid in adhesion and movement across smooth
surface, rugged surface, slope or zippers with the slope less than 20 degree.
To install this kind of plastic wheels with the axis of the gear motor of model BO-1 will
be required to use special joints to help and attach with a screw and 3 mm nuts.

1.5.3 Track and wheel set


It is a track set constructed for track with
wheels or crawler track with wheels in different
sizes and it is compatible with a plate set. The
set includes tracks with 30 joints (2 pieces), 10
joints (4 pieces), 8 joints (4 pieces), driving
wheels (2 pieces), large support wheels (2
pieces), medium support wheels (10 pieces),
poly caps (12 pieces), stainless axis with the
diameter of 3 mm, length of 110 mm (4 pieces)
and necessary screw set.

14 Robo-Creator : AT-BOT activity book

1.5.4 Base plate and Plate set


It is a multi-purpose material set for making the base or the chassis frame. The set
provides plastic plates produced from 2 types of ABS materials. The first type is called ATplate in black and the other is a plate with the size of 160 x 60 mm. There are holes on the
plates and each hole is 3 mm in size and they are seperated by 5 mm in distance and the
total holes are 341. The set also contains brackets for long axis (2 pieces), brackets for
short axis (2 pieces) and a screw set necessary for fastening.

1.5.5 Grid plates


Plates are plastic manufactured from ABS materials in the size of 80 x 60 mm and 80 x
80 mm each. Each hole has the size of 3 mm and the distance between each hole is 5 mm.

1.5.6 Plastic joiners


They are stiffed plastic components and there are three designs, including, straight
joiners, right angle joiners, and obtuse angle joiners. Each piece can be inserted together
and they are used to construct a decorated structure (or decoration). A set contains all
three types, available 5 colors and 4 pieces for each, and 60 pieces in total.

Robo-Creator : AT-BOT activity book 15

1.5.7 Plastic strip joiners


They are rigid and tough and there are holes in the size of 3 mm for each piece for
installing or connecting with other structure components by a screw. At the end of each
rod can insert with plastic joiners. There are three different sizes including 3, 5 and 12 holes
and each size has 4 pieces in a set.

1.5.8 Metal angle bar


Metal angle bars are metal components with the width of 7.5 mm and they are
cut into the right angle shape. There are holes with the size of 3 mm for using a screw to
install or construct with other structure parts. Three different sizes are provided in a set,
including 1x2 holes, 1x2 holes, and 2x5 holes and each size contains 4 pieces.

1.5.9 Screws and nuts


Screws and nuts are equipment for fastening many components together. They
compose of 2 mm-tapping screws (4 pieces), 3x8 mm-tapping screws (4 pieces), 3x10 mmtapping screws (30 pieces), 3x15 mm-tapping screws (4 pieces), 3x40 mm-tapping screws
(4 pieces), 3x8 mm-flat head screws (4 pieces), 3x5 mm-hand driven screws (4 pieces), 3x20
mm-hand driven screws (2 pieces), and 3 mm-nuts (30 pieces).

1.5.10 Metal stands-off


This kind of materials helps to hold different components together and support
boards, grid plates and base plates. They are made of rustproof nickle-plated metal and
have a characteristic of cylinder with the length of 25 mm. Inside of a cylinder, there is a
spiral hole along its body for a 3 mm-screw to fasten and 3 poles are provided in a set.

16 Robo-Creator : AT-BOT activity book

1.5.11 Plastic stands-off


Plastic stands-off help fasten different parts and prop up boards, grid plates, and
base plates. They are made from cohesive ABS plastic but able to be cut. The shape of
the rod is cylindrical and there is a hole throughout the rod to insert 3 mm-screws. You can
get different sizes and number of support poles, including, 3 mm. (4 pieces), 10 mm. (4
pieces), 15 mm. (4 pieces), and 25 mm. (4 pieces) in the set.

1.5.12 5-AA Battery holder


It is used to carry 5 AA batteries. There is a wire which connects the anode and the
cathode to the main controller board immediately.

1.5.13 JST3AA-8 cable


This is an INEX standard cable, 3-wires combined with 2mm. The JST connector is at
each end. 8 inches (20cm.) in length. Used for connecting between controller board and
all sensor modules in the Robo-Creator kit. The wire assignment is shown in the diagram
below.

2mm. pitch
GND
S
+5V

2mm. pitch
GND
S/Data
+5V

Robo-Creator : AT-BOT activity book 17

Chapter 2
AT-BOT Development tools
Robo-Creator robot kit supports the operation controller program which can be
developed from Assembly, BASIC or C programming languages. For here, we will use
C/C++ programming language with the open-source software called Wiring, which is
the name of a development project of a small control system in order to apply the
software and hardware together.
We focus on concrete utilization as well as the connection of devices with
electronic system so that the system can work according to the statement written
correctly, collectively, Physical computing or the computer system which concentrates
on physical signal connection, connecting external sensor devices or controlling display
of LEDs, light, and sound, etc.
The official website of Wiring here is www.wiring.org.co. At this website, there is
data of both hardware and software allowed to download with free of charge. Also, it
is the open-source project to give an opportunity to developers who will be able to join
the project and expand the project freely.
The founder of Wiring is Hernando Barragan (Architecture and Design School,
Universidad de Los Andes, Comlumbia). Wiring started at the Interaction Design Institute
Ivrea in Italy and it is currently developed at the Universidad de Los Andes, Architecture
and Design School in Colombia.

2.1 Supported operating system


The software for the program development is Wiring Development Environment
or sometimes called Wiring IDE and it can work with these operating systems or platforms.
Mac OS X 10.4 or higher (both models using Powerpc and Intel CPU)
Windows XP, Windows Vista and 7
Linux, both Fedora Core and Debian (including Ubuntu as well)
Other platforms which support the operation of Java 1.4 up

2.2 Wiring hardware


The main hardware of Wiring is Wiring I/O board which is a small circuit board
with ATmega128 microcontroller. ATmega microcontroller is very important to control
all kinds of work in which the main controller or microcontroller will be programmed

18Robo-Creator : AT-BOT activity book

through USB port in Wiring IDE software. There is a connection point to recieve signals
from both analog and digital external sensors that allows the board to acquire information
from the surrounding environment (temperature, light, distance to an object etc.).
Moreover, there is another point to send signals out to control external devices, such
as LEDs, loudspeakers, servo motors, and liquid crystal display or LCD, etc.
Robo-Creator robot kit provides the hardware of controller board compatible
with Wiring hardware and the system of Wiring IDE so you will be able to develop the
program comfortably.

2.3 Introduction to Wiring 1.0 IDE


Wiring 1.0 is the software for developing C/C++ programming language in order
to create a program controlling ATmega1281 microcontroller on Wiring I/O hardware
and then use the program in AT-BOT robot in Robo-Creator kit as well.
In the kit, tools used in development of the program are contained completely in
the format of IDE (Integrated Development Environment), either the text editor for coding
or C complier. Downloading the program and the window of serial monitor for receiving
and sending serial information to AT-BOT robot. Wiring is designed to use easily and the
C/C++ language is for programming which can work on the operating system of
Windows XP up, Linux and MAC OSX and installation files for each platform are separated.

2.3.1 Software installation


(1) Insert the CD Rom, come with Robo-Creator robot kit. Click the file named
Wiring1000_RoboCreatorR1_Setup.exe (the number of the installation file may be
changeable) and then the window of welcoming to the Wiring 1.0 setup will appear.

Robo-Creator : AT-BOT activity book 19

(2) Next, click to agree in each step of the setup as installation of other applications
of Windows until completion.
(3) Installing the Wiring 1.0 software by using the CD rom is bundled with RoboCreator robotic kit is the setup of both Wiring 1.0 software and USB driver to connect
with ATX controller board in the same time.
(4) Test to start the program by select START > All Programs > Wiring. Then for a few
moment, the window of Wiring IDE will be present.

After That you can use the Wiring IDE in the program development for AT-BOT
robots.

20Robo-Creator : AT-BOT activity book

Connect with USB port

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

4 Display RUN mode

R o b o - C r e a to r > > > >


3

> o R u n n i n g. . . o a r d
2

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

SW1

49

SERVO PORT
9

SW2

50

48

BATTERY LEVEL

10 11 12 13
S

Turn on power

ADC7

RESET

7.2-9V BATT.

14

PC6

15 PC7

MOTOR

3 Wait until the USB LED is on

Figure 2-1 : The steps of preparation for checking USB serial port
positions of AT-BOT robot

2.3.2 Checking the USB Serial port for the AT-BOT


(1) Plug the USB cable connecting ATX control board with the USB port of the
computer. Turn on and wait for the blue LED at the position of USB on the circuit board
is on as the figure 2-1.
(2) Click the START button and go to the Control Panel.
(3) Then double-click the System
(4) Go to the tab of Hardware and click on the Device Manager button

Robo-Creator : AT-BOT activity book 21

(5) Check the hardware listing at Port. You should see USB Serial port . Check the
position. Normally it is COM3 or higher (for example; COM10). You must use this COM
port with the Wiring IDE software.

22Robo-Creator : AT-BOT activity book

2.3.3 AT-BOT with Wiring IDE interface


(1) Open Wiring IDE. Wait for a while. The main window of Wiring IDE will appear.
(2) Choose the suitable hardware by select menu Tools > Board > INEX > RoboCreator R1 > ATmega1281 @16MHz

(3) Select menu Tools > Serial Port to choose the USB serial port of AT-BOT. It is
COM4 (for example).

Must do this step for every new connection of the AT-BOT with Wiring IDE

Now the AT-BOT is ready for interfacing and code development with the Wiring
IDE.

Robo-Creator : AT-BOT activity book 23

Chapter 3
Wiring IDE introduction
This chapter presents preliminary information of Wiring, which is the software tool
for developing the operation of AT-BOT robots or one of the activities to build robots in
Robo-Creator kit. For the details of program structure of C/C++ language Wiring supports,
you can read in the Wiring IDE help.

3.1 Components of Wiring IDE


Wiring IDE consist of two important parts which are text editor and C/C++
compiler. There are many tool s and comm and buttons to help the program
development as appeared in the figure 3-1.

3.1.1 Menu bar


Including File, Edit, Sketch, Tools and Help menu, will affect work files doing at
the present only.

Figure 3-1 : Main window of Wiring IDE software used in the


program development

24Robo-Creator : AT-BOT activity book

3.1.1.1 File

New (Ctrl+N) : Create new files. This is called sketch in Wiring and given name
following the recent date in the format sketch_YYMMDDa, such as sketch_080407a or
click the button

on the tool bar.

Open (Ctrl+O) : Open the exist sketch file or click on the button

Close (Ctrl+W) : Choose to close the sketch file.


Save (Ctrl+S) : Save the open sketch file in the old name and work similarly to
click the button

on the tool bar.

Save as(Ctrl+Shift+O) : Save the open sketch file in the new name and the old
file will not disappear.
Upload to Wiring hardware (Ctrl+U) : Exports the program to the Wiring I/O Board
(inthis document is the ATX controller board). After the files are exported, the directory
containing the exported files is opened. There is more information about uploading
below. It works in the same way as click the button on

the tool bar.

Preference : Customize the operation of Wiring IDE


Quit (Ctrl+Q) : Quit the Wiring program and close all windows of Wiring program.

Robo-Creator : AT-BOT activity book 25

3.1.1.2 Edit
The menu contains commands used to edit the sketch file that develops on the
Wiring IDE.

Undo (Ctrl+Z) : cancel the previous action of a command or the lastest typing.
You can cancel Undo command by click Edit > Redo.
Redo (Ctrl+Y) : To return to make a statement made before the Undo command
is available only when done Undo already.
Cut (Ctrl+X) : Delete and copy the selected text to store at the clipboard, which
functions as the temporary memory unit to preserve information.
Paste (Ctrl+V) : Place the data in the clipboard on the desired position or replace
the selected text.
Select All (Ctrl+A) : Select all letters or text in the open file in the text editor at that
time.
Find (Ctrl+F) : Search for any text in the open file in the text editor. In addition, it is
also able to find and replace another text.
Find Next (Ctrl+G) : Find text or words we use to search for the next one within
the open file in the text editor.

26Robo-Creator : AT-BOT activity book

3.1.1.3 Sketch
Sketch menu is a command menu relating to compile a sketch file.

Verify/Compile (Ctrl+R) : It is a command of program compilation and its function


is similar to pressing the

button on the tool bar.

Import Library : Open the included library of Wiring.


Show Sketch Folder : Show the folder of the current sketch file.
Add File: Add the required program file to the sketch file.

3.1.1.4 Tools
Tools menu is a command menu relating to selection of tools helped to develop
a program. Important commands you should know are as follows.

Auto Format : Try to format program code in the completed form.


Serial Monitor : Open the serial data terminal.
Board : Choose the interfaced hardwarere with the Wiring 1.0.
Serial Port : Choose the interfaced port of the Wiring I/O hardware.

Robo-Creator : AT-BOT activity book 27

3.1.1.5 Help

Getting Started : Open the window about the using Wiring of the Wiring website.
Examples : Open a sketch file of an example program.
Reference : Open Reference window of the Wiring website. It consists of
language, programming environment, libraries, and language comparison. You have
to connect with the internet if you would like to see the information.
Find in Reference (Ctrl+Shift+F) : Choose text in your program code. Here you will
drag black bar and click on it. The program will take the text you have chosen to find in
reference and if it cannot find anything, there will be a warning message in the window
of the program.
Wiring Hardware : Browse the information of Wiring I/O hardware via internet.
Troubleshooting : Open the window about solutions in performance of the Wiring
of the Wiring website.
Visit wiring.org.co (Ctrl+5) : Open the web browser to visit the homepage of
Wiring at http://wiring.org.co.
About Wiring : Show the copyright on the Wiring software

28Robo-Creator : AT-BOT activity book

3.1.2 Toolbar
There are six buttons of basic functions and initial operation as follows.
Run or Compile : This button is used to compile the program code.
New : This button is used to create a sketch file.
Open : Open the exist sketch file
Save : Save the open sketch file in the old name. If would like to
change filename, use Save As command instead.

Upload to Wiring hardware : Exports the program to the ATX


controller board). This procedure is called UPLOAD.
Serial monitor : opens the serial data communication between the
Wiring I/O hardware and the monitor of Wiring IDE through serial ports (or COM port) to
check the information sent back from Wiring I/O hardware (here it is ATX control board,
which is very useful for the detection of the programs operation).

3.1.3 Serial monitor


Wiring IDE has a Serial monitor. It is a serial data communication tool. User can
transmit, receive and show the serial data via this monitor with USB serial port of computer.
In the developed sketch code, must put two imporatant commnands as follows :
1. Serial.begin() : Set the baud rate of serial data communication. Normally the
baud rate value is 9600 bit per second. Must add this command into Setup() of sketchbook.
2. Serial.println() : Assign the sending message to Serial monitor on the Wiring IDE.
Openning the Serial monitor is very easy. Click on the
Serial monitor window is appeared following the figure below.

button at Toolbar. The

Robo-Creator : AT-BOT activity book 29

3.2 How to develop the program


(1) Check the installation of the hardware and software of Wiring. Include the
setting USB serial port that connected with Wiring I/O hardware; the ATX control board
of AT-BOT robot in Robo-Creator kit.
(2) Create a new sketch file by a click of the New button on the Tools bar or
choose from menu File > New

(3) Type the example code as as follows :


#include <atx.h>
int ledPin = 48;
void setup()
{
lcd("Hello Robot!");
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

// Include main library


// LED connected to pin 48 (bootloader)

// Title message on LCD


// Sets the digital pin as output

// Sets the LED on


// Waits for a second
// LED off

This program is used to test a basic hardware of AT-BOT robot. At the LCD display
shows message Hell Robot ! and blink the LED at port 48 of the ATX controller board with
one second rate.

30Robo-Creator : AT-BOT activity book

(4) Go to the File menu to choose the Save as command to save the file in the
name of Test. Now, there is test.pde file happening in the folder called test.

(5) Verify the sketch by click onthe Run button of choose from menu Sketch >
Compile/Verify

If there is any error occurring from compilation, a warning message will


be appeared in the messsage area. Therefore, you will have to correct the program.

If all are correct, the message area will display Done compiling message.

Robo-Creator : AT-BOT activity book 31

1 Connect to USB port


5 Display the programming mode

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

R o b o - C r e a to r > > > >


3

> o B o o t l o ad e r o a r d

Press and hold the START button


3 seconds.
The robot enter to programming
mode

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

SW1

49

SERVO PORT
8

SW2

50

48

LED at port 48 is on
for programming mode

BATTERY LEVEL

10 11 12 13
+

7.2-9V BATT.

Turn-on power

ADC7

KNOB

RESET

14

PC6

15

PC7

MOTOR

3 The blue LED of USB ready is on

Figure 3-2 : Illustration of AT-BOT robot controlled to enter the


program mode
After the compilation is finished, in the folder of test there will be a new
folder are in named Build and within this folder it contains the source file of C++
programming language and a supplementary file.
(6) Connect the ATX board with USB port. Turn-on power. Wait until USB connection
is completely ( blue LED at USB is turned-on) .
(7) Press and hold the START button on the ATX controller board 3 seconds. The
LCD module shows Entry program mode message following the figure 3-2.
(8) Click on the

Upload to Wiring Hardware. Code uploading is started. Wait

until uploading complete. The message Done uploading. RESET to start the new program.
is shown in the status bar of Wiring IDE.

32Robo-Creator : AT-BOT activity book

If there is error occurring from uploading, a warning message will be


appeared in the messsage area as follows

This mostly occurs if the serial port is invalid or not selected the board to
work in the program mode. Correction can be read in the topic of Troubleshooting of
uploading error.
(9) Press on the START button on the ATX control board to start the operation of
the program.
At the display of ATX board; it shows message Hello Robot! and the LED at
the port 48 lights up.

Robo-Creator : AT-BOT activity book 33

3.3 Troubleshooting of uploading error.


3.3.1 In case that you have clicked the Upload button
already but no any action
Cause :
Wiring software cannot link with ATX control board of AT-BOT robot because it is
not in the program mode.
Solution :
(1) Press the Ctrl, Alt and Delete key simultaneously and then the Window Security
window will pop up. Next, click on Task Manager to choose. In some computers, the
program may lead to the Window Task manager window immediately, in this case, you
can choose the Processes tab and search for the file named avrdude.exe. Finally, click
on that file and the End Process button respectively.

(2) Wiring IDE program will resume in a normal status and supply power to the
board again. Select the correct COM port and then set the ATX board to the
programming mode in order to upload the program again.

34Robo-Creator : AT-BOT activity book

3.3.2 In case if you click the Upload button, there is an error


message that not find any hardware for uploading

Cause :
Wiring software cannot connect with ATX control board or AT-BOT robot because
selecting a COM port is not correct.
Solution :
You need to choose an another COM port used for the connection again and
correctly by doing at the Tools > Serial port.

Robo-Creator : AT-BOT activity book 35

Chapter 4
ATX Library file
Developing C/C++ programming language with Wiring for AT-BOT robots is managed
under the support of atx.h library file in order to reduce steps and complexity in programming
to control parts of the hardware because it is required to give the priority for the program
development controlling AT-BOT robots to programming to support competitions.
The structure of atx.h library file shown as the diagram and details of all sub files are
consisted of as follows.

atx.h library file

36 Robo-Creator : AT-BOT activity book

4.1 lcd.h library file of LCD module displaying


This library file supports the instruction set about the display of messages at the LCD
module. Before activating the function of this library, you should append the library file in
the first part of the program with the statement.
#include <lcd.h> or #include <atx.h>
The main function of this library is lcd., which contains the function for message
display at the LCD module in the type of 16 characters and 2 lines.
Syntax

void lcd(char *p,...)


Parameter
p - Type of display data. Support the special character for setting display method.
Command

Operation

%c or %C

Display 1 character

%d or %D

Display the decimal value -32,768 to +32,767

%l or %L

Display the decimal value -2,147,483,648 to +2,147,483,647

%f or %F

Display floating point 3 digits

#c

Clear message before next display

#n

Display message on the second line (bottom line)

Example 4-1
lcd(Hello LCD);

// Displays Hello LCD message at LCD module

Result :

H e l l o o L C D rb o a r d r
W i r i n g I / Ob R o b o t d

Example 4-2
lcd(abcdefghijklmnopqrstuvwxyz);
// Display string. If over 16 charactes, the next character will
// show on the second line automatically.
Result :

abcdefghijklmnop
q r s t u v w x yz R o b o t d

Robo-Creator : AT-BOT activity book 37

Example 4-3
lcd(Value: %d unit ,518); // Display message with number date (518)
Result :

Value:g518kunitp
q r s t u v w x yz R o b o t d

Example 4-4
lcd(Value: %d ,analog(4));
// Display analog value from analog port 4 (PA4)
Result :

Value:gxxxkunitp
q r s t u v w x yz R o b o t d

therefore xxx as reading value 0 to 1023

Example 4-5
char c_test=j;
lcd(abcd%cxyz,c_test);
// Display character j with any message
Result :

abcdjxyzxxkunitp
q r s t u v w x yz R o b o t d

Example 4-6
lcd(Value: %f ,125.450);
// Display message with floating number 3 digit
Result :

Value:g125.450tp
q r s t u v w x yz R o b o t d

38 Robo-Creator : AT-BOT activity book

Example 4-7
lcd(count1: %d #ncount2: %d,12,48);
// Display message with 2 control code and special key #n
// for moving all message after #n to line 2 or bottom line of
// LCD screen
Result

count1:112.450tp
c o u n t 2 : x 48 R o b o t d

4.2 sleep.h : The delay time library


This library file supports all instructions for time delaying. This library must be included
at the top of the program with the command #include as follows :
#include <sleep.h> or #include <atx.h>
The important function is sleep . It delay time in millisecond unit.
Syntax

void sleep(unsigned int ms)


Parameter
ms - Set the delay time in millsecond unit. Range is 0 to 65,535.
Example 4-8
sleep(20);
sleep(1000);

// Dealy 20 miliisecond
// Delay 1 second

Robo-Creator : AT-BOT activity book 39

4.3 in_out.h : Digital input/output port library


This library file supports all instructions for readind and writing data to digital port of
controller board. This library must be included at the top of the program with the command
#include as follows :
#include <in_out.h> or #include <atx.h>
Important functions of this library file are consisted of :

4.3.1 in
Read data from the specific digital port
Syntax

char in(x)
Parameter
x - Choose digital port number. it is 0 to 50
Return value
0 or 1

Example 4-9
char x;

// Declare x variable for keeping reading input data

x = in(49);

// Read port 49 and store data to x variable.

Example 4-10
char x;

// Declare x variable for keeping reading input data

x = in(50);

// Read port 50 and store data to x variable.

4.3.2 out
Write or send the data to the specific digital port
Syntax

out(char _bit,char _dat)


Parameter
_bit - Choose digital port number. it is 0 to 50

Example 4-11
out(43,1);

// Write port 43 with logic 1

out(45,0);

// Write port 45 with logic 0

4.3.3 sw1_press
This function loops to check the SW1 pressing. It returns value after switch is released.
Syntax

void sw1_press()
Example 4-12
................
sw1_press();
................

// Wait until the SW1 is pressed and released

40 Robo-Creator : AT-BOT activity book

4.3.4 sw2_press
This function loops to check the SW2 pressing. It returns value after switch is released.
Syntax

void

sw2_press()

Example 4-13
..................
Sw2_press(); // Wait until the SW2 is pressed and released
.................

4.3.5 sw1
This function check the SW1 pressing in any time.
Syntax

char

sw1()

Return value
0 - SW1 is pressed
1 - SW1 is not pressed

Example 4-14
char x;

// Declare x variable for keeping the value

x = sw1();

// Get SW1 status and store to x variable

4.3.6 sw2
This function check the SW2 pressing in any time.
Syntax

char

sw2()

Return value
0 - SW2 is pressed
1 - SW2 is not pressed

Example 4-15
char x;

// Declare x variable for keeping the value

x = sw2();

// Get SW2 status and store to x variable

Robo-Creator : AT-BOT activity book 41

4.4 analog.h : Analog port library


This library file supports all instructions for reading the analog input port of the ATX
controller board. This library must be included at the top of the program with the command
#include as follows :
#include <analog.h> or #include <atx.h>

4.4.1 analog
This gets digital data from the analog to digital converter module of any analog
port; ADC0 to ADC7.
Syntax

unsigned int analog(unsigned char channel)


Parameter
channel - Analog input (ADC0 to ADC7)
Return value
Digital data from analog to digital converter module. The value is 0 to 1023 (in
decimal)

4.4.2 knob
This function gets data from ADC7 port. This port isconnected with variable resistor
on-board. It is called KNOB.
Syntax

unsigned int knob()


Return value
Digital data from analog to digital converter module. The value is 0 to 1023 (in
decimal)

Example 4-16
int val=0;

// Declare variable to keep the converted data

val

// Get data from analog input ch. 2 (ADC2)


// and store data to val variable.

= analog(2);

Example 4-17
int val=0;

// Declare variable to keep the converted data

val

// Get data from analog input ch. 7


// (ADC7 or KNOB) and store data to val
// variable

= knob();

42 Robo-Creator : AT-BOT activity book

4.5 motor.h : DC motor driving library


This library file supports all instructions for driving and controlling 6 DC motor outputs
of the ATX controller board. This library must be included at the top of the program with
the command #include as follows :
#include <motor.h> or #include <atx.h>

4.5.1 motor
It is DC motor driving function.
Syntax

void motor(char _channel,int _power)


Parameter
_channel - DC motor output of ATX board; value is 0 to 5
_power - Power output value; it is -100 to 100
If set _power as positive value (1 to 100); motor is driven one direction.
If set _power as negative value (-1 to -100); motor is driven opposite direction.
If _power as 0; motor is stop. This value is not recommended. Use motor_stop
function to stop motor better.

Example 4-18
motor(1,60);

// Drive motor ch.1with 60% of maximum power

motor(1,-60);

// Drive motor ch.1with 60% of maximum power and turn back

direction.

Example 4-19
motor(2,100);

// Drive motor ch.2 with maximum power

4.5.2 motor_stop
This function is driving off a motor or stop.
Syntax

void motor_stop(char _channel)


Parameter
_channel - DC motor output of ATX board; value is 0 to 5 and all (for driving off
all channels)

Example 4-20
motor_stop(1);

// Stop motor ch.1

motor_stop(4);

// Stop motor ch.4

Example 4-21
motor_stop(ALL);

// All motor are stop

Robo-Creator : AT-BOT activity book 43

4.6 servo.h : Servo motor library


This library file supports all functions for controlling 6 servo motor outputs of the ATX
controller board. This library must be included at the top of the program with the command
#include as follows :
#include <servo.h> or #inclue <atx.h>
There is one function. It is servo.

Syntax
void servo(unsigned char _ch, int _pos)

Parameter
_ch - Servo motor output (8 to 13)
_pos - Set the sevo motor shaft poistion (0 to 180 and -1)
If set to -1, disable selected servo motor output

4.7 sound.h : Sound generating library


This library file supports all functions for sound generating of the ATX controller
board and AT-BOT. This library must be included at the top of the program with the
command #include as follows :
#include <sound.h> or #inclue <atx.h>

4.7.1 beep
It is beep sound generating function. The beep frequency is 500Hz and 100
millisecond duration time.
Syntax

void beep()

4.7.2 sound
This is programmable sound generating function.
Syntax

void sound(int freq,int time)


Parameter
freq - Set frequency with value 0 to 32,767
time - Set duration time in millisecond unit from 0 to 32,767

Example 4-22
beep();

// Drives beep sound with 100 millisecond duration

sound(1200,500);

// Drives sound with 1200Hz 500 millisecond

44 Robo-Creator : AT-BOT activity book

4.8 serial.h : Serial data communication library


This library file supports all functions for sending and receiving the serial data via
UART port of the ATX controller board and AT-BOT. This library must be included at the top
of the program with the command #include as follows :
#include <serial.h> or #include <atx.h>

4.8.1 Hardware connection


UART0 port
UART0 port is connected via USB to Serial converter chip; FT232RL. For connecting
with computer, must connect via USB port on the ATX controller board. This connector is
same port for downloading.
UART1 port
Connect via RXD1 (port 2 ) and TXD1 (port 3)

4.8.2 uart
This is serial data sending function via UART0 port. The default baudrate is 115,200
bit per second.
Syntax

void uart(char *p,...)


Parameter
p - Type of data. Support the special character for setting display method.
Command

Operation

%c or %C

Display 1 character

%d or %D

Display the decimal value -32,768 to +32,767

%l or %L

Display the decimal value -2,147,483,648 to +2,147,483,647

%f or %F

Display floating point 3 digits

\r

Set the message left justify of the line

\n

Display message on the new line

4.8.3 uart_set_baud
This is baud rate setting function for UART0.
Syntax

void uart_set_baud(unsigned int baud)


Parameter
baud - Baud rate of UART0 2400 to 115,200

Example 4-23
uart_set_baud(4800);

// Set baud rate as 4,800 bit per second

Robo-Creator : AT-BOT activity book 45

4.8.3 uart_available
This is receiveing data testing function of UART0.
Syntax

unsigned char uart_available(void)


Return value
- 0 : no data received
- more than 0 : received character

Example 4-24
char x =uart_available();
// Check the recieving data of UART0.
// If x value is more than 0; it means UART0 get any data.
// Read it by using uart_getkey function in the order next immediately.

4.8.4 uart_getkey
This is data reading function from receivers buffer of UART0
Syntax

char uart_getkey(void)
Return value
- 0 : no data received
- data : received character in ASCII code

Example 4-25
#include <robot.h>
// Get function
void setup()
{
}
void loop()
// Main loop
{
if(uart_available())
// Check incoming data
{
if(uart_getkey()==a)
// Is key a pressed ?
{
lcd(Key a Active!); // Display message when get a
sleep(1000);
// Delay 1 second
}
else
{
lcd(#c);
// Clead display
}
}
}
Note : Default baud ratre of UART library is 115,200 bit per second. Data format
is 8-bit and no parity.

46 Robo-Creator : AT-BOT activity book

4.8.5 uart1
This is serial data sending function via UART1 port. The default baud rate is 9,600 bit
per second.
Syntax

void uart1(char *p,...)


Parameter
p - Type of data. Support the special character for setting display method. See
details in uart0 function.

4.8.6 uart1_set_baud
This is baud rate setting function for UART1.
Syntax

void uart1_set_baud(unsigned int baud)


Parameter
baud - Baud rate of UART0 2400 to 115,200

Example 4-26
uart1_set_baud(19200); // Set baud rate as 19,200 bit per second

4.8.7 uart1_available
This is receiving data testing function of UART0.
Syntax

unsigned char uart1_available(void)


Return value
- 0 : no data received
- more than 0 : received character

Example 4-27
char x =uart1_available();

// Check the receiving data of UART1.

4.8.8 uart1_getkey
This is data reading function from receivers buffer of UART1.
Syntax

char uart1_getkey(void)
Return value
- 0 : no data received
- data : received character in ASCII code

Robo-Creator : AT-BOT activity book 47

4.9 Digital compass library


It is compass.h file. This library file is not included in robot.h library file. Must include
the specific library file before using.
This library file supports all functions for interfacing the HMC6352 digital compass of
the ATX controller board. This library must be included at the top of the program with the
command #include as follows :
#include <compass.h>

4.9.1 compass_read
This reads the angle of the HMC6352 digital compass.
Syntax

int compass_read()
Return value
Angle value 0 to 359 defree

4.9.2 compass_set_heading
This is reference angle setting function. With this function, the current angle that
read from digital compass is set to 0 degree reference.
Syntax

void compass_set_heading()

4.9.3 compass_read_heading
This is reference angle reading function. Use this function after set the new reference
angle from compass_set_heading function.
Syntax

int compass_read_heading()
Return value
1 to 180 : positive angle (clock wise direction) of digital compass.
-1 to -180 : negative angle (Counter-Clockwise direction) of digital compass.

48 Robo-Creator : AT-BOT activity book

Robo-Creator : AT-BOT activity book 49

Chapter 5
The ATX controller board
hardware experiment
This chapter presents examples of the hardware experiment with the ATX controller
board of the Robo-Creator robotic kit. There are 7 experiments as follows.
Experiment 1 Shows message on the display of the ATX board
Experiment 2 Using SW1 and SW2 switch
Experiment 3 Reading analog from KNOB button of the ATX board
Experiment 4 Sound activity
Experiment 5 DC motors control
Experiment 6 Servo motor control
Experiment 7 Serial data communication with computers

50 Robo-Creator : AT-BOT activity book

Programming and Hardware experiment steps


(1) Open Wiring IDE and create a new sketch file.
(2) Type the code on the test editor of the sketch file
(3) Compile by click at the

button or choose at the menu Compile > Verify.

(4) Connect the ATX controller board with a USB port. Turn on power and wait until
the connection between the computer and ATX board is completed. This can be noticed
from the blue LED at the position of USB power on.
(5) Set the ATX board to program mode by pressing START switch and hold it for 3
seconds.
At the ATX board display, it shows messages

Robo-Creator
> Bootloader
and the red LED at the port 48 is powered on.
(6) Upload the code by click at the
Wiring hardware.

button or click at the menu file > Upload to

(7) Wait until the uploading is successful. Then press the START switch again. Finally,
the ATX controller circuit board will be running the latest uploaded program immediately.

Program L1-1

Robo-Creator : AT-BOT activity book 51

Experiment 1
Shows message on the display of the ATX board
Experiment 1.1 Simple message displaying on the ATX board
This experiment demonstrates the simle programming for showing the message at
the display of the ATX board.

Procedure
L1.1.1 Create the new sketch file. Type the Listing L1-1 and save as lcd_01.pde file
L1.1.2 Compile and upload the sketch to the ATX board.
L1.1.3 Run the program.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

At the LCD display of the ATX controller board show message Hello Robot! as follows.

Hel lo C Rob ot! > > > >


3

> o B o o t l o ad e r o a r d
2

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RESET

14

PC6

15 PC7

MOTOR

#include <atx.h>
// Include the main library
void setup()
{
lcd("Hello Robot!"); // Display mesage on the ATX display
}
void loop()
{}

Code explanation
This code runs within the setup function. There is one command. Display the message;
Hello Robot! on the screen. After that the program will jump to run in the loop function. No
any command in this function. The operation is stop finally.

Listring L1-1 : lcd_01.pde, the sketch file for simple displaying


message on the ATX board.

52 Robo-Creator : AT-BOT activity book

Experiment 1.2 Display message 2 lines of the ATX board


display
This experiment demonstrates the displaying message 2 lines of the ATX board
display.

Procedure
L1.2.1 Create the new sketch file. Type the Listing L1-2 and save as lcd_02.pde file
L1.2.2 Compile and upload the sketch to the ATX board.
L1.2.3 Run the program.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

At the LCD display of the ATX controller board show message as follows :

Lin e1 C Rob ot! > > > >


Lin e2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

10 11 12 13
S

BATTERY LEVEL
+

7.2-9V BATT.

RESET

#include <atx.h>
void setup()
{}
void loop()
{
lcd(Line1#nLine2);
}

14

PC6

15 PC7

MOTOR

// Include the amin library

// Display message 2 lines

Code explanation
The program starts running in the setup function then it repeats working in the loop function
to display message on both lines of the LCD. The result comes from the operation of control
code #n. The overall results have been shown the display of 2-line text.

Listring L1-2 : lcd_02.pde, the sketch file for displaying message 2


lines on the ATX board display.

Robo-Creator : AT-BOT activity book 53

Experiment 1.3 Shows message and number


This experiment demonstrates showing messages mixed with numbers at the ATX
board display. It shows the values of counting in every 1 second.

Procedure
L1.3.1 Create the new sketch file. Type the Listing L1-3 and save as lcd_03.pde file
L1.3.2 Compile and upload the sketch to the ATX board.
L1.3.3 Run the program.
At the LCD display of the ATX controller board show message as follows :

Count: xxx

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

therefore xxx is counting value that increse every second.

Cou nt:R20 0t! > > > >


Lin e2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

10 11 12 13
S

BATTERY LEVEL
+

7.2-9V BATT.

RESET

14

PC6

15 PC7

MOTOR

#include <atx.h>
// Include the main library
int i = 0;
// Declare the counting variable
void setup()
{}
void loop()
{
lcd("Count: %d ",i); // Display the counter on the ATX board display
sleep(1000);
// Delay 1 second
i++;
// Increase counter
}

Code explanation
The program starts running in the setup function and then it repeats working in the loop
function. It shows the counting value that increased every 1 second. The variable i stores the
count values.

Listring L1-3 : lcd_03.pde, the sketch file for displaying


message and number on the ATX board.

54 Robo-Creator : AT-BOT activity book

Experiment 2
Using SW1 and SW2 switch
Experiment 2.1 Using SW1 to start the counter
This expeirment demonstrates how to use the SW1 on the ATX controller board to
start the counter. The counting values also should be display on the ATX board display.

Procedure
L2.1.1 Create the new sketch file. Type the Listing L2-1 and save as switch_01.pde file
L2.12 Compile and upload the sketch to the ATX board.

#include <atx.h>
int i=0;
void setup()
{
lcd("SW1 Press!");
sw1_press();
lcd("#c");
}
void loop()
{
lcd("Count: %d ",i);
sleep(1000);
i++;
}

// Include the main library


// Declare the counter variable

// Display the title message


// Wait the SW1 pressing
// Clear display before show the next message

// Display the counting value


// Delay 1 second
// Increase counter

Code description
The program starts running in the setup function to wait for the pressing of SW1 and
there is an alert by the text of SW1 Press! at the display after the switch is pressed. The
program will repeat working in the loop function to display the count value that is increased
in every 1 second. The i variable is used to stores the count values.

Listing L2-1 : switch_01.pde, the sketch file for checking the SW1
of the ATX board pressing to start the counter

Robo-Creator : AT-BOT activity book 55

L2.1.3 Run the program.


At the LCD display of the ATX controller board show message as follows :

SW1 Press!
L2.1.4 Press the SW1 on the ATX board and release.
Counting is start. The counter value is displayed on the ATX board display as follows.

Count: xxx

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

therefore xxx is counting value that increase every second.

Cou nt:R20 0t! > > > >


Lin e2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

Press the switch


SW1 to start

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RES ET

14 PC6

15 PC7

MOTOR

Experiment 2.2 Checking the SW1 and SW2 pressing anytime


This experiment demonstrates the checking of SW1 and SW2 of the ATX board
pressing. It used to increase and decrease the variable value. Also display the value on
the LCD module of ATX board.

Procedure
L2.2.1 Create the new sketch file. Type the Listing L2-2 and save as switch_02.pde file
L2.2.2 Compile and upload the sketch to the ATX board.
L2.2.3 Run the program.
At the LCD display of the ATX controller board show message as follows :

Count: xxx
therefore xxx is the counting value. Start from 10.

56 Robo-Creator : AT-BOT activity book

#include <atx.h>
int i=10;

// Include the main library


// Declare the counter variable
// and set to start from 10

void setup()
{}
void loop()
{
lcd("Count: %d ",i); // Display the counting value
if(sw1()==0)
// SW1 is pressed ?
{
i++;
// If SW1 is pressed, increase counter.
sleep(200);
// Delay for switch debouncing
}
if(sw2()==0)
// SW2 is pressed ?
{
i-- ;
// If SW2 is pressed, decrease counter
sleep(200);
// Delay for switch debouncing
}
}
Code description
The program begins operating in the setup function. Then it repeats in the loop function.
It loop to check up pressing the switch of SW1 and SW2 all the time and display the count
values of the variable I at the LCD module as well.
Conditions of verification in the loop as following these:
1. If the SW1 is pressed ( sw1 ( ) function returns the value as 0)
The program responds by increase value of the variable i
2. If the SW2 is pressed (sw2 ( ) function returns the value as 0 )
The program responds by decrease value of the variable i.

Listing L2-2 : switch_02.pde, the sketch file for checking both SW1
and SW2 of the ATX board pressing anytime

Robo-Creator : AT-BOT activity book 57

L2.2.4 Press the SW1 switch on the ATX board. Observe the operation of the ATX board
display.
Each time you press the SW1, the count value is added up one value
L2.2.5 Press the SW2 switch on the ATX board. Observe the operation of the ATX board
display.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

Each time you press the SW2, the count value is deduct one value.

Count:R20 0t! > > > >


Line2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RES ET

14

PC6

15 PC7

MOTOR

Press SW1 to increase value

Press SW2 to
decrease value

58 Robo-Creator : AT-BOT activity book

Experiment 3
Reading analog from KNOB button of the ATX board
Experiment 3.1 Knob value reading
This experiment demonstrates how to read the analog value from KNOB button of
the ATX controller board. It is basic example of analog sensor reading. The result is 0 to
1023.

Procedure
L3.1.1 Create the new sketch file. Type the Listing L3-1 and save as knob_01.pde file
L3.1.2 Compile and upload the sketch to the ATX board.
L3.1.3 Run the program.
At the LCD display of the ATX controller board show message as follows :

KNOB: xxx
therefore xxx as KNOB position value from 0 to 1023
- Adjust to last left position. Value is 0.
- Adjust to last right position. Value is 1023.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

- Adjust to center position. Value is 512.

KNO B::101 0t! > > > >


Lin e2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RES ET

14

PC6

15 PC7

MOTOR

Adjust the KNOB


shaft to reading

Robo-Creator : AT-BOT activity book 59

#include <atx.h>
void setup()
{}
void loop()
{
lcd("KNOB: %d
sleep(100);
}

// Include the main library

",knob());

// Display the KNOB value


// Delay 0.1 second for displaying

Code explanation
The program begins operating in the setup function. Then it repeats in the loop function
to display the readable values from KNOB at the LCD module.
Knob value is read by using knob function of the atx.h library. It is analog to digital
converter function.

Listing L3-1 : knob_01.pde, the sketch file for reading the KNOB
button of the ATX board value.

Experiment 3.2 Use KNOB button to Mode selector


This experiment demonstrates about programming to use KNOB as the directional
determinant of counting value. If KNOB is adjusted to the left to compare with the middle
position, this will be the selection to count down values but if it is opposite, it will be the
selection to count values up. Additionally, the display of the count value is shown at the
LCD module.

Procedure
L3.2.1 Create the new sketch file. Type the Listing L3-2 and save as knob_02.pde file
L3.2.2 Compile and upload the sketch to the ATX board.
L3.2.3 Run the program.
At the LCD display of the ATX controller board show message as follows :

Count: xxx
Count Up
in the Count up mode or

Count: xxx
Count Down
in the Count down mode
therefore xxx is thge counting value. Start from 100.

60 Robo-Creator : AT-BOT activity book

#include <atx.h>
int i=100;
int k;
void setup()
{}
void loop()
{
lcd("Count: %d
k = knob();
if(k>512)

// Include the main library


// Declare the counter variable. Start from 100
// Declare the KNOB value variable

",i);

//
//
//
//

Display counter
Read the KNOB value to store to the k variable
Check the KNOBs value is through
the middle to right or not ?

{
i++;
lcd("#nCount Up

");

}
else
{
i--;
lcd("#nCount Down
}
sleep(1000);

");

// Increase the count value


// Display the Count up mode message
// on the lower line of LCD

//
//
//
//

If the KNOBs value is through


the middle to left, count down
Display the Count down mode message
on the lower line of LCD

// Delay 1 second

Code explanation
The program begins operating in the setup function. Then it repeats in the loop function
to verification of adjustment position of KNOB continually. At the same time, the count value of
the variable i and the counting mode at the LCD module as well. There are conditions for the
verification as follows:
1. If the KNOB value is greater than 512
The program will respond to adding up the count value of the variable i to one value
and show the Count up mode message on the display.
2. If the KNOB value is less than 512 (working in the section of else).
The program will respond to deducting the count values of the variable i to one value
and show the Count down mode message on the display.

Listing L3-2 : knob_02.pde, the sketch file for using the KNOB button
of the ATX board to Mode selector

Robo-Creator : AT-BOT activity book 61

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

The program wil count up and down depending on the value of KNOB, which is
result from the rotation of spindle of the variable resistor at the position of KNOB of ATX
board.

Count:120 0t! > > > >


Count t Up a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

10 11 12 13
S

BATTERY LEVEL
+

7.2-9V BATT.

RES ET

14

PC6

15 PC7

MOTOR

Adjust the KNOB shaft to


change the counting mode
Left direction to count
down mode
Right direction to count
up mode

L3.2.4 Adjust the KNOB shaft to go through the middle to the left (or may adjust to the left)
This will be found that the counter is in Count down mode. The count value will be
deducted 1 value in each second.
L 3.2.5 Adjust the KNOB shaft to go through the middle to the right (or maybe adjust to the
far right)
The counter is in Count up mode. The count value will be added up 1 value in
each second.

62 Robo-Creator : AT-BOT activity book

Experiment 4
Sound activity
Exeperiment 4.1 The signal selector
This experiment demonstrates about using the SW1 and SW2 on the ATX controller
board to generate the different sound frequency. If SW1 is pressed, ATX board drives
500Hz signal with 0.1 second duration. If the SW2 is pressed, it generate 2000Hz (2kHz)
signal with 0.5 second instead.

Procedure
L4.1.1 Create the new sketch file. Type the Listing L4-1 and save as sound_01.pde file
L4.1.2 Compile and upload the sketch to the ATX board.
L4.1.3 Run the program. Press the SW1 on the ATX controller board.
Everytime to press the switch SW1, you will hear the sound with the frequency of
500 Hz for 0.1 second from the piezo speaker on the ATX board.
L 4.1.4 Press the switch SW2

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

Everytime to press the SW2, you will hear the sound of frequency 2000Hz for 0.5
second.

Cou nt:R20 0t! > > > >


Lin e2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RES ET

14 PC6

15 PC7

MOTOR

Press SW1 to generate 500Hz signal

Press SW2 to
generate 2000Hz
signal

Robo-Creator : AT-BOT activity book 63

#include <atx.h>
void setup()
{
}
void loop()
{
if(sw1()==0)
{
beep();
sleep(100);
}
if(sw2()==0)
{
sound(2000,500);
sleep(100);

// Include the main library

// The SW1 is pressed ?


// If SW1 is pressed,
// generate the 500Hz signal 0.1 second duration
// Delay for switch debouncing
// The SW2 is pressed ?
// If SW2 is pressed,
// generate the 2000Hz signal 0.5 second duration
// Delay for switch debouncing

}
}

Code explanation
The program operates in the loop function to check the pressing of the SW1 and SW2
switches and there are conditions as follows.
1. If the SW1 is pressed (sw1( ) function returns the value as 0)
The program will respond to generate the 500Hz signal for 0.1 second.
1. If the SW2 is pressed (sw2( ) function returns the value as 0)
The program will respond to generate the 2000Hz signal for 0.5 second.

Listing L4-1 : sound_01.pde, the sketch file for using the SW1 and
SW2 on the ATX board to set the condition for signal generating

64 Robo-Creator : AT-BOT activity book

Experiment 4.2 Tuning frequency by KNOB


This experiment demonstrates about using KNOB button to adjust the sound
frequency and show the frequency value at the ATX board display

Procedure
L4.2.1 Create the new sketch file. Type the Listing L4-2 and save as sound_02.pde file
L4.2.2 Compile and upload the sketch to the ATX board.
L4.2.3 Run the program.
At the LCD display of the ATX controller board show message as follows :

Freq: xxx Hz
therefore xxx is the generated signal frequency
L4.2.4 Asjust the KNOB button slowly from left to right direction. See the display operation
and listen the sound signal from ATX board.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

The frequency of the sound displaying at the LCD module will increase from 0 to
2046Hz (as 2 times of the value read from KNOB). The sound you have listen will be
dramatically sharpened following the adjustment of frequency increasing.

Freq::120 0tHz > > >


Line2 t l o a d e r o a r d

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RES ET

14

PC6

15 PC7

MOTOR

Adjust the KNOB button to change the sound frequency

Robo-Creator : AT-BOT activity book 65

#include <atx.h>

// Include the main library

int k;

// Declare the KNOB value variable

int f;

// Declare the frequency variable

void setup()
{
}
void loop()
{
k = knob();

// Read the KNOB value to store in the k variable

f = 2*k;

// Increase the value 2 times

lcd("Freq: %d Hz

",f);

// Display the sound frequency

sound(f,200);

// Generate the sound signal


// from the variable data for 0.2 second

sleep(1000);

// Delay 1 second

Code explanation
The program starts working in the setup function and repeats to work in the loop function
to repeat reading from the KNOB button. Then, the value you have got multiply by 2 to use for
the frequency value desired so the value will be in the range of 0 to 2046Hz and the frequency
value will be shown at the LCD module.
For the sound generation at the Piezo speaker, the program will space in each second
briefly.

Listing L4-2 : sound_02.pde, the sketch file for using the KNOB button
on the ATX board to adjust the sound signal frequency

66 Robo-Creator : AT-BOT activity book

Experiment 5
DC motors control
Experiment 5.1 Direction control for DC motor driver
This experiment demonstrates about direction control for DC motor driver output 0
and 1 of the ATX board. The motor driver will be drive DC motor forward and backward
every 3 seconds continually.

Hardware connection
Connect the DC motor #1 with Motor-0 output of the ATX board.
Connect the DC motor #2 with Motor-1 output of the ATX board.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

DC motor #2

ATX1.0 r >> >>>> >>>


controll er R board
USB DATA

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RESET

14

PC6

15

PC7

MOTOR

DC motor #1

Procedure
L5.1.1 Create the new sketch file. Type the Listing L5-1 and save as motor_01.pde file
L5.1.2 Compile and upload the sketch to the ATX board.
L5.1.3 Run the program.
DC motor at output 0 and 1 start and reverse direction every 3 seconds.

Robo-Creator : AT-BOT activity book 67

#include <atx.h>

// Include the main library

void setup()
{
}
void loop()
{
motor(0,70);

// Drive motor-0 with 70% power

motor(1,70);

// Drive motor-1 with 70% power

sleep(3000);

// Delay 3 seconds before change the direction

motor(0,-70);

// Drive motor-0 backward with 70% power

motor(1,-70);

// Drive motor-1 backward with 70% power

sleep(3000);

// Delay 3 seconds before change the direction

Code explanation
The program repeats to work in the loop function to drive the DC motor of the channel 0
and 1 simultaneously with 70% power equally both. And every 3 seconds reversing direction of
rotation will be operated continuously.

Listing L5-1 : motor_01.pde, the sketch file for direction control of


DC motor driver of the ATX board. DC motor will reverse direction
every 3 seconds continually

Experiment 5.2 Timing control motor


In this experiment demonstrates the programming for driving both DC motor at the
Motor-0 and 1 output of the ATX board with timing control. Both motors will operate 3
seconds and then stop 3 seconds alternately and continuously.

Hardware connection
Connect the DC motor #1 with Motor-0 output of the ATX board.
Connect the DC motor #2 with Motor-1 output of the ATX board.

Procedure
L5.2.1 Create the new sketch file. Type the Listing L5-2 and save as motor_02.pde file
L5.2.2 Compile and upload the sketch to the ATX board.
L5.2.3 Run the program.
DC motors at output 0 and 1 start and stop 3 seconds alternately and continually.

68 Robo-Creator : AT-BOT activity book

#include <atx.h>

// Include the main library

void setup()
{
}
void loop()
{
motor(0,90);

// Drive motor-0 with 90% power

motor(1,90);

// Drive motor-1 with 90% power

sleep(3000);

// Delay 3 seconds

motor_stop(0);

// Stop motor-0

motor_stop(1);

// Stop motor-1

sleep(3000);

// Delay 3 seconds

Code explanation
The program repeats to work in the loop function to drive the DC motor at channel 0 and
1 simultaneously with the 90% power driving equally both. After 3 seconds, all motors will stop
for 3 seconds also and start to rotate again. This will work together seamlessly.

Listing L5-2 : motor_02.pde, the sketch file for timimg control of DC


motor driver of the ATX board. DC motor will start and stop every 3
seconds continually

Robo-Creator : AT-BOT activity book 69

Experiment 6
Servo motor control
Experiment 6.1 Control position of the servo motor
This experiment demonstrates about programming to control the servo motor shaft
position. The servo motor is driven to move the shaft to 60 degrees position and stop to
lock at this position for 5 minutes. Next, change to the position to 120 degrees and stop to
lock at this position for 5 minutes as well. Then, the shaft will be moved to the position of 60
degrees again so that the positions will be switched back and forth like this constantly.

Hardware connection

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

Connect a standard servo motot to servo motor output port 12 of the ATX board

Servo motor

ATX 1.0 r > > >>>> > >>


3

con troll er R boa rd


USB DATA

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13

STANDARD
SERVO MOTOR

7.2-9V BATT.

RESET

14

PC6

15

PC7

MOTOR

Procedure
L6.1.1 Create the new sketch file. Type the Listing L6-1 and save as servo_01.pde file
L6.1.2 Compile and upload the sketch to the ATX board.
L6.1.3 Run the program.
The servo motor shaft will move between 60 and 120 degrees position every 5
seconds

70 Robo-Creator : AT-BOT activity book

#include <atx.h>

// Include the main library

void setup()
{}
void loop()
{
servo(12,60);

// Drive the servo motor at port 12 to move its shaft to


// 60 degrees position

sleep(5000);

// Delay 5 seconds

servo(12,120);

// Drive the servo motor at port 12 to move its shaft to


// 120 degrees position

sleep(5000);

// Delay 5 seconds

Code explanation
The program repeats to work in the loop function to drive a servo motor to move its shaft
between the position of 60 and 120 degrees in every 5 seconds.

Listing L6-1 : servo_01.pde, the sketch file for driving a servo motor
to control the movement position

Experiment 6.2 Switch-controlled the servo motor position


This experiment demonstrates about control the servo motor shaft position by 2 of
switch on the ATC board. The SW1 is used to increase the position angle ans SW2 is used to
decrease the position angle.

Hardware connection
Connect a standard servo motot to servo motor output port 12 of the ATX board

Procedure
L6.2.1 Create the new sketch file. Type the Listing L6-2 and save as servo_02.pde file
L6.2.2 Compile and upload the sketch to the ATX board.
L6.2.3 Run the program.
At the LCD display of the ATX controller board show message as follows :

Servo: xxx
therefore xxx is servo motor shaft position. Start at 90

Robo-Creator : AT-BOT activity book 71

#include <atx.h>
int p=90;

// Include the main library


// Declare the position variable
// and set the default value as 90

void setup()
{}
void loop()
{
servo(12,p);
lcd("Servo: %d
if(sw1()==0)
{
p++;
if(p>180)
{
p=0;
}
sleep(100);
}
if(sw2()==0)
{
p-- ;
if(p<0)
{
p=0;
}
sleep(100);

",p);

//
//
//
//

Drive a servo motor to move the shaft to position


that defined by the p variable
Show the current position of servo motor shaft
Is the SW1 switch pressed ?

// Increase the position value


// Is the position more than 180 ?
// If the position value more than 180,
// set the position value back to 0
// Delay 0.1 second
// Is the SW2 switch pressed ?
// Decrease the postion value
// Is the position lower than 0 ?
// If the position value lower than 0,
// set the position value as 0
// Delay 0.1 second

}
}

Code explanation
The program repeats to work in the loop function to drive a servo motor to move its shaft
to the position stored in the variable p along with displaying a position value at the LCD module.
Furthermore, there are conditions of verification within the loop as follows:
1. If the SW1 switch is pressed (sw1( ) function returns the value as o)
The program responds by adding the p variable value to 1 value and check that
exceed 180 or not. If it is exceeded, set the position to start with the new 0. Next, time delay will
proceed to not cause the addition of values too fast.
2. If the SW2 switch is pressed (sw2( ) function returns the value as 0)
The program responds by reducing the p variable value to 1 value and verifies that
the value is below 0 or not. If it is, set the position to start with the new 0 and then time delay.

Listing L6-2 : servo_02.pde, the sketch file for driving a servo motor
to control the movement position by SW1 and SW2 switch on the
ATX board

72 Robo-Creator : AT-BOT activity book

L6.2.4 Press the SW1 switch


The shaft position of the servo motor will be increased 1 value. At the same time,
the servo motor shaft will be moved according to the increasing value. When adding the
value to 180, the position value will be back to start at the new 0.
L6.2.5 Press the SW2 switch
The shaft position of the servo motor will be decreased 1 value. At the same time,
the servo motor shaft will be moved according to the decreasing value. When the value
is lower than 0, the position value will be back to start at the new 0.

Robo-Creator : AT-BOT activity book 73

Experiment 7
Serial data communication with computers
Experiment 7.1 Transmit the serial data to computer
This experiment demonstrates about computer interfacing of the ATX controller
board. Begins with transmitting the serial data to USB port via the USB to serial converter
circuit on the ATX board. The data will be display on the Serial Monitor of Wiring IDE.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

Connect to USB port

ATX 1.0 r > > >>>> >>>


con troll er R board
USB DATA

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RESET

14

PC6

15 PC7

MOTOR

Procedure
L7.1.1 Create the new sketch file. Type the Listing L7-1 and save as uart_01.pde file
L7.1.2 Compile and upload the sketch to the ATX board. Still connect the USB cable with
USB port of computer.

74 Robo-Creator : AT-BOT activity book

#include <atx.h>

// Include the main library

void setup()
{}
void loop()
{
uart("Hello Robot!\r\n");

// Transmit the serial data


// to computer with carrier return

sleep(2000);

// Delay 2 seconds

Code explanation
The program reapeats working in the loop function to transmit some text Hello Robot! to
display on the Serial Monitor of the computer every 2 seconds continuously.

Listing L7-1 : uart_01.pde, the sketch file for transmitting the serial
data to computer of the ATX controller board
L7.1.3 Run the program. Click on the

button to open the Serial Monitor of Wiring IDE

The Serial Monitor window appears and dsiplay message Hello Robot! every 2
seconds.

Robo-Creator : AT-BOT activity book 75

Experimenrt 7.2 KNOB data monitoring


This experiment demonstrates about transmitting the serial data from reading
theKNOB value of the ATX board to shows on the Serial Monitor. It is the simple sensor data
monitoring application.

Procedure
L7.2.1 Create the new sketch file. Type the Listing L7-2 and save as uart_02.pde file
L7.2.2 Compile and upload the sketch to the ATX board. Still connect the USB cable with
USB port of computer.
L7.2.3 Run the program. Click on the

button to open the Serial Monitor of Wiring IDE

L7.2.4 Adjust the KNOB button and see the operation at the Serial Monitor of Wiring IDE.
The Serial Monitor displays message KNOB: xxx every 0.1 second. Therefore xxx
value us 0 to 1023.

#include <atx.h>
void setup()
{}
void loop()
{
uart("KNOB: %d
sleep(100);

// Include the main library

\r\n",knob()); // Transmit the serial data


// to computer with carrier return
// Delay 0.1 second

Code explanation
The program repeats to work in the loop function to transmit the KNOB value to KNOB to
display on the Serial Monitor of the computer every 0.1 seconds continuously.

Listing L7-2 : uart_02.pde, the sketch file for transmitting the KNOB
button of the ATX board to computer

76 Robo-Creator : AT-BOT activity book

Experiment 7.3 Receiving the serial data from computer


In this experiment presents about receiving the data from computer via USB port.
The data will be transmitted out from computer by inputing with the SErial Monitor window
of Wiring IDE to control the sound generation of the ATX board.

Procedure
L7.3.1 Create the new sketch file. Type the Listing L7-3 and save as uart_03.pde file
L7.3.2 Compile and upload the sketch to the ATX board. Still connect the USB cable with
USB port of computer.
L7.3.3 Run the program. Click on the

button to open the Serial Monitor of Wiring IDE

L7.3.4 Type character of number 1 at the transmit box. Choose the serial data parameter
box to No line ending. After that, click on the Send button of the Serial Monitor.

L7.3.5 Observe the operation of the ATX controller board.


The ATX board receives the number 1 data to generates the 500Hz signal with 0.1
second.
7.3.6 Type character of number 2 at the transmit box then click on the Send button of the
Serial Monitor.

L7.3.7 Observe the operation of the ATX controller board.


The ATX board receives the number 2 data to generates the 2000Hz signal with 0.5
second.

Robo-Creator : AT-BOT activity book 77

#include <atx.h>
char c;
void setup()
{}
void loop()
{
if(uart_available())
{
c = uart_getkey();
if(c=='1')
{
beep();
}
if(c=='2')
{
sound(2000,500);
}
}
}

// Include the main library


// Declare the data variable

// Check the serial data receiving


// Store the serial data to the data variable
// Is it number 1 data ?
// If yes, generate the 500Hz signal with 0.1 second
// Is it number 2 data ?
// If yes, generate the 2000Hz signal with 0.5 second

Code explanation
The program repeats to work in the loop function to receive the serial data from computer.
After the receiving occur, it will be stored data to the c variable and check the value as follows :
1. If the data is the number 1 (from pressing the key 1)
The program will respond by generating the 500Hz signal for 0.1 second.
2. If the data is the number 2 (from pressing the key 2)
The program will respond by generating the 2000 Hz signal for 0.5 second.
3. If it is another data
No response

Listing L7-3 : uart_03.pde, the sketch file for receiving the serial data
from computer of the ATX board

78 Robo-Creator : AT-BOT activity book

Robo-Creator : AT-BOT activity book 79

Chapter 6
AT-BOT movement
After try out with some experiment to verify the operation of the hardware in the
chapter 5. Next, it will be the part of understanding to the mechanical structure of AT-BOT
robots and how to move AT-BOT robots. The movement of AT-BOT robots is different from
familiar two wheeled mobile robots because an AT-BOT has 4 sets of DC motor gearbox
and spike wheels. Therefore, programming to drive robots also needs to be considered
the functions of all 4 motors. In this chapter, the content will be presented with examples
of programming under C/C++language of Wiring to manage AT-BOT to move in the basic
patterns, either moving straight, backward and turning or turning around in various ways.

Left front wheel


Motor-0

Right front wheel


Motor-3

ON
42 ADC2
41 ADC1
40 ADC0

ADC7
SW1

46 ADC6

10 11 12 13

SW2
48

MOTOR

44 ADC4

50

PC7

BATTERY LEVEL

45 ADC5

49

PC6

START

Left back wheel


Motor-1

43 ADC3

SERVO PORT

14
15

ATX 1.0 r > > >>>> > >>


con troll er R boa rd

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

8
+

USB DATA

E2

RESET

7.2-9V BATT.

Right back wheel


Motor-2

Figure 6-1 : Illustration of determination of motors and wheels


positions in an AT-BOT

80 Robo-Creator : AT-BOT activity book

6.1 Mechanical structure of AT-BOT


An AT-BOT robot has 4 sets of driving wheels and they are arranged according to
the positions as in the figure 6-1. Generally, driving AT-BOT robots with all 4 sets of driving
wheels can be efficient based on one factor you need to consider. The factor is the
friction between touching surface and wheels of a robot. In AT-BOT, we will use the 48:1
DC motor gearboxes and splike wheels. This will make motion good and fast on virtually all
surfaces. Torque obtained from the motor sets will be a lot and enough to drive a robot
moving up the slop of 20 degrees or rough surfaces.
Connection of each motor with a DC motor driving circuit on the ATX board of an
AT-BOT robot detailed as follows
The left front wheel is connected with the Motor-0 output.
The left back wheel is connected with the Motor-1 output
The right back wheel is connected with the Motor-2 output
The right front wheel is connected with the Motor-3 output

6.2 Principles of the movement of AT-BOT


6.2.1 Moving forward
To drive an AT-BOT robot to move forward can be done by controlling the DC
motor gearboxes of all 4 wheels to turn in the direction that forces the robot to move
forward with a stable driving power, such as driving with the power of 70% or 100%, etc. to
not cause turning around.

43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW2

45 ADC5

49
48

MOTOR

44 ADC4

50

PC7

15

BATTERY LEVEL

PC6

C r e at o r e > >>> > > > >

SW1

c o n tr o l l er Rb o a r d

ADC7

10 11 12 13

44 ADC4

KNOB

45 ADC5

SERVO PORT

46 ADC6

40 ADC0

48

MOTOR

USB DATA

41 ADC1

50

PC7

7.2-9V BATT.

42 ADC2

SW2

15

BATTERY LEVEL

START

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

43 ADC3

49

PC6

C r e at o r e > >>> > > > >

14

SW1

c o n tr o l l er Rb o a r d

ADC7

10 11 12 13

SERVO PORT

9
S

E2

RESET

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

ON

ON
E2

RESET

7.2-9V BATT.

START

Robo-Creator : AT-BOT activity book 81

6.2.2 Moving backwards

To drive an AT-BOT robot to move backwards can do by handling the DC motor


gearbox of all 4 wheels to turn in the direction that forces the robot to move backwards
with a stable driving power.

ON

ON

E2

43 ADC3

42 ADC2

41 ADC1

40 ADC0

46 ADC6

45 ADC5

ADC7

14

SW1

46 ADC6

10 11 12 13

SERVO PORT
9

40 ADC0

49

PC6

SW2

48

MOTOR

BATTERY LEVEL

SW2

15 PC7

48

MOTOR

BATTERY LEVEL

50

44 ADC4

15 PC7

45 ADC5

49

PC6

50

44 ADC4

C r e a to r e > >> > > >> >

41 ADC1

KNOB

c o n t ro l l er R b o ar d

42 ADC2

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

43 ADC3

C r e a to r e > >> > > >> >

+
14

SW1

c o n t ro l l er R b o ar d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

ADC7

10 11 12 13

SERVO PORT

KNOB

5
4
3
2

ON

ON

USB DATA

E2

RESET

USB DATA

E2

RESET

43 ADC3

42 ADC2

41 ADC1

40 ADC0

46 ADC6

45 ADC5

ADC7

10 11 12 13

SERVO PORT

40 ADC0

ADC7

14

SW1

46 ADC6

10 11 12 13

14

SW1

49

PC6

SW2

SW2

15 PC7

48

MOTOR

48

MOTOR

BATTERY LEVEL

50

44 ADC4

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

50

44 ADC4

C r e a to r e > >> >> > > >

41 ADC1

KNOB

c o n t ro l l er R bo a r d

42 ADC2

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

43 ADC3

C r e a to r e > >> >> > > >

SERVO PORT

KNOB

c o n t ro l l er R bo a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

USB DATA

RESET

USB DATA

E2

RESET

START

START

Turning
point

7.2-9V BATT.

7.2-9V BATT.

START
START

1
0
5
4
3
2

1
0

7.2-9V BATT.
7.2-9V BATT.

6.2.3 Turning to the left

There are two formats of the movement, including turning with two wheels and
turning around.

6.2.3.1 Turning to the left with two wheels

This movement uses 2 sets of motors and wheels in driving an AT-BOT to turn left by
controlling the right front wheel and the right back wheel to rotate forward. Meanwhile,
the left front wheel and the left back wheel are stopped, so the robot will be able to turn

left and the rotation point is between the left front wheel and the left back wheel as the
diagram below.

82 Robo-Creator : AT-BOT activity book

6.2.3.2 Turning to the left by spining


To drive an AT-BOT robot to turn left with another pattern is available by managing
the right front wheel and the right back wheel turning forward but the left front wheel and
the left back wheel will turn backwards or in the opposite direction. Therefore, AT-BOT
robot will turn left with spining and the turning point is at the middle of the robots body.
Turning itself to the left by this method will give a high power of the movement but the
robot needs more energy as well.

43 ADC3
42 ADC2
41 ADC1
40 ADC0

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

Turning
point

46 ADC6

SW1

14

44 ADC4

C r e a to r e > >>> > > > >

ADC7

SERVO PORT

c o n t ro l l er Rb o a r d

KNOB

10 11 12 13

45 ADC5

46 ADC6

40 ADC0

48

USB DATA

41 ADC1

50

MOTOR

7.2-9V BATT.

42 ADC2

SW2

15 PC7

BATTERY LEVEL

START

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

43 ADC3

49

PC6

C r e a to r e > >>> > > > >

14

SW1

c o n t ro l l er Rb o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

10 11 12 13

ADC7

SERVO PORT

9
S

E2

RESET

USB DATA

KNOB

ON

ON
E2

RESET

7.2-9V BATT.

START

6.2.4 Turning to the right


There are 2 formats of the movement, including turning by 2 wheels and spining.

6.2.4.1 Turning to the right by two wheels


To make AT-BOT robot move in this pattern of turning to the right will be done
differently from turning left. It is said that controlling the left front wheel and the left back
wheel to turn forward but the right front wheel and the right back wheel to stop. Therefore,
the robot will be able to turn left and has the rotation point between the right front wheel
and the right back wheel.

43 ADC3
42 ADC2
41 ADC1
40 ADC0

SW2

44 ADC4

50
48

MOTOR

BATTERY LEVEL

45 ADC5

49

Turning
point

46 ADC6

SW1

PC6

C r e a t o re > >> > > >> >

ADC7

14
15 PC7

c o n t r o ll er R b o ar d

KNOB

44 ADC4

9 10 11 12 13

45 ADC5

SERVO PORT

46 ADC6

40 ADC0

48

USB DATA

41 ADC1

50

MOTOR

7.2-9V BATT.

42 ADC2

SW2

15 PC7

BATTERY LEVEL

START

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

43 ADC3

49

PC6

C r e a t o re > >> > > >> >

14

SW1

c o n t r o ll er R b o ar d

ADC7

SERVO PORT

9 10 11 12 13
S

E2

RESET

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

ON

ON
E2

RESET

7.2-9V BATT.

START

Robo-Creator : AT-BOT activity book 83

6.2.4.2 Turning to the right by spining


This driving will be done in the opposite way with turning to the left of the pattern 2
(topic 6.2.4). By controlling the left front wheel and the left back wheel to turn forward but
the right front wheel and the right back wheel to be turn backward. The AT-BOT will turn
right with spining and the turning point is at the middle of the robots body. With this method
will give a high power in the movement but need to use more energy as well.

43 ADC3
42 ADC2
41 ADC1
40 ADC0

SW2

44 ADC4

50
48

MOTOR

BATTERY LEVEL

45 ADC5

49

Turning
point

46 ADC6

SW1

PC6

C r e a to r e > >>> > > > >

ADC7

SERVO PORT

14
15 PC7

c o n t ro l l er Rb o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

44 ADC4

10 11 12 13

45 ADC5

46 ADC6

48

40 ADC0

50

MOTOR

USB DATA

41 ADC1

SW2

15 PC7

BATTERY LEVEL

7.2-9V BATT.

42 ADC2

49

PC6

START

E2

43 ADC3

14

SW1

C r e a to r e > >>> > > > >

ADC7

10 11 12 13

c o n t ro l l er Rb o a r d

KNOB

SERVO PORT

RESET

USB DATA

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

ON

ON
E2

RESET

7.2-9V BATT.

START

84 Robo-Creator : AT-BOT activity book

Experiment 8
Wheel driving setting
This experiment must be done first for programming to control AT-BOT to move around
by presenting the adjustment step of the motor driving to test the basic movement.
AT-BOT are defined all motor connections as follows:
The left front wheel is connected with the Motor-0 output.
The left back wheel is connected with the Motor-1 output
The right back wheel is connected with the Motor-2 output
The right front wheel is connected with the Motor-3 output

In order that this test is in the same way, need to determine the direction of the
wheel rotation in each position. In here, a motor of any wheel is driven with a positive
value power (+). From the motor function, the motor will turn to the direction that forces a
robot to move forward. For example, when you drive the motor from the Motor-1 output
with a positive power, the motor shaft will rotate to the direction that makes a robot move
forward. If it rotates in the reverse direction, change the polarity connecting of the motor
cables at the Motor-1 output to opposite polarity. And then examine remaining 3 motors
and wheels with the same method.

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2

15

48

MOTOR

44 ADC4

50

PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a t o r e > >> > > > > >

ADC7

SERVO PORT

9 10 11 12 13

c o n t r o l l er R b o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

Robo-Creator : AT-BOT activity book 85

#include <atx.h>
// Include the main library

Code explanation
The motor of the channel 0, 1, 2, and 3
are driven with +50% power. Observe the
voltage polarity that supplies to the motor from

void setup()
{}
void loop()
{
motor(0,50);
motor(1,50);
motor(2,50);
motor(3,50);
}

color of the status LED on the motor output of


all 4 outputs will have to become green. (If
driving with a negative value, the LED will get
red). In order to define the polarity connecting
of all motor outputs according to the reference.
Apply this pattern of the connection of motor
cables on all experiments of this.

Listing L8-1 : motor_test.pde, the sketch file for checking the motor
connection of the AT-BOT

Procedure
L8.1 Remove all motor cable.
L8.2 Create the new sketch file. Type the Listing L8-1 and save as motor_test.pde file
L8.3 Compile and upload the sketch to the ATX board.
L8.4 Connect the left front motor cable to motor-0 output
If polarity is correct, robots wheel will rotate to the direction that drive the robot
forward. If not, swap the connection of motor cable

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0

ADC7

46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a to r e > >> >> > > >

KNOB

10 11 12 13

SERVO PORT

c o n t ro l l er R bo a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

USB DATA

E2

RESET

7.2-9V BATT.

START

86 Robo-Creator : AT-BOT activity book

L8.5 Connect the left back motor cable to motor-1 output of the AT-BOT
If polarity is correct, robots wheel will rotate to the direction that drive the robot
forward. If not, swap the connection of motor cable

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2
50
48

MOTOR

44 ADC4

BATTERY LEVEL

15 PC7

45 ADC5

49

PC6

C r e a to r e > >> >> > > >

ADC7

10 11 12 13

SERVO PORT

c o n t ro l l er R bo a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

L8.6 Connect the right back motor cable to motor-2 output of AT-BOT
If polarity is correct, robots wheel will rotate to the direction that drive the robot
forward. If not, swap the connection of motor cable

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a to r e > >> >> > > >

ADC7

10 11 12 13

SERVO PORT

c o n t ro l l er R bo a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

Robo-Creator : AT-BOT activity book 87

L8.7 Connect the right back motor cable to motor-3 output of AT-BOT
If polarity is correct, robots wheel will rotate to the direction that drive the robot
forward. If not, swap the connection of motor cable

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0

ADC7

46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a to r e > >> >> > > >

KNOB

10 11 12 13

SERVO PORT

c o n t ro l l er R bo a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

USB DATA

E2

RESET

7.2-9V BATT.

START

Since all motor connections are correct and get the correct result.
User must use these motor connection and polarity mainly on
programming for all experiments in this activity book.

88 Robo-Creator : AT-BOT activity book

Experiment 9
Creating the AT-BOT movement function
These experiments will present examples of program development to control the
AT-BOTs movement by using the movement control function. Includes forward, backwards,
turn left, turn right and stop.

Experiment 9.1 Move forward function


This experiment demonstrates about AT-BOT moving forward after the SW1 switch is
pressed.

#include <atx.h> // Include the main library


void forward()
// Function of driving a robot forward
{
motor(0,40); // Drive the motor of the left front wheel forward with 40% power
motor(1,40); // Drive the motor of the left back wheel forward with 40% power
motor(2,40); // Drive the motor of the right back wheel forward with 40% power
motor(3,40); // Drive the motor of the right front wheel forward with 40% power
}
void setup()
{
lcd("SW1 Press!");
// Display the title message
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
forward();
// Drive a robot forward
}

Code explanation
When the program starts, there is a title message at the ATX board display and then wait
for the pressing of SW1. After SW1 is pressed, the program will repeat to work in the loop
function to drive motors in order to make the robot move forward constantly.
Addition Program developers can define motor driving power as appropriate. Generally, it is
defined that values of the motor driving power in all wheels should be the same. In case that
each motor operates differently, a developer needs to test by adjusting a value of driving power
of each motor to be not the same in order to find out the best value that makes a robot move
best as possible.

Listing L9-1 : robot_move_forward.pde, the sketch file for controlling


the AT-BOT to move forward

Robo-Creator : AT-BOT activity book 89

Procedure
L9.1.1 Create the new sketch file. Type the Listing L9-1 and save as robot_move_forward.pde
file.
L9.1.2 Compile and upload the sketch to the AT-BOT.
L9.1.3 Run the program.
AT-BOT display shows the title message ; SW1 Press.
AT-BOT moves forward after the SW1 is pressed.

Experiment 9.2 Move backward function


This experiment demonstrates about AT-BOT moving backward after the SW1 switch
is pressed.

Procedure
L9.2.1 Create the new sketch. Type the Listing L9-2 and save as robot_move_backward.pde
file.
L9.2.2 Compile and upload the sketch to the AT-BOT.
L9.2.3 Run the program.
AT-BOT display shows the title message ; SW1 Press.
AT-BOT moves backward after the SW1 is pressed.

#include <atx.h> // Include the main library


void backward() // Function of driving a robot backward
{
motor(0,-40); // Drive the motor of the left front wheel backward with 40% power
motor(1,-40); // Drive the motor of the left back wheel backward with 40% power
motor(2,-40); // Drive the motor of the right back wheel backward with 40% power
motor(3,-40); // Drive the motor of the right front wheel backward with 40% power
}
void setup()
{
lcd("SW1 Press!");
// Display the title message
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
backward();
// Drive a robot backward
}

Listing L9-2 : robot_move_backward.pde, the sketch file for controlling


the AT-BOT to move backward

90 Robo-Creator : AT-BOT activity book

Experiment 9.3 Turn left function


This experiment demonstrates about AT-BOT turning left after the SW1 switch is
pressed.

Procedure
L9.3.1 Create the new sketch file. Type the Listing L9-3 and save as robot_turn_left.pde file.
L9.3.2 Compile and upload the sketch to the AT-BOT.
L9.3.3 Run the program.
AT-BOT display shows the title message ; SW1 Press.
AT-BOT turns left after the SW1 is pressed.

#include <atx.h> // Include the main library


void turn_left() // Function of driving a robot to turn left
{
motor(0,-40); // Drive the motor of the left front wheel backward with 40% power
motor(1,-40); // Drive the motor of the left back wheel backward with 40% power
motor(2,40); // Drive the motor of the right back wheel forward with 40% power
motor(3,40); // Drive the motor of the right front wheel forward with 40% power
}
void setup()
{
lcd("SW1 Press!");
// Display the title message
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
turn_left();
// Drive a robot to turn left
}

Listing L9-3 : robot_turn_left.pde, the sketch file for controlling the


AT-BOT to turn left

Robo-Creator : AT-BOT activity book 91

Experiment 9.4 Turn right function


This experiment demonstrates about AT-BOT turning right after the SW1 switch is
pressed.

Procedure
L9.4.1 Create the new sketch file. Type the Listing L9-4 and save as robot_turn_right.pde file.
L9.4.2 Compile and upload the sketch to the AT-BOT.
L9.4.3 Run the program.
AT-BOT display shows the title message ; SW1 Press.
AT-BOT turns right after the SW1 is pressed.

#include <atx.h> // Include the main library


void turn_right()// Function of driving a robot to turn right
{
motor(0,40); // Drive the motor of the left front wheel forward with 40% power
motor(1,40); // Drive the motor of the left back wheel forward with 40% power
motor(2,-40); // Drive the motor of the right back wheel backward with 40% power
motor(3,-40); // Drive the motor of the right front wheel backward with 40% power
}
void setup()
{
lcd("SW1 Press!");
// Display the title message
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
turn_left();
// Drive a robot to turning right
}

Listing L9-4 : robot_turn_right.pde, the sketch file for controlling the


AT-BOT to turn right

92 Robo-Creator : AT-BOT activity book

Experiment 9.5 Timimg control the robot movement


This experiment demonstrates how to control the AT-BOTs movement by time setting.

Procedure
L9.5.1 Create the new sketch file. Type the Listing L9-5 and save as robo_pause.pde file.
L9.5.2 Compile and upload the sketch to the AT-BOT.
L9.5.3 Run the program.
AT-BOT display shows the title message ; SW1 Press.
AT-BOT moves forward with 2 seconds and stop for 3 seconds alternate continually
after the SW1 is pressed.

#include <atx.h> // Include the main library


void forward()
// Function of driving a robot forward
{
motor(0,40); // Drive the motor of the left front wheel forward with 40% power
motor(1,40); // Drive the motor of the left back wheel forward with 40% power
motor(2,40); // Drive the motor of the right back wheel forward with 40% power
motor(3,40); // Drive the motor of the right front wheel forward with 40% power
}
void setup()
{
lcd("SW1 Press!");
// Display the title message
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
forward();
// Drive a robot forward
sleep(2000);
// Delay 2 seconds
motor_stop(ALL);
// Stop all moving
sleep(3000);
// Delay 3 seconds
}

Listing L9-5 : robo_pause.pde, the sketch file for controlling the ATBOT to move with time setting

Robo-Creator : AT-BOT activity book 93

Chapter 7
Object avoidance by contact
Subsequent learning after driving robots is to read values from sensors in order to
define the conditions in the movement and the most basic sensor that functions in here is
switch. In this chapter, it will mention about application of a switch input board cooperated
with AT-BOT so as to detect touching with obstacles and hence a robot will be able to
recognize moving to meet obstacles. After this it will process to define movement to be
capable of passing through the obstacles.
An important material used in this learning is a switch input board. There is a circuit
and a working diagram shown as in the figure 7-1. When there is a pressing of switch, it
means touching or bumping with obstacles. The logic output signal will change from the
logic 1 to 0 until release the switch or there is no bumping and then the output signal
will change back to be 1 again.
With this sensor characteristic, it is used to determine the conditions of the movement
for AT-BOT by attaching the switches at the front of a robot body. Once a robot runs to
touch or crash with any obstruction, the switch will be pressed and the controller will
recognize a change of the output connected with the switch. Therefore, this process will
control the robot to move backward and followed by changing a direction of movement.
Only this, the robot will be able to move passed the obstacless.

Output

LED1

LED status

+V

R2
10k
R1
510

LED Switch
status

S1
Switch

R3
220

DATA

Output
GND

When there is no pressing the


switch, the DATA pin has a
logic 1 from connection of
the resistor R2 with the supply
voltage.
When pressing the switch, it will
cause the connection
between the DATA pin and
ground. Therefore, the pin
DATA will have a logic as 0
and there will be electricity
current through the LED 1.
Finally, the LED1 will light up.

Figure 7-1 : Picture, schematic and and circuit explanation of the


Switch input board used in AT-BOT

94 Robo-Creator : AT-BOT activity book

Experiment 10
Reading the Switch input board
This experiment is a test for function of a switch input board on AT-BOT by which the
ATX board will read the status of pressing the switch from the switch input board connected
with ADC0 port (or the port 40) to show at the LCD module of the ATX board.

Additional hardware connection


Attach a switch input board at the front of the AT-BOT
Connect the switch unput board cable to ADC0 port or Port 40 of the ATX board on the
AT-BOT
Switch input board

connect to ADC0

The front of AT-BOT

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2

15

48

MOTOR

44 ADC4

50

PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a to r e > >> >> > > >

ADC7

10 11 12 13

SERVO PORT

c o n t ro l l er R bo a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

#include <atx.h>

// Include the main library

void setup()
{}
void loop()
{
lcd("Switch1: %d ",in(40));
}

// Read the switch input board status from


// ADC0/40 port to display on the LCD module
// of the AT-BOT

Listing L10-1 : switch1_test.pde, the sketch file for reading status of


the Switch input board that interface with AT-BOT at ADC0/40 port

Robo-Creator : AT-BOT activity book 95

Procedure
L10.1 Create the new sketch file. Type the Listing L10-1 and save as switch1_test.pde file.
L10.2 Compile and upload the sketch to the AT-BOT.
L10.3 Run the program.
AT-BOT display shows the title message :

Switch: x
therefore x is the reading data of the Swich input board
L10.4 Press and hold the switch and then check the result of the operation.
The status value of the switch (x) will change to 0 and when you release the switch,
the result will return to the value of 1 again.

STA

T
41 ADC1

4
DC
A

44

40 ADC0

C5
AD

5 A DC 2
442

46 ADC6

C6

11
E R 10O PORT
SSERV
9
8 89 10 11 12 13

+
14

ON

USB D ATA

E2

14

PC6

S W1RESET
49

50

48

TWI
UA RT1
0 SCL 1 SD A 2 R X1 3 TX1

SW2

15

PC7

MOTOR

BATTERY LEVEL

ADC2
ADC64 1 45
A DCA1DC 540 44ADC0
ADC4
42 46

10 11 12 13

SERVO PORT

ADC3
0
4 043A DC

U SB D ATA

E2

15

PC7

MOTOR

46 A DC 6

10 11 12 13

SERVO PORT
BATTERY LEVEL

46 A DC 6

14

SW1

PC6

49

40 ADC0

+
14

S W1

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

ADC 7

10 11 12 13

4 1 A DC 1

10 11 12 13

14

SW1

SW2

ADC7

15 P C7

48

14

SW1

MOTOR

48

MOTOR

BATTERY LEVEL

50

4 4 ADC4

15 P C7

BATTERY LEVEL

SW2

50

4 4 ADC4

9 10 11 12 13

SERV O PORT

KNOB

45 ADC5

PC6

49

45 ADC5

PC6

49

45 ADC5

44 A DC 4

MOTOR
PC7

15

BA TTER Y LEVEL

PC6

48

44 A DC 4

S W2

15

PC7

MOTOR

BA TTER Y LEVEL

50

45 ADC5

PC6

49

5
4
3
2
1

43 ADC3

RX

UA 1 3

46 ADC6

2
DC
A

40 ADC0

2
4

L
SC

I SD
TW 1

T
E SE
R

41 ADC1

C3
AD

AD
>
46
>
d
>
> t o rr e > > > > > > > >
C0
C r e> a
a
AD
40
o
>
1
c >o n Rtbr o l l e r R b o a
DC
48 r d
A
>
41
0
r
e
5
EL
USB D ATA
e
2
r
LEV
W
Y
S
R
l
o
O
ER
T
ON
O
49
TT
t TWIl
M
UA RT1
BA
1
a 0 SCLo 1 SD A 2 R X1 3 TX1
7
W
S
r
PC
eE2
15
t
r
6
7
n
C
DC
PC
A
RT
o
1449
ADC7 13
SW1
S W2 50
48
+
B KNOB PO
1 X1
12
cRESET
NO
RT T
VO
K

TA
DA

E2

45 A DC
435 ADC3
44 ADC4
42 A DC 2

US

46 ADC6

43

4 0 A DC 0

.
41 ADC1

TT

42 ADC2

41 ADC1

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

42 ADC2

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

SERVO PORT

42 ADC2

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

ADC7

c o n t r o l l er R b o a r d

C r e at o r e> >> >> > > >

43 ADC3

SW2
KNOB
50 ADC7
48

c o n t r o l l ecroRnbtoraorldl e r R b o a r d

C r e a t o r e >C>r>e>a>t>o>r>e > > > > > > > >

43 A DC 3

KNOB ADC 7

c o nt r o ll er Rb o a rd c on t r ol l erR b o ar d

C r e a t o r e > > > > > > > > C r e a t o r e > >> > > > > >

43 A DC 3

BA

USB DATA

E2

RESET

USB DATA

E2

R ESET KNOB

U SB D ATA

E2

SERVO PORT

STAR T
START

0
5
4

7.2-9V BAT T.

7 .2 -9V B AT T.

KNOB

5
3
5
4
3
2

ON

ON

RESET

ON

R ESET

START

4
1
STAR T

3
S

4
3
2
2
0
-

1
1

7.2-9V BATT.

7.2 -9V BAT T.

1
0

START

START

0
5

7 .2 -9V B AT T.
7.2-9V BATT.

Obstacle
Obstacle
Obstacle
Obstacle

7.
2-9
V

96 Robo-Creator : AT-BOT activity book

Experiment 11
AT-BOT avoid the obstacle by touching

This experiment demonstrates about programming to controls AT-BOT in order to


detect an obstacle by bumping and then change its direction to avoid the obstacle. A
touch sensor or a switch input board is applied and installed at the front of a robot
according to the experiment 10. There are conditions of the operation as follows:
1. A robot does not find any collision, the robot will move straight forward
constantly.
2. A robot is crashed, there will be a loud sound out once. Then, the robot will
step backward and change the direction to the left and keep moving forward to avoid
the object.

Procedure

L11.1 Create the new sketch file. Type the Listing L10-1 and save as switch1_test.pde file.

L11.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable from the robot.

L11.3 Place the robot on the floor. Place some obstacle such as box or can to set the
demonstration field.

L11.4 Run the program.

AT-BOT display shows the title message : Press SW1

L11.5 Press the SW1 at AT-BOT.

AT-BOT move forward. If it will bump a obstacle, it will step back and change a
direction to the left. After that the robot will move forward continuously.

Robo-Creator : AT-BOT activity book 97

#include <atx.h>
// Include the main library
#define POW 80
// Set the motor power to 60%
char mid;
// Declare the switch status variable
void forward(unsigned int time)
// Moving forward function
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for moving forward
}
void backward(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for moving backward
}
void turn_left(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for turning left
}
void turn_right(unsigned int time)
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for turning right
}
void setup()
{
lcd("SW1 Press!");
sw1_press();
}
void loop()
{
mid = in(40);
if(mid==0)
{
beep();
backward(500);
turn_left(800);
}
else
{
forward(1);
}
}

// Display title message for asking to press the SW1


// Wait until the SW1 is pressed

// Read the switch status to store to the mid variable


// Check the switch pressing
// If the sensor is pressed, drive a beep once
// Move backward 0.5 second
// Turn left 0.8 second for avoiding the obstacle

// The robot move forward with 0.001 second

Listing L11-1 : robo_bumper1.pde, the sketch file for demonstration


the AT-BOT avoid the obstacle by using one touch sensor (continue)

98 Robo-Creator : AT-BOT activity book

Code explanation
The program begins with display the title message for asking the SW1 pressing in order to
wait for pressing the switch of SW1 by the sw1_press function. When the SW1 on the AT-BOT is
pressed, the program will be able to follow a command in the next line, which is to work in the
loop function. It defines to repeat reading a status value of the ADC0/40 port, which is connected
with the switch input board or touch sensor. Store the value at the variable mid. Once the switch
input board is pressed, it will give the result as 0 and then the program will bring the value of
the variable to compare as the following:
The case of if(mind==0) : it is the checking the switch input board is pressed or
not. If it is true, this will influence the robot to drive a beep sound and move backward. After
that the robot will turn left to avoid an object (the value of time delay will be adjusted by a
developer as appropriate).
The case of else : it is inferred that the robot has not found any obstacle. If is true,
it will cause the robot to move forward for a short distance.

Listing L10-1 : switch1_test.pde, the sketch file for reading status of


the Switch input board that interface with AT-BOT at ADC0/40 port
(final)

Robo-Creator : AT-BOT activity book 99

Experiment 12
Reading two touch sensors
This experiment purpose is reading the status of two of switch input boards. Now it
is called Touch sensor. The ATX board will read the status of switch pressing from the
switch input board connected with ADC0/40 and ADC2/41 ports to show at the LCD
module of the ATX board.

Additional hardware connection


Attach two of the switch input board at the front of the AT-BOT
Connect the switch input board #1 cable to ADC0/40 port of the AT-BOT
Connect the switch input board #2 cable to ADC1/41 port of the AT-BOT
Switch input
board #1

Switch input
board #2

connect to
ADC0/40

The front of AT-BOT

connect to
ADC1

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0

ADC7

46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a t o r e > >> > > > > >

KNOB

10 11 12 13

SERVO PORT

c o n t ro l l erR b o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

USB DATA

E2

RESET

7.2-9V BATT.

START

Procedure
L12.1 Create the new sketch file. Type the Listing L12-1 and save as switch2_test.pde file.
L12.2 Compile and upload the sketch to the AT-BOT.

100 Robo-Creator : AT-BOT activity book

#include <atx.h>
// Include the main library
void setup()
{}
void loop()
{
lcd("Switch1: %d #nSwitch2: %d ",in(40),in(41));
// Read the switch input board status from ADC0/40
// and ADC1/41 port to display on the LCD module of the AT-BOT
}

Listing L12-1 : switch2_test.pde, the sketch file for reading status of


two Switch input board that interface with AT-BOT at ADC0/40 and
ADC1/41 port
L12.3 Run the program.
AT-BOT display shows the title message : Press SW1
L12.4 Press the SW1 on the AT-BOT.
AT-BOT display shows the title message :

Switch1: x
Switch2: y
therefore x and y are the reading data of the Swich input board #1 and 2
consequently
L12.5 Press the Switch input board #1 (at port ADC0/40) and see the operation.
The status value of the switch (x) will change to 0 and after release the switch, the
result will return to the value of 1 again.
L12.6 Press the Switch input board #1 (at port ADC1/41) and see the operation.
The status value of the switch (y) will change to 0 and after release the switch, the
result will return to the value of 1 again.

T
R
S TA

41 AD C1

43

2
4

E2

AD

C2

43

45

RX

E2

ES
R

SC

ET

I SDA
TW 1

AD

C5

44

4
A DC

40 ADC0

AD

C7

RV

SE

10

13

PC6

49

11

RT
PO 12

14

SW1

SW2

15

ON

USB DAT A

E2

PC6

49

SW2

15

50

PC7

48

MOTOR

48

MOTOR

BAT TERY LEVEL

50

44 AD C4

15 PC7

BATTERY LEVE L

SW2

TWI
UART1
0 SCL 1 SDA 2 RX 1 3 TX1

49

45 ADC5

14 PC 6

ADC7 RESET SW1

10 1 1 1 2 13

SERVO PORT

46 ADC6

14

45 ADC5

PC6

49

SW2

15

ADC7

10 11 1 2 13

SERVO PORT

KNOB

48

44 AD C4

50

PC7

MOTOR

BAT TERY LEVEL

14

SW1

PC 6

49

50

SW2 50

15 PC7

14

48

6
PC

MOTOR

MOTOR

BATTERY LEVE L

48

44 ADC4

PC7

BAT TERY LEVEL

46 ADC6 45 ADC5

1 0 1 1 12 1 3

SERVO PORT

KNOB A DC7

B
NO
K

C6
>
AD
>
46
> rd
>
0
C
ADC3 AD
42 AD C2 41 ADC1 >
40 AD C0 a 46 ADC6 45 ADC5 44 AD C4
40
o
>
b
>
48
R
>
r
50
e
V EL
e
2
r
LE
W
Cr e
RY
o a tlo r e > > > > > > > S>
TE
l
49
t
AT
1 r d B
cao n to r o l l e r R b o a
7
SW
r
PC
e
15
t

1
A DC

AT
D

T WI
UA RT1 T1 TX
AR 3
0 SC L 1 SDA 2 RX1 3 UTX1 1

41

ON

46 ADC6
43 AD
45 C3ADC5
42 ADC2
44 ADC4
41 AD C1

RESET

US

US B DATA

C3
AD

ON

40 ADC0

AR
T

42 ADC2

TWI
UART1
0 SCL 1 SDA 2 RX 1 3 TX1

KNOB

c o n t r o l l e r R b o acrodn t r o l l e r R b o a r d

C r e a t o r e > > > > > >C>r>e a t o r e > > > > > > > >

43 AD C3

ST

AD
C4

USB DAT A

E2

AD

44

48

44 AD C4

45
C5

MOTOR

ON

RESE T

>

MOTO R

45 ADC5

>

>

50

PC7

48

44 ADC4

50

1 5 PC 7

40 ADC0

46 ADC6

14

SW1

46 ADC6

>
>
a

>

SW2

15

BATTERY LEVEL

SW2

BATTERY LEVEL

41 ADC1

1 0 11 12 1 3

40 AD C0

1 0 1 1 12 1 3

AD
C2

>

49

PC6

45 ADC5

PC 6

49

42 AD C2

SERVO PORT

41 ADC1

SW1

c o n t r o l l er R b o a r d

C re a to r e> >> >> > >>

43 ADC3

TWI
UA RT1
0 SC L 1 SD A 2 RX1 3 TX1

KNOB A DC 7

42 AD C2

SERVO PORT

ADC 7

c o n t r o l l er R b o a r d

Cr e a to r e> >>> > > >>

43 ADC3

TWI
UA RT1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

C3

40 AD
42 C0

AD

e
R
b

14

SW1

14

SW1

43

41 ADC1

ADC7

48

10 11 1 2 1 3

50

LE
VE

SW
2

46 ADC6

1 0 1 1 12 13

42 ADC2

SERV O PORT

KNOB

49

MO
TO

BA
TT
ER

40 ADC0

SERVO PORT

KNOB

TT

US
B

TW
I
1 SD

ADC

P
C7

SW
1

41
DAT
C
A
A
r
DC1
c
40 > > > >
C r e ao t oe r
a e > >> >
AD
n
t
C0
c o n t r tor l lo e r R b o a r d
r
46
o
e
A
AD
l
C6
>

43 ADC3

0 SC

PO

KN
OB

RV
O

MOTO R

US B DATA

E2

US B DATA

E2

RESET

5
4
3
2

O
N

E
2

US B DATA

SE

SE
10

RT

13

15

2 RX U
AR
1
T1
3 TX
TWI
U1ART1
0 SCL 1 SDA 2 RX1 3 TX1

12

PC
6

48

TWI
UART1
0 S CL 1 S DA 2 RX1 3 TX1

50

PC7

15

ON

RESET

ON

RE

T.
AT
BE2

11

+
14

46 AD C6
43 ADC3
45 ADC5
42 AD
44C2ADC4
41 ADC1

RESET

9V
27.

ON

40 ADC0

USB DAT A

E2

SW2

BATT ERY LEVE L

PC6

49

MOTOR

MOTOR

41 ADC1

ON

+
14

ADC 7 RES ET SW1

10 11 12 13

44 AD C4

48

45 ADC5

50

PC7

15

48

44 ADC4

50

PC7

STA RT

START

BA

42 AD C2

C r e a t o r e > > > > > >C>r>e a t o r e > > > > > > > >

43 ADC3

SERVO P ORT

46 ADC6

SW2

BATTERY LEVEL

45 ADC5

15

BATTERY LEVEL

SW2

ADC7

c o n t r o l l e r R b o acrodn t r o l l e r R b o a r d

KNOB

40 ADC0

PC6

49

46 ADC6

PC6

49

7.2- 9V BATT.

7.2 -9V BATT.

2
- 9V

USB DAT A

41 ADC1

TWI
UAR T1
0 SCL 1 SDA 2 RX1 3 TX1

42 ADC2

+
14

SW1

co nt rol l erRb oa rd

C r e a t o r e > >> > > > > >

43 ADC3

10 1 1 1 2 1 3

40 AD C0

14

E2

RESE T

USB DATA

SERV O PO RT

41 ADC1

START

ON

ON

KNO B ADC7

42 ADC2

TWI
U ART1
0 SCL 1 SDA 2 RX1 3 TX1

10 11 12 1 3

SW1

co ntr ol l erRb oar d

C r e a t o r e > >> > > > > >

43 ADC3

TWI
U ART1
0 SCL 1 SDA 2 RX 1 3 TX1

E2

USB DATA

START

START

7.2 -9V BATT.

7.2 -9V BATT.

START
STA RT

0
5
4

Obstacle

Obstacle

Obstacle

Obstacle

2
1

0
5
4
3
2
1
0

7.2 -9V BATT.


7.2 -9V BATT.

S ERVO PORT

START

E2

RESET

ON

7.2-9V BATT .

5
4
3
2

1
0

STA RT

START

KNO B ADC7

1
0

7.2 -9V BATT.


7.2-9V BA TT.
RESET

7.2-9V BATT.

Obstacle
Obstacle
Obstacle
Obstacle

7.

Robo-Creator : AT-BOT activity book 101

Experiment 13
AT-BOT avoid the obstacle with two touch sensors

This experiment demonstrates about programming to using two switch input boards
as two of touch sensors. One is installed at the left front side of the AT-BOT. Another one is
on the right front side. The operated condition is as follows :
1. A robot does not find any collision, the robot will move straight forward
constantly.

2. A robot is crashed on the left side, there will be a loud sound out once. Then,
the robot will move backward and change the direction to the right and keep moving
forward to avoid the object.

3. A robot is crashed on the right side, there will be a loud sound out once. Then,
the robot will move backward and change the direction to the left and keep moving

forward to avoid the object.

TO

102 Robo-Creator : AT-BOT activity book

4. A robot is crashed on the front side. Both touch sensors are attacked. There will
be a loud sound out once. Then, the robot will move backward and change the direction
to the left and keep moving forward to avoid the object.

Obstacle

Obstacle

ON
1

49
S W2

BA
TTE

C6
A DC

AR

C4

ST

AD

44

>

>

>

9V

AD

45

>

50

48

RY

LE

15

P C7

VE

ADC4

48

MOT OR

P C6

46

>

ADC5 44

50

PC 7

SW

14

45

15

C0

>

C7

RT
13

SW2

48

MOTOR

44 AD C4

50

PC7

BAT TERY LEVEL

15

SW2

BATTERY LEVEL

ADC5

PC6

49

12

PO

BA
TT
.

11

er

>

ADC6

RV
O

10

>

46

14

SW1

AD

40 ADC0

ADC 7

1 0 11 12 1 3

OB

41 ADC1

SERVO P ORT

SE

C1
AD

42 ADC2

PC6

49

ADC6 45

AD

40

2 R UA RT
X1
1
3 TX

KN

41

A DC

KNOB

46

14

SW1

START

40 ADC0

42

C re a t or e> >>> > >> >

START

C3

c o n t r o l l er R b o a r d

UART1
2 RX 1 3 TX1

E2

43 AD C3

7.2 -9V BA TT.

RE
S ET

AD
USB D ATA

TWI
0 SCL 1 SDA

DA
TA

0 SC TW
I
L
1 SD

43

E2

43 45
42 44
41 AD C1
AD C6
ADC3ADC5
ADC2ADC4

U SB

ON

ON

RESE T

46

ADC7

10 11 1 2 13
S

40 ADC0

42 ADC2 41 ADC1

C r e a t o r e > > > > > >C>r>e a t o r e > > > > > > > >

T WI
UAR T1
0 SCL 1 SDA 2 RX1 3 TX 1

43 AD C3

E2

US B DATA

SERVO PORT

KNOB

48

MOT OR

50

1 5 PC 7

SW2

BAT TERY LEVEL

ON

PC6

49
7.2-9V BATT .

c o n t r o l l e r R b o acrodn t r o l l e r R b o a r d

UART1
2 RX 1 3 TX1

14

TWI
0 SCL 1 SDA

ADC4

ADC 7 RESE T SW1

1 0 11 12 1 3
S

ST ART

E2

44

SERVO PORT

ADC5

48

MOTOR

45

PC7

K NOB

ADC6

15

SW2 50

BA TTERY LEVE L

48

MOTOR

44 AD C4

50

PC7

USB D ATA

RESE T

46

PC6

ADC5

15

SW2

BATTERY LEV EL

STA RT

7.2-9V BATT .

41 ADC1 40 AD C0

14

SW1 49

C r e a t o r e > >> > > > > >

ADC7

10 1 1 12 13
S

43 ADC3 42 AD C2

SERVO PORT

46 AD C6 45

c on tr o ll erR b oa r d

TWI
UA RT1
0 SCL 1 SD A 2 RX1 3 TX1

KNOB

41 ADC1 40 ADC0

PC6

49

C r e a t o r e > >> > > > > >

14

SW1

co n tr o ll erR b oa r d

43 ADC3 42 ADC2

TWI
UART1
0 SC L 1 SDA 2 RX1 3 TX 1

ADC7

10 11 1 2 13
S

US B DATA

E2

US B DATA

E2

SERVO PORT

KNOB

7.2 -9V BA TT.

2-

ON

RESET

ON

RESET

7.2 -9V B ATT.

Obstacle

7.

Obstacle

STA RT

MO

TO
4 R

Additional hardware connection and construction


Attach 2 of Switch inpiut board at the fronmt of AT-BOT by using the Right angle joiners,
Straigh joiners, 3 x 10mm. screws and 3mm. nuts. Attach the sensor at left front and right
front side of the robot by doing at an angle about 45 degrees.
- Attach the switch input board #1 at left front side and connect its cable to ADC0/
40 port of the AT-BOT
- Attach the switch input board #2 at right front side and connect its cable to
ADC1/41 port of the AT-BOT

Procedure
L13.1 Create the new sketch file. Type the Listing L13-1 and save as robo_bumper2.pde.
L13.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable from the robot.
L13.3 Place the robot on the floor. Place some obstacle such as box or can to set the
demonstration field.
L13.4 Run the program.
AT-BOT display shows the title message : Press SW1
L13.5 Press the SW1 on the AT-BOT. Observe the robot operation.
AT-BOT will go straight constantly. If the robot finds a obstacle and there is collision
according to the defined condition, AT-BOT will be able to change the direction of
movement to avoid the obstacle.

Robo-Creator : AT-BOT activity book 103

#include <atx.h>
// Include the main library
#define POW 80
// Set the motor power to 60%
char left,right;
// Declare the switch status variable
void forward(unsigned int time)
// Moving forward function
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for moving forward
}
void backward(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for moving backward
}
void turn_left(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for turning left
}
void turn_right(unsigned int time)
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for turning right
}
void setup()
{
lcd("SW1 Press!");
// Display title message for asking to press the SW1
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
// Read the left switch status to store to the left variable
left = in(40);
right = in(41);
// Read the right switch status to store to the right variable
if(left==1 && right==1)
// Check both switch are not pressed (no attack)
{
forward(1);
// Robot move forward short time
}
else if(left==0 && right==1) // Check only the left switch pressing
{
beep();
// If the left swith is pressed,
// drive a beep sound once
backward(500);
// Move backward 0.5 second
turn_right(800);
// Spin right 0.8 second
}
//for changing the direction of movement

Listing L13-1 : robo_bumper2.pde, the sketch file for demonstration


the AT-BOT avoid the obstacle by using two touch sensor (continue)

104 Robo-Creator : AT-BOT activity book

else if(left==1 && right==0)


{
beep();
backward(500);
turn_left(800);
}
else if(left==0 && right==0)
{
beep();
backward(500);
turn_left(1500);

// Check only the right switch pressing


//
//
//
//
//

If the left swith is pressed,


drive a beep sound once
Move backward 0.5 second
Spin left 0.8 second
for changing the direction of movement

// Check both switch are pressed together


//
//
//
//
//

If the both swith is pressed,


drive a beep sound once
Move backward 0.5 second
Spin left 1.5 seconds
for changing the direction of movement

}
}

Code explanation
At the beginning of the operation, the program will show the text of Press SW1 at the
LCD module to wait for pressing the SW1 switch on the AT-BOT. When the SW1 is pressed, CPU
will operate the command in the loop to repeat reading status values of the both switch input
boards at ACD0/40 and ADC1/41 to store at the variable of left and right respectively. Any
switch board is pressed, this will give the result value as 0 and then the program will take the
values of both variables to compare based on the below mentioned 4 cases:
Case 1: if(left==1 && right==1) verified whether there is no pressing both switches
or not. If it is true (no collision occurred), it will respond a robot to move forward in a short
distance because it has not found any obstacle.
Case 2: else if(left==0 && right==1) checked pressing only the left switch, so if
it is true (bumping at the left), there will be a response by making a beep sound and a robot will
step back to turn right in order to not clash with an obstacle.
Case 3: else if(left==1 && right==0) examined pressing only the right switch, if
it is correct (bumping at the right), it will react by a beep sound. Then, a robot will move
backward to turn left so it can avoid an obstacle.
Case 4: else if(left==0 && right==0) it is a test of pressing both switches. If it is
true (colliding with the front so both switches are pressed simultaneously), there will be 1
rhythm of a sound. Meanwhile, it will cause a robot to reverse and then turn left to avoid an
obstacle.
Program developers can modify the values of time delay used to define rhythmic movement
freely in each situation appropriately.

Listing L13-1 : robo_bumper2.pde, the sketch file for


demonstration the AT-BOT avoid the obstacle by using
two touch sensor (final)

Robo-Creator : AT-BOT activity book 105

Chapter 8
AT-BOT Line tracking
One mission of learning about programming to control a small autonomous robot
is detection and locomotion along lines. Therefore, AT-BOT are able to perform this mission.
Contents in this chapter will start with explanation of function of light reflection sensors
which will be applied as line sensors and followed by preparation of field for test, examples
of sensor calibration to work efficiently. After this, entering to experimentls of programming
with C/C++ language to seek lines, move within the defined areas and move along the
lines which are not considered the junction and considered the junction.
Therefore, learners need efforts to understand and perform trials according to
sequences because the whole contents are related each and you will have to apply
some knowledge from the previous contents to integrate as well.

8.1 Properties of a light reflector : ZX-03R


It is a circuit board used to detect reflection of light. It consist of a super bright red
LED which functions as a light source and highly sensitive phototransistor which can detect
both visible and invisible light (normal light and Infrared light). The phototransistor will get
reflected light which is originated from the LED and reflected on the floor. The
phototransistor will give different results based on intensity of reflected ligh it gains. Figure
8-1 shows the working process of this sensor when it is applied on white and black floor.
When the supply voltage is applied, the super-bright red LED will be active. The red
light is emitted all the time. In part of the receptor or the phototransistor will obtain the red
light from the reflection. The phototransistor will get more or less quantity of light depending
on whether any object obstacles or not and the ability to reflect red light on that object,
which is based on the feature of floor and color of the objects. Smooth white objects can
reflect light well, so the light receptor or the phototransistor will obtain a lot of reflected
light. As a result the output voltage of the circuit is high accordingly. Meanwhile, black
objects reflex less light so the light receptor works less and sents out low voltage. With
those features, the circuit boards of the reflected light sensors are installed at the bottom
of a robots structure to detect surface and lines.

106 Robo-Creator : AT-BOT activity book

SFH310

+V

current

LED

510
10k

OUT

High output voltage


GND

Super-bright
red LED
Photo-transistor
White surface

(A) Operation of sensor with the white surface

SFH310

+V

current

LED

510
10k

OUT

Low output voltage


GND

Super-bright
red LED
Photo-transistor
Black surface

(B) Operation of sensor with the black surface

Figure 8-1 : Operation of ZX-03R; the reflected light sensors when


used to detect a black line and white surface

Robo-Creator : AT-BOT activity book 107

8.2 Line tracking activity preparation


8.2.1 Material preparation for making the demonstration field
In the program development to instruct a robot moving along lines by using the
light reflection sensor, first thing to do is analyze lines and surface in order to use the data
to set conditions and then compare with a reference value from the test of reading of the
state of light reflected on lines and field floor which is actually used. The test of reading is
based on the differences of light reflection at each surface with different color. White
surface is able to reflex light well but dark surface can do less because black color has low
ability of light absorption. Learning in this chapter choose the demonstration field which
have white surface and black line. Developers have to make this field first.
The demonstration filed of this handbook will be made from Polypropylene board
or PP board and black tape. Important materials and tools compose of:
1. White Polypropylene or PP board. Size is 90 x 60cm. However the sizing can
change depending on your applications and resoucres.

2. Black electrical tape 1 inches width 2 rolls. It is recommended to buy 3M brand


because this brand has high elastic and it can stick curve well.

3. Scissors or a Cutter

108 Robo-Creator : AT-BOT activity book

8.2.2 Installation the light reflector sensors (ZX-03R) for AT-BOT


The purpose of application of the ZX-03R light reflector sensors in AT-BOT is to detect
surface and lines so the sensors will be installed under a robot body. There are steps
mentioned as follows:
(1) Attach 3 of ZX-03R sensors with 12-hole Strip joiners by 3 x 10mm. screws and
3mm. nuts. There are 3mm. plastic standoffs separated out between the sensor boards
and the Strip joiners.

12-hole Strip joiner Robot chasis


3mm. plastic standoff
25mm.
standoff

3 x 40mm. screw
ZX-03R
3 x 15mm.
screw

ZX-03R

3mm. plastic standoff


3 x 40mm.
screw
ZX-03R

3mm. nut

3mm. nut

25mm.
standoff

3mm. plastic standoff


25mm. plastic standoff

Right ZX-03R is connected


to ADC2 port

Middle ZX-03R is
connected to ADC1 port
25mm. plastic standoff
Left ZX-03R is connected
to ADC0 port
may be cut if it touch the
front wheel

Top view

3mm. nut

Robo-Creator : AT-BOT activity book 109

(2) Turn off power of AT-BOT and then attach the set of the ZX-03R structure from
step (1) at the front of the robot chasis by using 2 sets of 25mm. standoffs to cushion and
fasten them with 3 x 40mm. screws and 3mm. nuts 2 sets. As the result, the ZX-03R boards
are far from the floor about 5mm. Then, plug the cables of the left, middle and right ZX-03R
to ADC0, ADC1 and ADC2 port of AT-BOT respectively.
Finally, the AT-BOT is ready for the mission to detect surface and lines.

The objective of this style of installation sensors is to check surface and lines. The
sensors can detect both white and black lines including different colors such as purple
and yellow, green and red, etc.

8.2.3 Determination of the reference value to separate


differences between lines and surface
To define the reference value for using in comparison in order to let a robot know
that a sensor has found lines or surface, generally it is based on a programming of reading
a value from each light reflection sensor to display. Normally, both values have to be
quite different.
Programming C/C++ with Wiring to read values from a ZX-03R light reflection sensor
of the AT-BOT will use the analog function. It shows result values 0 to 1023.

110 Robo-Creator : AT-BOT activity book

Super-bright
red LED
Photo-transistor

Value from
analog function

White surface

400 to 900

(A) The white surface reading of the ZX-03R sensor

Super-bright
red LED
Photo-transistor
Black line

Value from
analog function

10 to 300

(B) The black line reading of the ZX-03R sensor

If the ZX-03R is at the area of black lines, a value will be low and tends towards the
value 0.
If the sensor is at the area of a white surface, a value will be high and tends towards
the value 1023.
The calculation of the reference value used to identify white surface and black
lines will be as follows
Reference value = (value from the white surface + value from black lines)/2
For example, if the white surface values is 950 and black lines is 250, so the reference
value will be (950+250)/2 = 600
However, in a practical application user may configure to be wider than these but
should be in the range of 250 to 950 but should not choose values to be close to 250 and
950 too much.

Robo-Creator : AT-BOT activity book 111

Experiment 14
Determine the reference value for line detection
This experiment is a test to find reference values for detecting lines of AT-BOT with 3
of ZX-03R sensors. They are installed in the front and under the structure base of the AT-BOT.
The left sensor is connected with ADC0, the middle one connected with ADC1 and the
right one connected with ADC2. The result values will be processed and shown at the LCD
module of the AT-BOT robot.
The experiment will present and make the understanding about how to find the
reference value for distinguish between lines and surface. It is important knowledge for
making the line tracking robot.

Make the simple field


The test field in this experiment has a white surface and constructed by sticking of
black tapes as a quadrangle with curve angles according to the figure below:

The size of this field can be modified as appropriate. In this book, choose a white
PP board with the size of 90x60cm. stuck with black tapes to create the curve border in
the size of 70x40cm.

112 Robo-Creator : AT-BOT activity book

Hardware connection
Connect ADC0/40 port of AT-BOT with the left front ZX-03R sensor
Connect ADC1/41 port of AT-BOT with the middle front ZX-03R sensor
Connect ADC2/42 port of AT-BOT with the right front ZX-03R sensor

ZX-03R sensors are


attached at bottom
of the robot chasis

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a t o r e > >> > > > > >

ADC7

SERVO PORT

9 10 11 12 13

c o n t r o l l er R b o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

#include <atxt.h>
// Include the main library
void setup()
{}
void loop()
{
lcd("L %d M%d
#nR %d
",analog(0),analog(1),analog(2));
// Read all sensor value from ADC0, ADC1 and ADC2 port
// to display on the AT-BOT display
}

Listing L14-1 : reflect_test.pde, the sketch file for reading sensor from
ADC0, ADC1 and ADC2 input to display at the AT-BOT

Robo-Creator : AT-BOT activity book 113

Procedure
L14.1 Create the new sketch file. Type the Listing L10-1 and save as reflect_test.pde file.
L14.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L14.3 Run the program.
AT-BOT display shows the title message :

L xxx M yyy
R zzz
therefore xxx ,yyy and zzz are the digital data from reading the sensor at left, middle
and right position of AT-BOT
L14.4 Place the robot on the white surface. Read and record the reading values which
are displayed on the AT-BOT .
If all 3 sets of the sensing boards read similar values, you will average all values. The
value is about 900 in this step from the test.

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

15 PC7

BATTERY LEVEL

45 ADC5

49

PC6

C r e a t o r e > >> > > > > >

ADC7

SERVO PORT

9 10 11 12 13

c o n t r o l l er R b o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

114 Robo-Creator : AT-BOT activity book

L14.5 Place the robot above the black line and then read and record a measured value
which is shown on the LCD module.
All 3 sets of the sensing boards should read similar values. At this step in this test,
the average is about 100.

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

ON
43 ADC3
42 ADC2
41 ADC1
40 ADC0
46 ADC6

14

SW1

SW2
48

MOTOR

44 ADC4

50

PC7

BATTERY LEVEL

15

45 ADC5

49

PC6

C r e a t o r e > >> > > > > >

ADC7

10 11 12 13

SERVO PORT

c o nt r o l l erR b o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

USB DATA

E2

RESET

7.2-9V BATT.

START

L14.6 Calculate a reference value as follows:


Reference value = ( Value from the white surface + Value from the black line)/2
= (900+100) / 2 = 500
However, in actual application, possible to define wider values than these but
they should be in the range of 100 to 900 and they should not to be close to 100 and 900
too much. From now on, examples will be based on the reference value 500.

Robo-Creator : AT-BOT activity book 115

Experiment 15
Frame detection robot
This experiment demonstrates about controlling AT-BOT robots to move within a
rectangular frame with curve corners surrounded with black lines. This experiment use
only 2 of ZX-03R sensors that is left and right front position.

Use only 2 of ZX-03R at left and


right position to detect the black
line for controlling the robot
move within the frame

Procedure
L15.1 Create the new sketch file. Type the Listing L15-1 and save as robo_inner.pde file.
L15.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L15.3 Place the AT-BOT within the frame of the test field that making in Expeirment 14.
L15.4 Run the program.
AT-BOT display shows the title message :

Press SW1
L15.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
AT-BOT will move within the black frame. If the sensor detect the black line, robot
will move backward and change direction similar the object avoider robot activity in
chapter 7 but changing from inspection of bumping to inspection of black lines and white
surface instead.

116 Robo-Creator : AT-BOT activity book

#include <atx.h>
// Include the main library
#define POW 80
// Set the motor power to 60%
#define REF 500
// Set the reference value as 500
unsigned int left,right;
// Declare the ZX-03R sensor variable
void forward(unsigned int time)
// Moving forward function
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for moving forward
}
void backward(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for moving backward
}
void turn_left(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for turning left
}
void turn_right(unsigned int time)
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for turning right
}
void setup()
{
lcd("SW1 Press!");
// Display title message for asking to press the SW1
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
left = analog(0);
// Read the left sensor data
// to store to the variable left
right = analog(2);
// Read the left sensor data
// to store to the variable right
if(left>REF && right>REF)
// Verify both sensor still not
// detect the black line
{
forward(1);
// No line detect,
// move forward for short time
}
else if(left<REF && right>REF)
// Only the left sensor detect line ?
{

Listing L15-1 : robo_inner.pde, the sketch file for demonstration the


AT-BOT move within the black frame by using two line sensors (cont.)

Robo-Creator : AT-BOT activity book 117

backward(500);
turn_right(800);
}
else if(left>REF && right<REF)
{
backward(500);
turn_left(800);
}
else if(left<REF && right<REF)
{
backward(500);
turn_left(1500);
}

// If correct, move backward 0.5 second


// Spin right 0.8 second to change direction
// Only the right sensor detect line ?
// If correct, move backward 0.5 second
// Spin left 0.8 second to change direction
// Both sensors detect the line ?
// If detect, move backward 0.5 second
// Spin left 1.5 second to change direction

Code explanation
When the program runs, at the LCD module there is message Press SW1 to wait for
pressing the SW1 button. After SW1 is pressed, CPU operate within the loop function to repeat
reading values from the ZX-03R sensors at ADC0 (left) and ADC2 (right) input of AT-BOT to store
at the variable left and right respectively. The values will be compared with the reference value
later. Therefore, the robot would have found the white surface or black lines.
If any ZX-03R sensor has the result value greater than REF (reference value equal 500),
the robot will decide that it has found the white surface. On the other hand, if the result value is
less than REF, the robot will consider that it has detected black lines already.
Then, the program will compare values of both variables based on 4 cases as follows:
1. if(left>REF && right>REF) : it is verification that the sensors have not detected
any line, true or not. If it is true (not found any strip), this will react to the robot to move
forward.
2. else if(left<REF && right>REF) : it is checking that the left sensor has found
only one line, true or not. If it is true (found a line or a black frame), this will respond to the
robot to back and then turn right in order to change the direction of motion.
3. else if(left>REF && right<REF) : it is examination that the right sensor has
detected only one line, true or not. If it is true (detected a line or black frame), it will affect the
robot to move backwards and then turn left to alter the direction of movement.
4. else if(left<REF && right>REF) : it is checkup that both side sensors have
found lines, true or not. It is true (discovered a line or black frame), will take action to the robot
moving back and turn left in order to change the direction of locomotion.
Note: REF value for comparing lines and surface in this experiment is 500, which comes from
the experiment 14. Developers may have different reference values depending on other external
factors, such as the amount of illumination from the sun around a test field, incandescent light
bulbs, or other light sources, including distance from sensors to the field floor.

Listing L15-1 : robo_inner.pde, the sketch file for demonstration the


AT-BOT move within the black frame by using two line sensors (final)

118 Robo-Creator : AT-BOT activity book

Experiment 16
AT-BOT with the Zigzag movement
This is an additional example of searching and detecting lines of the AT-BOT by
specifying that there are 2 parallel black lines. Then, the robot will be released to move
along the direction that is an angle about 45 degrees with a black line and the robot will
move straight constantly until it has detected a line and then turns. AT-BOT continues moving
forward till it will found a line again, so it will turn to another direction again. Normally, it

at least 30cm.

works like this so on. Therefore, the robot has a route of zigzag motion between both black
lines as the following figure.

During the zigzag movement in this experiment, use only 2 of ZX-03R sensors in the
left and right front like the experimet 15.

Make the demonstration field


Using a PP board with the size of 90x60 cm. from the experiment 14 (or possible
other sizes as required but should be large enough to let a robot be able to move
conveniently) to be a field of this experiment and stick 2 parallel lines of black tapes with
the distance at least 30 centimeters in order to make the robot have enough area in
locomotion.

Procedure
L16.1 Create the new sketch file. Type the Listing L15-1 and save as robo_pingponf.pde
L16.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L16.3 Place the robot at the start point at an angle about 45 degrees with a black line.

Robo-Creator : AT-BOT activity book 119

#include <atx.h>
// Include the main library
#define POW 80
// Set the motor power to 60%
#define REF 500
// Set the reference value as 500
unsigned int left,right;
// Declare the ZX-03R sensor variable
void forward(unsigned int time)
// Moving forward function
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for moving forward
}
void backward(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for moving backward
}
void turn_left(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for turning left
}
void turn_right(unsigned int time)
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for turning right
}
void setup()
{
lcd("SW1 Press!");
// Display title message for asking to press the SW1
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
left = analog(0);
// Read the left sensor data
// to store to the variable left
right = analog(2);
// Read the left sensor data
// to store to the variable right
if(left>REF && right>REF)
// Verify both sensor still not
// detect the black line
{
forward(1);
// No line detect,
// move forward for short time
}

Listing L16-1 : robo_pingpong.pde, the sketch file for demonstration


the AT-BOT move zigzag or similar pingpong movement by using two
line sensors (continue)

120 Robo-Creator : AT-BOT activity book

else if(left<REF && right>REF)


{
turn_right(400);
}
else if(left>REF && right<REF)
{
turn_left(400);
}
else if(left<REF && right<REF)
{
backward(1);
}

// Only the left sensor detect line ?


// If correct, spin right 0.4 second
// to change the direction
// Only the left sensor detect line ?
// If correct, spin left 0.4 second
// to change the direction
// Both sensors detect the line ?
// If detect, move backward for short time

}
Code explanation
Starting the program with show the message to ask pressing the SW1 button at the LCD
of AT-BOT. After SW1 is pressed, CPU will start working in the loop function to repeat reading
values of the ZX-03R sensors from ADC0 (left) and ADC1 (right) to store at the variable left and
right respectively. Verify and check which sensor can detect black lines similar the previois
experiment. Then, compare both variable values as follows:
1. if(left>REF && right>REF) : it is verification that both side sensors have not
found any line, true or not. If it is true (not found any strip), this will react to the robot to move
forward continuously.
2. else if(left<REF && right>REF) : it is checking that the left sensor has found
only one line, true or not. In this case, it is interpreted that the robot has found the upper border
line. The program will define the robot to turn right to avoid the line. Program developers can
modify a delay time value in turning to have a turning angle as required.
3. else if(left>REF && right<REF) : it is examination that the right sensor has
detected only one line, true or not. If it is true, in this case will interpret that the robot has found
the lower border line. Then, the program will control the robot to turn left to be out of the line.
4. else if(left<REF && right>REF) : it is checkup that the line sensors in both
sides have found lines, true or not. If it is true (discovered a line), will react to the robot moving
back for a short time to be away from the line.
Moreover, developers may add commands to instruct robots turning around and turn with
other different angles in order to create the type of complete zigzag movement, may involving
adjusting the position and the distance of the line sensors from the floor and mechanical
adjustments as necessary to make the motion of the robot follow the requirement.

Listing L16-1 : robo_pingpong.pde, the sketch file for demonstration


the AT-BOT move zigzag or similar pingpong movement by using two
line sensors (final)

Robo-Creator : AT-BOT activity book 121

L16.4 Run the program.


AT-BOT display shows the title message :

Press SW1
L16.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
The AT-BOT will move straight constantly until it has found a line, it will turn with an
angle about 90 degrees. Then, it will go ahead again till it finds a line of another side, so it
will turn and walk forward to turn back to another side in a zigzag motion. Normally, the
robot will move between black lines of both sides and program developers have to observe
whether the movement of the robot and the angle of turning to change a direction are
suitable or not. If it is not complete enough, program developers can adjust appropriate
delay time value for turning in the program until the function of the robot is satisfied.

122 Robo-Creator : AT-BOT activity book

Experiment 17
Line tracking robot using 2 sensors
After learning and testing the line detection from the experiment 15 and 16, In this
experiment presents the line tracking robot activity by using 2 of ZX-03R sensors. The
demonstration field of this experiment is shown in the figure L17-1. The conditions to act the
mission as follows:
1. Robot moves along the black line.
2. When detect the junction, robot must stop for 3 seconds .

Figure L17-1 : Illustration of the demonstration field of the experiment


17. It is made from the PP board and black tapes are stuck on it as
the pattern from above

Robo-Creator : AT-BOT activity book 123

Basic principle of the line tracking robot using 2 of sensors


The main purpose of a line tracking robot is to control the robot to move along the
line while 2 sensors are located astride the line. Therefore, there will be 4 cases of events,
which are used to specify the conditions of functioning as follows:

Case 1 : The robot is in the field lines or bestride the lines

Using 2 of ZX-03R sensors


at the left and right front side to control
the robot to move along the line.

Left sensor position :

located on the white surface


(sensors value > reference value)

Right sensor position :

located on the white surface


(sensors value > reference value)

Operation :

Robot moves straight and delay the time of the


motion for a short time.

124 Robo-Creator : AT-BOT activity book

Case 2 : The robot skips a line to the right

ZX-03R result data


left
(ADC0)

(ADC2)

right

Left sensor position :

located on the black line


(sensors value < reference value)

Right sensor position :

located on the white surface


(sensors value > reference value)

Operation :

Robot turn left with a short time to bestride the


black line again .

Case 3 : The robot tilts from the route to the left

ZX-03R result data


left

right

(ADC0)

(ADC2)

Left sensor position :

located on the white surface


(sensors value > reference value)

Right sensor position :

located on the black line


(sensors value < reference value)

Operation :

Robot turn right with a short time to bestride the


black line again .

Robo-Creator : AT-BOT activity book 125

Case 4 : The robot has found the crossline or may be junction

ZX-03R result data


left

right

(ADC0)

(ADC2)

Left sensor position :

located on the black line


(sensors value < reference value)

Right sensor position :

located on the black line


(sensors value < reference value)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

Make the demonstration field


Using a PP board with the size of 90x60 cm. from the experiment 14 (or possible
other sizes as required but should be large enough to let a robot be able to move
conveniently) to be a field of this experiment and stick the black tapes following the
figure L17-1

Procedure
L17.1 Create the new sketch file. Type the Listing L15-1 and save as robo_line1.pde
L17.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L17.3 Place the robot bestride the black line of the field.
L17.4 Run the program.
AT-BOT display shows the title message :
Press SW1

126 Robo-Creator : AT-BOT activity book

#include <atx.h>
#define POW 80
#define REF 500
unsigned int left,right;
void forward(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void backward(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void turn_left(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void turn_right(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void setup()
{
lcd("SW1 Press!");
sw1_press();
}
void loop()
{
left = analog(0);
right = analog(2);
if(left>REF && right>REF)

//
//
//
//
//

Include the main library


Set the motor power to 60%
Set the reference value as 500
Declare the ZX-03R sensor variable
Moving forward function

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

forward
forward
forward
forward
forward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

backward
backward
backward
backward
backward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 backward
Motor-1 backward
Motor-2 forward
Motor-3 forward
turning left

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 forward
Motor-1 forward
Motor-2 backward
Motor-3 backward
turning right

// Display title message


// for asking to press the SW1
// Wait until the SW1 is pressed

//
//
//
//
//
//

Read the left sensor data


to store to the variable left
Read the left sensor data
to store to the variable right
Verify both sensor still not
detect the black line

{
forward(1);

// If no detect the line, robot moves forward

Listing L17-1 : robo_line1.pde, the sketch file for demonstration the


AT-BOT moves along the black line by using 2 line sensors (continue)

Robo-Creator : AT-BOT activity book 127

else if(left<REF && right>REF)


{
turn_left(10);
}
else if(left>REF && right<REF)
{
turn_right(10);
}
else if(left<REF && right<REF)
{
pause();
sleep(3000);
forward(300);

// Only the left sensor detect line ?


// Spin left 0.01 second
// to move to bestride the line again
// Only the right sensor detect line ?
// Spin right 0.01 second
// to move to bestride the line again
// Both sensors detect the line ?
//
//
//
//

If detect, robot found the crossline


Stop movement 3 seconds
Move forward 0.3 second
to over the crossline

}
}

Listing L17-1 : robo_line1.pde, the sketch file for demonstration the


AT-BOT moves along the black line by using 2 line sensors (final)
L16.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
Robot moves along the black line and stop for 3 seconds when detect the junction.
After that moves along line continually.

128 Robo-Creator : AT-BOT activity book

Experiment 18
Line tracking robot using 3 sensors
This experiment is continuing of the experiment 17 by adding a ZX-03 sensor to apply
as the third line sensor. It will be installed at the middle position between the left and the
right sensor (In the experiment 17, if you did not take out the middle sensor at the bottom of
robot chasis, you will use it in this study). This is to help the AT-BOT able to detect the crossline
or junction better. For the test field, still use the same layout as the experiment 17 as well as
the hardware connection. The additional sensor is to connect with ADC1 port of AT-BOT.
The functioning conditions of AT-BOT robots in this study include:
1. A robot must move along the black line.
2. When a robot has discovered the crossline, it needs to stop and wait at the
junction for 3 seconds. And then, it will move on.

Principles of the line tracking robot by using 3 line sensors


There are 6 situations will probably happen in using 3 line sensors. User can apply
them on the determination of the functioning conditions as follows:

Case 1 : A robot is in the lines or bestride a line

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

Left sensor position :

located on the white surface (value > 500)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the white surface (value > 500)

Operation :

Robot moves straight and delay the time of the


motion for a short time.

Robo-Creator : AT-BOT activity book 129

Case 2 : A robot goes out of a line to the right

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

Left sensor position :

located on the black line (value < 300)

Middle sensor position :

located on the white surface (value > 500)

Right sensor position :

located on the white surface (value > 500)

Operation :

Robot turn left with a short time to bestride the


black line again .

Case 3 : A robot tilts from a route to the left

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

Left sensor position :

located on tthe white surface (value > 500)

Middle sensor position :

located on the white surface (value > 500)

Right sensor position :

located on the black line (value < 300)

Operation :

Robot turn right with a short time to bestride the


black line again .

130 Robo-Creator : AT-BOT activity book

Case 4 : A robot has found junction on the left

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

Left sensor position :

located on the black line (value < 300)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the white surface (value > 500)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

Case 5 : A robot has found junction on the right

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

Left sensor position :

located on the white surface (value > 500)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the black line (value < 300)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

Robo-Creator : AT-BOT activity book 131

Case 6 : A robot has found a junction, which is a crossline

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

Left sensor position :

located on the black line (value < 300)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the black line (value < 300)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

Procedure
L18.1 Create the new sketch file. Type the Listing L18-1 and save as robo_line2.pde
L18.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L18.3 Place the robot bestride the black line of the field.
L18.4 Run the program.
AT-BOT display shows the title message :
Press SW1
L18.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
Robot moves along the black line and stop for 3 seconds when detect the junction.
After that moves along line continually.

132 Robo-Creator : AT-BOT activity book

#include <atx.h>
#define POW 80
#define REF 500
unsigned int left,middle,right;
void forward(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void backward(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void turn_left(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void turn_right(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void setup()
{
lcd("SW1 Press!");
sw1_press();
}
void loop()
{
left = analog(0);

//
//
//
//
//

Include the main library


Set the motor power to 60%
Set the reference value as 500
Declare the ZX-03R sensor variable
Moving forward function

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

forward
forward
forward
forward
forward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

backward
backward
backward
backward
backward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 backward
Motor-1 backward
Motor-2 forward
Motor-3 forward
turning left

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 forward
Motor-1 forward
Motor-2 backward
Motor-3 backward
turning right

// Display title message


// for asking to press the SW1
// Wait until the SW1 is pressed

// Read the left sensor data


// to store to the variable left
mid = analog(1);
// Read the middle sensor data
// to store to the variable mid
right = analog(2);
// Read the left sensor data
// to store to the variable right
if(left>REF && mid<REF && right>REF)
// Check the robot bestride the line or not ?
{
forward(1);
// If correct, robot moves forward with a short time
}

Listing L18-1 : robo_line2.pde, the sketch file for demonstration the


AT-BOT moves along the black line by using 3 line sensors (continue)

Robo-Creator : AT-BOT activity book 133

else if(left<REF && mid>REF && right>REF)


// Check the robot to move rightward away of the line
{
turn_left(10);
// Spin left 0.01 second for trying to bestride the line again
}
else if(left>REF && mid>REF && right<REF)
// Check the robot to move leftward away of the line
{
turn_right(10);
// Spin right 0.01 second for trying to bestride the line again
}
else if(left<REF && mid<REF && right<REF)
// Check the robot to found the crossline
{
pause();
// If found, stop movement
sleep(3000);
// Delay 3 seconds
forward(300);
// Move forward 3 seconds to over the crossline
}
else
// For another conditions
{
forward(1);
// Move forward with a short time
}
}

Code explanation
There are 4 conditions of the sensor operation to control the AT-BOT movement.
1. if(left>REF && mid<REF && right>REF) : it is checking about the robot still
bestride the line or not. If true, robot will move forward with a short time
2. else if(left<REF && mid>REF && right>REF) : it is checking about the robot
moves rightward away of the line or not. If true, robot is controlled to spin left with a short time
to back to bestride the line again.
3. else if(left>REF && mid>REF && right<REF) : it is checking about the robot
moves leftward away of the line or not. If true, robot is controlled to spin right with a short time
to back to bestride the line again.
4. else if(left<REF && mid<REF && right<REF) : it is checking about the robot
found the crossline or not. If true, it will stop at the junction for 3 seconds after that moves
forward 0.3 second to over the crossline.

Listing L18-1 : robo_line2.pde, the sketch file for demonstration the


AT-BOT moves along the black line by using 3 line sensors (final)

134 Robo-Creator : AT-BOT activity book

Additional conclusion
From the experiment 17 and 18, the robot moves along the line with same concept
but their difference is the number of line sensors because the test field is not much
complexity so the application of only 2 line sensors can accommodate. However, in case
that a test field is more complex such as a left junction and a right junction available.
Using 3 line sensors will be more efficient than only 2 sensors. Consideration of comparable
cases will be shown as follows:

(1) The left junction case


(1.1) Using 2 of line sensors

ZX-03R result data

ZX-03R result data

left
(ADC0)

(ADC2)

left
(ADC0)

(ADC2)

right

right

The result is only the left sensor detects the black line. It is inconclusive. Now,
the robot is moving out of the line to the right or straight to the left junction.

(1.2) Using 3 of line sensors

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

With this techniques, the left and middle sensors will detect lines.
The conclusion is clear that now the robot is moving straight to the left junction.

Robo-Creator : AT-BOT activity book 135

(2) The right junction case


(2.1) Using 2 of line sensors

ZX-03R result data

ZX-03R result data

left

right

left

right

(ADC0)

(ADC2)

(ADC0)

(ADC2)

The result is only the right sensor detects the black line. It is inconclusive.
Now, the robot is moving out of the line to the left or straight to the right junction.

(1.2) Using 3 of line sensors

With this techniques, the right and middle sensors will detect lines.
The conclusion is clear that now the robot is moving straight to the right junction.

136 Robo-Creator : AT-BOT activity book

Experiment 19
Advance line tracking robot
This experiment expands on the experiment 18 by adding the complexity of the
test field and further conditions of movement along lines as shown in the figure L19-1. ATBOT robots have to use 3 line sensors in the operation of this mission. The conditions of the
functioning compose of:
1. The robot has to be released from the start point.
2. The robot has to move to pass the left and the right crossroads of the way to
the triple junction.
3. When the robot has arrived to the junction, it must turn left and move along
the line in clockwise direction.

Creating the test field


Using a PP board with the size of 120 x 60cm. or it is possible to use other sizes to build
the field but they should be large enough to support the convenient movement of a
robot and stick it with black tapes according to the figure L19-1.

About the robot


Use the AT-BOT that is installed 3 of line sensors from the experiment 17 and 18

Procedure
L19.1 Create the new sketch file. Type the Listing L18-1 and save as robo_line3.pde
L19.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

START

Junction with
crossline

Figure L19-1 : The test field of the advance line tracking robot in the
experiment 19

Robo-Creator : AT-BOT activity book 137

#include <atx.h>
#define POW 80
#define REF 500
unsigned int left,middle,right;
void forward(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void backward(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void turn_left(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void turn_right(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void setup()
{
lcd("SW1 Press!");
sw1_press();
}
void loop()
{
left = analog(0);

//
//
//
//
//

Include the main library


Set the motor power to 60%
Set the reference value as 500
Declare the ZX-03R sensor variable
Moving forward function

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

forward
forward
forward
forward
forward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

backward
backward
backward
backward
backward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 backward
Motor-1 backward
Motor-2 forward
Motor-3 forward
turning left

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 forward
Motor-1 forward
Motor-2 backward
Motor-3 backward
turning right

// Display title message


// for asking to press the SW1
// Wait until the SW1 is pressed

// Read the left sensor data


// to store to the variable left
mid = analog(1);
// Read the middle sensor data
// to store to the variable mid
right = analog(2);
// Read the left sensor data
// to store to the variable right
if(left>REF && mid<REF && right>REF)
// Check the robot bestride the line or not ?
{
forward(1);
// If correct, robot moves forward with a short time
}
else if(left<REF && mid>REF && right>REF)
// Check the robot to move rightward away of the line

Listing L19-1 : robo_line3.pde, the sketch file for demonstration the


AT-BOT moves along the black line by using 3 line sensors on the
complex route (continue)

138 Robo-Creator : AT-BOT activity book

{
turn_left(10);
// Spin left 0.01 second for trying to bestride the line
}
else if(left>REF && mid>REF && right<REF)
// Check the robot to move leftward away of the line
{
// Spin right 0.01 second for trying to bestride the line
turn_right(10);
}
else if(left<REF && mid<REF && right>REF)
// Check the robot to detect the left junction
{
forward(10);
// Move forward 0.01 second to pass the left junction
}
else if(left>REF && mid<REF && right<REF)
// Check the robot to detect the right junction
{
forward(10);
// Move forward 0.01 second to pass the right junction
}
else if(left<REF && mid<REF && right<REF)
// Check the robot to detect the crossline
{
// Move forward 0.1 second to pass the crossline
forward(100);
turn_left(200);
// Spin left 0.2 second for trying to bestride the line
// at the crossline again
}
}

Code explanation
There are 6 conditions of the sensor operation to control the AT-BOT movement.
1. if(left>REF && mid<REF && right>REF) : it is checking about the robot still
bestride the line or not. If true, robot will move forward with a short time
2. else if(left<REF && mid>REF && right>REF) : it is checking about the robot
moves rightward away of the line or not. If true, robot is controlled to spin left with a short time
to back to bestride the line again.
3. else if(left>REF && mid>REF && right<REF) : it is checking about the robot
moves leftward away of the line or not. If true, robot is controlled to spin right with a short time
to back to bestride the line again.
4. else if(left<REF && mid<REF && right>REF) : it is checking about the robot
detect the left junction or not. If true, robot will move forward to pass this junction.
5. else if(left>REF && mid<REF && right<REF) it is checking about the robot
detect the right junction or not. If true, robot will move forward to pass this junction.
6. else if(left<REF && mid<REF && right<REF) : it is checking about the robot
found the crossline or not. If true, robot will move forward 0.1 second and turn left 0.3 second
after that back to check all line tracking conditions continually

Listing L19-1 : robo_line3.pde, the sketch file for demonstration the


AT-BOT moves along the black line by using 3 line sensors on the
complex route (final)

Robo-Creator : AT-BOT activity book 139

L19.3 Place the robot bestride the black line of the field.
L19.4 Run the program.
AT-BOT display shows the title message :
Press SW1
L18.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
The robot AT-BOT will move along the black line until the robot has found the
triple junction, it will turn left and move along the curve in the clockwise direction constantly.

Start

Junction with
crossline

More information
From the previous experiments, the test fields have black strips and white surface.
If you will change to black surface and white lines, programming to control functioning
will be able to use the principles of line and surface analysis of the experiment 14 and the
principles of driving robots from the experiment 15 to 18 as guidelines of program
development.
For example, if you would like to test that a robot has found any crossline or not.
In the case that the field surface is white and the strips are black, apply this
condition to check.
else if(left<REF && mid<REF && right<REF)
But if the field surface is black and the strips are white, use this condition to verify.
else if(left>REF && mid>REF && right>REF)
Because strips have become white, if read a value from a line sensor of each
position and it shows that the value is greater than the reference value. Hence, the line
sensor of that position has found a white line.

140 Robo-Creator : AT-BOT activity book

Experiment 20
Line tracking robot with the white line mission
This experiment represents a guideline of the control program development for ATBOT to move along the white lines on a black surface. The appearance of the test field in
this experiment is opposite to that of the experiment 15 to 19, by which the test field in this
trial has the same feature with which of the experiment 18 but colors are changed that
black lines converts to white lines and the white surface change to black surface as
demonstrated in the figure L20-1.
The conditions of functioning include:
1. The robot must move along the white line.
2. When the robot detects the crossline, it will have to stop and wait for 3 seconds
approximately and then it will move on.

Making the test field


Using a black PP board in the size of 90x60 cm. (possible other sizes as you wish but
the size should be large enough that the robot can move around comfortably) as the
field surface and stick with white tape to create a curve frame and 2 points of the
intersections in accordance with the example in the figure L20-1.

Figure L20-1 : The test field for Line tracking robot in the experiment 20

Robo-Creator : AT-BOT activity book 141

Principles of the line tracking robot with white line by using


3 line sensors
There are 6 situations will probably happen in using 3 line sensors. User can apply
them on the determination of the functioning conditions as follows:

Case 1 : A robot is in the lines or bestride a line

ZX-03R result data


left
left
(ADC0)

mid

right

(ADC1)

(ADC2)

Left sensor position :

located on the black line (value < 300)

Middle sensor position :

located on the white surface (value > 500)

Right sensor position :

located on the black line (value < 300)

Operation :

Robot moves straight and delay the time of the


motion for a short time.

Case 2 : A robot goes out of a line to the right

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

Left sensor position :

located on the white surface (value > 500)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the black line (value < 300)

Operation :

Robot turn left with a short time to bestride the


black line again .

142 Robo-Creator : AT-BOT activity book

Case 3 : A robot tilts from a route to the left

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

Left sensor position :

located on tthe black line (value < 300)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the white surface (value > 500)

Operation :

Robot turn right with a short time to bestride the


black line again .

Case 4 : A robot has found junction on the left

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

Left sensor position :

located on the white surface (value > 500)

Middle sensor position :

located on the white surface (value > 500)

Right sensor position :

located on the black line (value < 300)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

Robo-Creator : AT-BOT activity book 143

Case 5 : A robot has found junction on the right

ZX-03R result data


left
(ADC0)

mid

right

(ADC1)

(ADC2)

Left sensor position :

located on the black line (value < 300)

Middle sensor position :

located on the white surface (value > 500)

Right sensor position :

located on the white surface (value > 500)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

Case 6 : A robot has found a crossline

ZX-03R result data


left

mid

right

(ADC0)

(ADC1)

(ADC2)

Left sensor position :

located on the black line (value < 300)

Middle sensor position :

located on the black line (value < 300)

Right sensor position :

located on the black line (value < 300)

Operation :

choose the robot to move forward, turn left,


turn right, stop or backward as needed.
It is based on the goal of the mission.

144 Robo-Creator : AT-BOT activity book

Procedure
L20.1 Create the new sketch file. Type the Listing L20-1 and save as robo_line4.pde
L20.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L20.3 Place the robot bestride the white line of the field.
L20.4 Run the program.
AT-BOT display shows the title message :
Press SW1
L20.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
Robot moves along the white line and stop for 3 seconds when detect the junction.
After that moves along line continually.

#include <atx.h>
#define POW 80
#define REF 500
unsigned int left,middle,right;
void forward(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void backward(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,-POW);
motor(3,-POW);
sleep(time);
}
void turn_left(unsigned int time)
{
motor(0,-POW);
motor(1,-POW);
motor(2,POW);
motor(3,POW);
sleep(time);
}
void turn_right(unsigned int time)
{
motor(0,POW);
motor(1,POW);
motor(2,-POW);

//
//
//
//
//

Include the main library


Set the motor power to 60%
Set the reference value as 500
Declare the ZX-03R sensor variable
Moving forward function

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

forward
forward
forward
forward
forward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0
Motor-1
Motor-2
Motor-3
moving

backward
backward
backward
backward
backward

//
//
//
//
//

Drive
Drive
Drive
Drive
Delay

the
the
the
the
for

Motor-0 backward
Motor-1 backward
Motor-2 forward
Motor-3 forward
turning left

// Drive the Motor-0 forward


// Drive the Motor-1 forward
// Drive the Motor-2 backward

Listing L20-1 : robo_line4.pde, the sketch file for demonstration the


AT-BOT moves along the white line by using 3 line sensors (continue)

Robo-Creator : AT-BOT activity book 145

motor(3,-POW);
sleep(time);
}
void setup()
{
lcd("SW1 Press!");
sw1_press();
}
void loop()
{
left = analog(0);

// Drive the Motor-3 backward


// Delay for turning right

// Display title message


// for asking to press the SW1
// Wait until the SW1 is pressed

// Read the left sensor data


// to store to the variable left
mid = analog(1);
// Read the middle sensor data
// to store to the variable mid
right = analog(2);
// Read the left sensor data
// to store to the variable right
if(left<REF && mid>REF && right<REF)
// Check the robot bestride the line or not ?
{
forward(1);
// If correct, robot moves forward with a short time
}
else if(left>REF && mid<REF && right<REF)
// Check the robot to move rightward away of the line
{
// Spin left 0.01 second for trying to bestride the line again
turn_left(10);
}
else if(left<REF && mid<REF && right>REF)
// Check the robot to move leftward away of the line
{
// Spin right 0.01 second for trying to bestride the line again
turn_right(10);
}
else if(left>REF && mid>REF && right>REF)
// Check the robot to found the crossline
{
pause();
// If found, stop movement
sleep(3000);
// Delay 3 seconds
forward(300);
// Move forward 3 seconds to over the crossline
}

Listing L20-1 : robo_line4.pde, the sketch file for demonstration the


AT-BOT moves along the white line by using 3 line sensors (final)

146 Robo-Creator : AT-BOT activity book

Robo-Creator : AT-BOT activity book 147

Chapter 9
AT-BOT with touchless object avoiding
In this chapter, it will present the application of a sensing module and a distance
measurement with the infrared distance sensor or Infrared ranger GP2D120 with AT-BOT. .
As the result, the AT-BOTs have ability to detect objects without contact with objects.
Therefore, the robots are able to go straight or avoid more wisely.

9.1. The characteristics of the module GP2D120


The GP2D120 is the infrared distance sensing module, which has three used pins
composed of the power supply pin (Vcc), the ground pin (GND), and the output voltage
pin (Vout). Reading voltage values from GP2D120 will have to wait until the preparation
phase is finished by which it takes about between 32.7 and 52.9 milliseconds (1 millisecond
equals 0.001 seconds). Hence, the reading of the voltage should wait until after the first
period.
The output voltage of GP2D120 at the distance 30cm. with the power supply +5V is
in the range from 0.25 to 0.55V and the median is 0.4V. Therefore, the transition period of
the output voltage at the distance 4cm. is 2.25V 0.3V.

Infrared LED

Infrared Receiver

Output voltage (V)


2.8
2.4
2.0

GP2D120

1.6

Vout GND Vcc

1.2
0.8
0.4

Supply

0
38.39.6 ms

Measurement

Vout

1st measure

Not stable

12

16

20

24

28

32

Distance (cm)
2nd measure

1st output

n
measure

2nd output

n output

5 ms

Figure 9-1 : Body, Pin assignment and Characteristic graph of the


Infrared ranger GP2D120

148 Robo-Creator : AT-BOT activity book

9.2 How to install the GP2D120 with AT-BOT


From the chapter 7, AT-BOT can keep away from the obstacles by touching or
bumping. As this chapter, the contents will focus on the ability development of AT-BOT to
another level by using the GP2D120 module to help with the detection of the distance
from obstacles in order to let the robots capable of getting away from obstruction without
touching.
The installation of the GP2D120 can be done as follows:
(1) Attach a right-angle joiner with the front of the robot at the middle with screws 3
x 10mm. screws and 3mm. nuts.
(2) Fasten a 5-hole strip joiner on the right-angle joiner from the first step with 3 x
10mm. screws and 3mm. nuts at the 3rd hole (the middle) of the strip joiner.
(3) Join the GP2D120 with the strip joiner by 2 sets of 3 x 10mm. screws and 3mm.
nuts by which the screws will be strung through the holes of the wings used to fasten the
module and the notches at the end of the strip joiner.

GP2D120 module
GP2D120

Front of the
AT-BOT

GP2D120

43 ADC3
42 ADC2
41 ADC1
40 ADC 0
46 ADC6

SW1
SW2

45 ADC5

49

44 ADC4

50
48

MOTOR

BATTERY LEVEL

C r e a t o r e > >> > > > > >

ADC7

SERVO PORT

PC6
15 PC7

c o n t r o l l er R b o a r d

KNOB

14

44 ADC4

45 ADC5

9 10 11 12 13

46 ADC6

40 ADC 0

48

USB DATA

41 ADC1

50

MOTOR

7.2-9V BATT.

42 ADC2

SW2

15 PC7

BATTERY LEVEL

START

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

43 ADC3

49

PC6

C r e a t o r e > >> > > > > >

14

SW1

c o n t r o l l er R b o a r d

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

ADC7

SERVO PORT

9 10 11 12 13
S

E2

RESET

USB DATA

KNOB

ON

ON
E2

RESET

7.2-9V BATT.

connect to ADC3

START

Robo-Creator : AT-BOT activity book 149

(4) Plug the signal cable of the GP2D120 to ADC3 port of the AT-BOT robot that can
detect the obstacles without any contact and be ready for the programming later on.

9.3 gp2d120_lib.h - the library for GP2D120


The gp2d120_lib.h file contains an instruction set or a C/C++ language program
function to perform the GP2D120 module. Before running a function in this library,
developers have to append the library file at the beginning of the program with the
command below.
#include <gp2d120_lib.h>

9.3.1. Hardware connection


Because the GP2D120 is a sensor that shows the result as DC voltage related with a
measured distance. Starting the use of the module should connect with any analog input
of the AT-BOT. Includes ADC0 to ADC6.

9.3.2 getdist function


In the gp2d120_lib.h library , there is a function to read a measured distance from
the GP2D120 in the unit of centimeter.
Syntax

unsigned int getdist(char adc_ch)


Parameter
adc_ch : used to define analog input connecting with the GP2D120.
Return value
The distance in the centimeter unit.

150 Robo-Creator : AT-BOT activity book

Experiment 21
Distance meaturement by GP2D120
This experiment is a programming to read distance values from the module GP2D120
by which the readable values are shown at the LCD module of AT-BOT.

Additional hardware connection


AT-BOT robot already installed with the GP2D120 module and connect to ADC3 port.

Procedure
L21.1 Create the new sketch file. Type the Listing L21-1 and save as gp2d120_test.pde
L20.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
#include <atx.h>
//
#include <gp2d120_lib.h>
//
unsigned int dist;
//
void setup()
{}
void loop()
{
dist = getdist(3);
//
if(dist>=4 && dist<=32)
//
{
lcd(Distance: %d cm ,dist); //
}
else
{
lcd(Out of Range!
);
//
//
}
sleep(100);
//
}

Include the main library


Include the GP2D120 library
Declare the distance measurment variable

Read value from GP2D120 at ADC3 input


Compare with the reference value
Show the distance at AT-BOTs display

Show the warning message


when the measured data is

out of range

Delay 0.1 second

Code explanation
The distance value that read from GP2D120 with getdist function is stored in the variable
dist. Then, the value will be analyzed whether it is in the range of 4 to 32cm. or not before
displayed at the LCD module. If the value is less than 4 and greater than 32, it is considered that
the data is not reliable. After this, the message as Out of Range! will be displayed at the LCD
module.

Listing L21-1 : gp2d120_test.pde, the sketch file for reading the


distance measurement from GP2D120

Robo-Creator : AT-BOT activity book 151

L21.3 Run the program.


AT-BOT display shows the title message :

Distance: xxx cm
therefore xxx is the distance value in centimetre unit.
and displays the message

Out of Range!
when the distance value is out of the range 4 to 32cm.
L21.4 Put an object or your hand to block in the front of the GP2D120 in the operating
distance, which is 4 to 32 cm. Then, move in and away the object from the GP2D120.
Finally, monitor the readable distance value at the LCD module.
If the object is in the operating distance of the GP2D120, at the LCD module, there
will be a display of the distance value in centimetre. But if the object is not in the operating
distance, at the LCD module will be displayed the message Out or Range!, instead.

4cm.
7.2-9V BATT.

14

PC6

15 PC7

MOTOR

+
S

10 11 12 13

BATTERY LEVEL

SERVO PORT

0
RESET

KNOB

ADC7

SW1

49

SW2

50

48

move object

E2

TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

ON
USB DATA

GP2D120

c o n t r o l l er R b o a r d
C r e a t o r e > >> > > > > >
4
43 ADC3

42 ADC2

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

START

connect to ADC3

32cm.

152 Robo-Creator : AT-BOT activity book

Experiment 22
Touchless object avoiding robot
This is the ability development of AT-BOT robos. Previously, AT-BOT used to avoid
obstacles by bumping. However, this experiment is different from the mentioned experiment
that this experiment will use the GPD120 sensor to help for detecting and avoiding the
obstacles without contact. There are conditions of functioning as follows:
1. In case that a robot does not detect any obstruction in the distance of 14 cm.
The robot will move forward.
2. In case that a robot finds obstruction in the distance less than 14 cm. The
robot will move backwards and then turn left to change the movement direction.

Procedure
L22.1 Create the new sketch file. Type the Listing L22-1 and save as robo_ranger.pde
L22.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L22.3 Place the robot on the floor. Place some obstacle such as box or can to set the
demonstration field.
L22.4 Run the program.
AT-BOT display shows the title message : Press SW1
L18.5 Press the SW1 button on the AT-BOT. Observe the robot operation.
The AT-BOT will go straight constantly. When the robot will be able to detect
obstruction, it will reverse and turn left. After that, the AT-BOT will move forward in order to
avoid the obstruction. If the robot turns around and finds an object, it will go backward
again and turn left. The pattern will be repeated until the robot will be able to pass objects
and move straight later on.
Obstacle

Obstacle

Obstacle

Obstacle

Robo-Creator : AT-BOT activity book 153

#include <atx.h>
// Include the main library
#include <gp2d120_lob.h>
// Include the GP2D120 library
#define POW 80
// Set the motor power to 80%
unsignd int dist;
// Declare the distance variable
void forward(unsigned int time)
// Moving forward function
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for moving forward
}
void backward(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for moving backward
}
void turn_left(unsigned int time)
{
motor(0,-POW);
// Drive the Motor-0 backward
motor(1,-POW);
// Drive the Motor-1 backward
motor(2,POW);
// Drive the Motor-2 forward
motor(3,POW);
// Drive the Motor-3 forward
sleep(time);
// Delay for turning left
}
void turn_right(unsigned int time)
{
motor(0,POW);
// Drive the Motor-0 forward
motor(1,POW);
// Drive the Motor-1 forward
motor(2,-POW);
// Drive the Motor-2 backward
motor(3,-POW);
// Drive the Motor-3 backward
sleep(time);
// Delay for turning right
}
void setup()
{
lcd("SW1 Press!");
// Display title message for asking to press the SW1
sw1_press();
// Wait until the SW1 is pressed
}
void loop()
{
dist = getdist(3);
// Read the distance from GP2D120 at ADC3 input
if(dist>=4 && dist<15)
// Found the object within 4 to 14cm. or not ?
{
backward(500);
// If found, move backward 0.5 second
turn_left(800);
// Spin left 0.8 second
}
// to change the movement direction
else
{
forward(1);
// Move forward with a short time
}
}

Listing L22-1 : robo_ranger.pde, the sketch file for demonstration the


AT-BOT avoids the object without contact by using GP2D120 sensor
(continue)

154 Robo-Creator : AT-BOT activity book

Code explanation
At the beginning of the program, there will be a display of the text Press SW1 at the LCD
module. When pressing the SW1 is done, the CPU will operate in the loop function to repeat
reading values from the GP2D120, which is connected to the ADC3 input.
Those values will be collected at the variable dist. Then, the program will take distance
values to compare based on these cases:
1. if(dist>=4 && dist<15) : it is verification that if the robot finds an object in the
range of 4 to 14 cm. or not. If it is true (a barrier present), the program will respond the robot
by moving backward and turning left to avoid the obstruction.
2. else : if the condition of the case 1 is not true, that is meant that there is no barrier
in the interesting distance, so the AT-BOT is designed to move forward.
More information
The development of the control program for AT-BOTs cooperated with the GP2D120 will
run the getdist function from the gp2d120_lib.h library to read distance values from barriers
in the centimetre.

Listing L22-1 : robo_ranger.pde, the sketch file for demonstration the


AT-BOT avoids the object without contact by using GP2D120 sensor
(final)

Robo-Creator : AT-BOT activity book 155

Chapter 10
AT-BOT with Servo motor
In this chapter, it will introduce the control of servo motors of the AT-BOT. It can
drive 6 of 4.8 to 6V R/C servo motors through the ports 8 to 13.

10.1 Servo motor introduction


Figure 10-1 shows a drawing of a Standard Servo. The plug is used to connect the
servo motor to a power source (Vdd and Vss) and a signal source (a microcontroller I/O
pin). The cable conducts Vdd, Vss and the signal line from the plug into the servo motor.
The horn is the part of the servo that looks like a four-pointed star. When the servo is running,
the horn is the moving part that the microcontroller controls. The case contains the servos
control circuits, a DC motor, and gears. These parts work together to take high/low signals
from the microcontroller and convert them into positions held by the servo horn.
Figure 10-2 shows the servo motor cable assignment. It has 3 wires with difference
color; Black for GND or Vss or Negative pole, Red for Vdd or Servo motor supply voltage
and White (sometime is yellow or brown) wire for signal.
The servo motor plug standard has 2 types; S-type and J-type are shown in the
figure 10-3.

Horn
Plug

Cable

STANDARD
SERVO MOTOR

Case

Figure 10-1 : Standard servo motor physical

156 Robo-Creator : AT-BOT activity book

(a) S-type plug

(b) J-type plug

Figure 10-2 : Standard Servo motor Figure 10-3 : Standard Servo motor
cable assignment
plug type
Controlling of the servo motors is used to pulse controlling. The control pulse is positive
going pulse with length of 1 to 2 ms which is repeated about 50 to 60 times a second. You
can check the details in the figure 10-4. Start by generating a pulse a period 20 millisecond
and adjust the positive pulse width 1 millsecond. The servo motor will move the horn to last
left position. The pulse width 1.5 millisecond move the servo horn to center and pulse
width 2 millsecond causes the servo horn to last right position.
The important specification of servo motor are 2 points, Speed or Servo turn rate or
transit time and Torque. The servo turn rate, or transit time, is used for determining servo
rotational velocity. This is the amount of time it takes for the servo to move a set amount,
usually 60 degrees. For example, suppose you have a servo with a transit time of 0.17sec/
60 degrees at no load. This means it would take nearly half a second to rotate an entire
180 degrees. More if the servo were under a load. This information is very important if high
servo response speed is a requirement of your robot application. It is also useful for
determining the maximum forward velocity of your robot if your servo is modified for full
rotation. Remember, the worst case turning time is when the servo is at the minimum
rotation angle and is then commanded to go to maximum rotation angle, all while under
load. This can take several seconds on a very high torque servo.
Torque is the tendency of a force to rotate an object about an axis. The torque
unit is ounce-inches (oz-in) or kilogram-centimetre (kg-cm). It tell you know about this servo
motor can drive a load weight in 1 oz. to move 1 inche or 1kg. weight to moved 1
centimeter (1oz. = 0.028kg. or 1kg. = 25.274oz.). Normally the RC servo motor has 3.40 kgcm/47oz-in torque.

Robo-Creator : AT-BOT activity book 157

1 to 2 millsecond pulse

(a) Servo motor control pulse


20ms period

1 millisecond pulse

(b) 1 millisecond pulse causes


servo horn moves anti-clockwise
direction to last right position (0o)

STANDARD
SERVO MOTOR

1.5 millisecond pulse

(c) 1.5 millisecond pulse causes


servo horn moves to center position

STANDARD
SERVO MOTOR

2 millisecond pulse

(d) 2 millisecond pulse causes


servo horn moves clockwise
direction to last left position (180o)

STANDARD
SERVO MOTOR

Figure 10-4 : Timing diagram of the servo motor control pulse

158 Robo-Creator : AT-BOT activity book

10.3 Servo motor control management of AT-BOT


robot
In an AT-BOT robot, using the port pins 8 to 13 (sorted by the requirements of Wiring
I/O and required to use these pin names in the programming in Wiring IDE) to generate
pulse signal in order to drive servo motors.

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

The power supply voltage of servo motors comes from a battery connected with
the circuit board of ATX through the control circuit of the constant power supply. At +6V
can supply the electric current 1500mA, so it can be applied to all models of small servo
motors, which use the power supply in the range of 4.8 to 6V.

C r e a t o r e > >> > > > > >


3

c o n t r o l l er R b o a r d
USB DATA

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

E2

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
9

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RESET

14

PC6

15 PC7

MOTOR

6 of Servo motor output


[8 to 13]

Figure 10-5 : Demonstration of servo motor outputs of the AT-BOT

Robo-Creator : AT-BOT activity book 159

10.4 Servo motor library


One important thing of controlling servo motors is to create pulse signal, which
comes from programming to command the microcontroller to produce desirable pulse
signal. To help the programming controlled by C language in AT-BOT robots more
convenient, the library file servo.h has to be attached with the program.
This library file supports all functions for controlling 6 servo motor outputs of the ATX
controller board. This library must be included at the top of the program with the command
#include as follows :
#include <servo.h> or #inclue <atx.h>
There is one function. It is servo.

Syntax
void servo(unsigned char _ch, int _pos)

Parameter
_ch - Servo motor output (8 to 13)
Define as 8 for servo motor output #1 (8)
Define as 9 for servo motor output #2 (9)
Define as 10 for servo motor output #3 (10)
Define as 11 for servo motor output #4 (11)
Define as 12 for servo motor output #5 (12)
Define as 13 for servo motor output #6 (13)
_pos - Set the sevo motor shaft poistion (0 to 180 and -1)
If set to -1, disable selected servo motor output

160 Robo-Creator : AT-BOT activity book

Experiment 23
AT-BOT controlled the servo motor
In this experiment presents about how to test the positional control of the servo
motorat ch. 10 of an AT-BOT by pressing the SW1 (adding a positional value) and SW2
(reducing a position value) swithes as well as monitoring a value of the reference position
of the servo motor. The reference position value will be shown at the LCD module on the
AT-BOT robot.

Additional hardware connection


Connect a servo motor to port 10 of servo motor output of the AT-BOT

Procedure
L23.1 Create the new sketch file. Type the Listing L23-1 and save as servo_test.pde
L23.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.
L23.3 Run the program.
AT-BOT display shows the title message :

Position: xx

41 ADC1

40 ADC0

46 ADC6

45 ADC5

44 ADC4

42 ADC2

43 ADC3

START

therefore xx is the servo motor shafts position number

Pos ition: >20> > >>


con troll er R boa rd

Servo motor

Decrease
position

USB DATA

ON
TWI
UART1
0 SCL 1 SDA 2 RX1 3 TX1

KNOB

ADC7

SW1

49

SW2

50

48

SERVO PORT
8

BATTERY LEVEL

10 11 12 13
S

7.2-9V BATT.

RESET

Increase
position

E2

14

PC6

15 PC7

MOTOR

STANDARD
SERVO MOTOR

Robo-Creator : AT-BOT activity book 161

L23.4 Try to press the SW1 switch on the AT-BOT to increase the postion value.
The servo motor shaft is driven to upper position. Also at the display of AT-BOT shows
the position value. However if the value is greater than 90 greatly, tthe servo motor may
not stable to maintain position.
L23 .5 Press the SW2 switch on the AT-BOT to decrease the postion value.
The servo motor shaft is driven to lower position. At the display of AT-BOT shows the
position value. However if the value is less than 20 greatly, the servo motor may not stable
to maintain position also.
Note: About the positional range of servo motors, each manufacturer may have
different positional range or direction of position. Therefore, developers have to create
programming about testing positional values of servo motors in order to know how many
a positional value is in the control program when you need to control a servo motor to be
located at a desired position.
#include <atx.h>
unsigned int pos=0;
void setup()
{}
void loop()
{
lcd(Position: %d
servo(10,pos);
if(sw1()==0)
{
pos++;
sleep(100);
}
if(sw2()==0)
{
pos - - ;
sleep(100);
}
}

// Include the main library


// Declare the servo motor position variable

,pos);

// Show the position value


// Set the postion to servo motor output #10
// Check the SW1 pressing
// Increase the position value
// Debouncing delay
// Check the SW2 pressing
// Decrese the position value
// Debouncing delay

Code explanation
The program will operate in the loop function to repeat showing the result of the positional
values of the servo motor at port 10 of AT-BOT and define the values constantly. At the same
time, the function will verify addition and deduction of the positional values of servo motor shaft
form pressing the switches SW1 and SW2 respectively. The program will work like this repeatedly
and continuously.

Listing L23-1 : servo_test.pde, the sketch file for controlling


the servo motor shaft position of the AT-BOT

You might also like