Students Guide For Making Your Own Small Robot
Students Guide For Making Your Own Small Robot
Small Robot
E.T. Bryant
Copyright © 2019 by E.T. Bryant. All rights reserved worldwide.
No part of this publication may be replicated, redistributed, in any form without the
prior written consent of the author. The programming code is the only exception.
Dedicated to Troy
Introduction
My previous book Student’s Guide to Building Robots describe the robots I
have built. It offered suggestions for how you might build a robot. I also gave all
the code used in my robots. In this book I give more detail about how to build your
own robot. I start by guiding you through identifying the problem your robot will
solve in the What’s the Problem chapter.
In the Algorithm chapter you can lay out the steps used in your code for the
robot to solve the problem. You can use a simple algorithm, or one with pseudo –
code. A flowchart is explained as a way to solve logic errors in your code. The
Components chapter will help you to identify the components you will need in
building a robot similar to my R3D4 robot.
The Platforms chapter explains different types of robot platforms you might
be able to use. You are going to need motors and in the Motors chapter I describe
the motors I have used and some you might consider. The motors will need a
motor controller and they are explained in that chapter as well.
Batteries are needed to power your robot and in the Batteries chapter you
learn about the different types of batteries I have used. Some of my robots use
Raspberry Pi computers but for your first robot you might want to use a micro-
controller. Many of my robots use both, so in the Micro-controllers chapter I talk
about the Arduino micro-controllers I have used.
Sensors are important for detecting distance and also motion. The Sensors
chapter gives details on how to connect them to your robot. After you determine
what sensors you will need you can start assembly of your robot, if you are trying
to build one like my R3D4. You will find assembly instructions in the Assembly
chapter.
Finally, you are ready to program your R3D4 robot in the Programming the
Robot chapter. If you are unfamiliar with the Arduino IDE, I explain the different
menus and then the code I used in my robot for navigation, the MP3 player shield,
and for the 7 color LEDs. As in my previous book it is not my intention to teach
you how to program in C language. There are plenty of textbooks and courses
available to teach programming. However, I have commented my code as much as
I could. If you have questions I would be happy to answer what I can if you direct
them to [email protected]. You can also visit my website
www.bpmspublish.com for information about my books.
What’s the Problem?
Every computer program starts with a problem, something you need to
solve. You can start by creating a problem statement. What problem will you solve
with your robot? Will it need to walk or talk? Does your robot need to travel under
water or simply avoid running into walls?
If this is your first robot project you may want to start by simply having your
robot avoid obstacles. Then your problem becomes how to get your robot to do
that. Will it be able to see the obstacle with a camera and relay this image back to
you remotely? Then you can send a signal for the robot to avoid hitting something.
Can you use an ultrasonic sensor to detect the distance to an obstacle, and then by
programming code send a signal to the motors to turn avoiding hitting it? Are there
other sensors, such as infrared sensors to avoid obstacles?
Whenever you have a problem then there must be questions as to how you
will solve the problem. The method you choose can have different results and may
depend on what you want to achieve. If you wanted to remotely control the robot,
but cannot physically see where it is you probably would need the camera method.
If you just wanted the robot to send a signal to you indicating it has detected an
obstacle then you could use an infrared sensor. For each obstacle it detects it could
send a signal, then you can take action to tell the robot what to do in order to avoid
the obstacle. Sometimes you may find for whatever method you choose you have
created a new problem to solve.
The robot I am going to show you how to build is one that will handle three
problems:
Example 1:
Example 2:
I have given this robot three problems to solve so we can break that down
into three different algorithms.
Main loop
Comment: The reason for two sensors to check the front is to check for something
high or low, like a coffee table or a chair.
Else
Comment: If the right sensor does not detect anything the program
should fall through to the next if statement.
If Left IR sensor is Low than
Comment: While loop will continue until the condition is not met.
Comment: Now that we have made a right turn we need to check to see if
there is anything blocking the robot.
Comment: We now have to check to see if there is anything on the left side.
Call Half left turn function Comment: This will give the robot a small
turn to send it off into the 7 o’clock direction.
Of course, once this falls through it will go back to the main loop to detect if
anything is in the way of the last direction. It may try to do a right turn which it
will find an obstacle and then after doing two lefts will send it off in a reverse
direction from which it started.
The program is not perfect and I am sure you might come up with something
better, but it is a good example. I am not going to include the algorithm for the turn
functions as they will be shown with the code later.
Main loop
If motion than
Main loop
Wait 3 seconds
The last two algorithms are fairly simple and you are probably saying why
bother, but the process is called documentation. Documentation is very important
when working with computers or building robots. There are many times when I
needed to go back to my small notebook and refer to something. For example,
when I wanted to use the seven colored LEDs again I needed to refer to the pins to
see what pin was needed to sequence the LED through all seven colors.
Be sure you do not forget to document your progress. Even though it might
take you a little more time it will pay off in the long run.
We have not finished with our documentation yet. If you have read my first
book Student’s Guide to Building Robots, you learned that approximately 90% of
all errors found in programming are syntax errors. These errors occur because you
did not follow the syntax of the programming language, or in most cases you
misspelled or left out something. Then approximately 6% of errors are logic errors.
This is where your program code will compile without errors, but does not do what
it is supposed to. Your program might accidently go into an infinite loop because
you forgot to add your condition to break out of the loop.
Finally approximately 4% of errors are called run-time. Everything compiles
without errors, the program runs, but may suddenly lock up or do something
unexpected. This typically is caused by your code trying to access a portion of
memory another part of the program has obtained.
For this guide we will look at solving the 6% caused by logic errors and to
help with that you will need to have a flowchart. Let’s flowchart just the Main loop
of the Detect and Avoid Obstacles algorithm so you can get the idea.
Flowchart example:
Start Main
Loop
Check Ultrasonic
Sensor
Check IR Sensor
False True
Ultrasonic
Call the Find sensor greater
Call the Forward
Open Function than 12 AND Function
IR sensor
high
Restart Main
Loop
Components
Now it is time to identify the components you will need to build your robot.
If you want your robot to move around you’re going to need motors. Sensors are
needed to detect obstacles. In this robot we also use a motion detector. If you have
built several robots you may want to repurpose some of the components you
already have. I have taken many of my components for this robot from my Rosie
robot I mentioned in my Student’s Guide to Building Robots book. Those of you
who have seen pictures of Rosie, may ask why I did it. My answer would be I just
wanted to build a robot similar to R2D2, but needed the parts. Robot builders often
scavenge parts from their past robots.
I have compiled a listing of what I have used in the R3D4 robot built for this
guide.
You can see now why I scavenged parts from Rosie the robot. The number of
parts needed does add up.
Platforms
If this is your first robot I would recommend purchasing a robot platform.
They generally come with the platform, motors, in some cases wheels, and a
battery pack. My first purchased platform was the Magician Robot Platform,
shown below.
This platform can be purchased for around $30. The nice thing about it is it
already has some holes in the Acrylic so you can mount your sensors and micro-
controllers. If you read my first book you will know I used this platform for my
Rover robot. I used foam board to make a mount for my ultrasonic sensor. The
downside is it comes with a ball caster, which as you can see lowers the front. I
replaced that with a 1 ½ inch wheel caster. The battery pack is sufficient to power
the motors with AA batteries, but you will need a 9 volt battery holder, battery, and
power plug to connect to your micro-controller. Another slight disadvantage is, if
you leave the battery pack on the lower level, you need to remove the upper level
to replace the batteries. This is a great starter platform for building your robot.
If you want four motors for a little tougher robot you can purchase the Dagu
four motor robot platform or the DFRobot four motor robot platform, shown
below.
The Dagu platform is currently selling for around $27, while the DFRobot platform
sells for around $40. The advantage of the Dagu platform is it is lighter in weight
because of the acrylic pieces. The DFRobot platform, however, is made of metal.
Unfortunately, the metal and screws to hold the platform together adds weight. I
have used the DFRobot platform and mounted a battery pack with four C-cell
batteries on the small extension in the front. The robot moved around well as long
as I did not add a body and only used a minimum of components. When I first
purchased this platform I mounted a webcam to the front with a pan and tilt
mechanism that used two servo motors. It did work very well.
Some students like the idea of building a robot using a platform that has
tank-like tracks. The Rover 5 robot platform can meet that requirement.
The Rover 5 platform can be purchased for around $40. It comes assembled
with motors tracks, and battery pack. You can adjust the outriggers to raise or
lower the tracks. The manufacturer recommends you use 6 – AA rechargeable
batteries to provide the proper voltage. You can mount a piece of foam board or
light-weight balsa on top. I made mine large enough to go over the tracks and just
go a little beyond the front and back. By doing this it keeps obstacles from hitting
the tracks. It also gives enough room to mount all your components.
Below is a picture of my Rover 5 robot.
If you are handy with tools, particularly a jig saw you can cut out a plywood
platform to attach your motors and components. You will see my homemade
platform for the R3D4 robot later.
Motors
The cheapest motors to buy are the yellow, orange, blue, and black robot
motors. The color is not important. What is important is the gear ratio. The yellow
motors I used in my R3D4 robot have a 1:24 gear ratio. Understanding torque and
its relationship to the weight of a robot is still a mystery to me. But from what I do
understand is the higher the second number the greater the torque. This should
mean that my black gear motors in my Andy robot with a 1:56 ratio has more
torque than the 1:24 gear motor. I do know that while Andy is a slower robot it
does seem to have more power. Shown below is a picture of a yellow motor.
You will need to solder your wires to almost all motors, unless they come
wired. You can see one of the solder leads, which is the hole on the bottom right. It
does not matter what color wires you use as the motor polarity is reversed
depending on when the motor is going forward or backward. I do use different
colored wires mainly to know which wires go to a specific motor.
You can see holes next to the screws. These can be used to secure your
motor to the platform. Most of the platform kits have mounts to secure the motors
to the platforms. Some have screws supplied. This type of motor can run around $8
per motor and some come with wheels. The voltage needed for these motors range
between 4.5 – 9 volts.
If you need a heavier motor for a larger robot you could use a 12 volt motor
like the one shown below. But for small robots the one I described above works
fine.
The problem I have found with this type of motor is finding wheels to fit it.
Motor Controllers:
You will need a controller to direct the motors into the different directions.
The controller uses a circuit called an H-bridge. I am not going into the details of
how and H-bridge works in this guide. If you are interested in them there are
tutorials and guides online. Below I will explain some of the ones I have used.
The first one is the L298N H-bridge motor controller, which costs around
$17. It is a rather simple motor controller with two motor connections. There is the
connection for the power to the motors, a ground connection, and a + 5 volt
connection that can be used to power the controller, from the micro-controller.
Then you have an input connector for the speed and motor direction for each
motor.
In the picture below you connect the motor wires into the green connectors,
one motor for each side typically, however, if you have four motors you can wire
the left motors together and the right motors together. The blue connectors are for
the power to the motors, ground (GND), and + 5 volts. You can just see on the
right side E1 and M2 which are the pins coming from the micro-controller to
provide data for the speed and direction.
I find this motor controller to be very reliable. It is the one used in my R3D4
robot.
The L293D motor controller shield is a nice controller that will not only
control your motors, but also servos. It can be obtained for around $6, but a better
quality one might cost a little more. It mounts on top of an Arduino micro-
controller which is a space saver.
Batteries
Batteries provide power for your motors and your micro-controllers.
Unfortunately, batteries also add weight, so you want the maximum amount of
power with the least about of weight. I have used several different types of
batteries which I have listed below.
AA – Use once or rechargeable. The one use AA batteries have a 1.5 voltage, and
have capacity of 400 - 900 mAh. The NiMH rechargeable ones have a 1.2 voltage
and can range up to 2300 mAh capacity.
AAA – I have not used any rechargeable AAA batteries, but the use once batteries
have a 1.5 voltage and 750 mAh capacity.
C-Cell – nominal voltage is 1.5 volts, and alkaline batteries can hold up to 8,000
mAh while the rechargeable NiMH batteries can hold up to 6,000 mAh capacity.
9 volt – A 9 volt rechargeable has between 200 – 325 mAh capacities. Alkaline
batteries can have a much as 600 mAh capacity.
Of course, it all depends on how you use them and the drain on the capacity.
Devices that draw a lot of amps will drain a battery quicker. One way to obtain
more power would be to wire batteries in parallel, as shown below. You still have
1.5 volts but twice the power.
For more voltage you would wire your batteries in a series configuration,
shown below.
Each battery here has 1.5 volts, when wired in series your output would be 3
volts. For my R3D4 robot there are 8 batteries wired in series for a total of 12
volts, if I use AA alkaline batteries and 9.6 volts if I use NiMH rechargeable
batteries.
My MOE robot uses 8 AAA batteries since MOE is a small robot it needed
the smaller batteries. Andy robot uses 4 C-cell batteries to power the motors, so
your choice of batteries depends on how they will be used.
Micro-controllers
The Arduino micro-controllers have microprocessors and while they are
limited in their memory and instructions they can process, you can program them
to do amazing things. My favorite is the Arduino Uno. It uses the Atmel ATmega
328P 8 bit processor.
Operating Voltage 5V
EEPROM 1 KB (ATmega328P)
LED_BUILTIN 13
Length 68.6 mm
Width 53.4 mm
Weight 25 g
There are times when you need a smaller micro-controller. I have used the
Arduino Nano and constructed some micro-controllers using the ATtiny 13, 44,
and 85 microprocessors. Their circuit boards can be about 1 inch by 1 inch in size.
Below is the Arduino Nano micro-controller. It is approximately 1 inch wide by 2
inches long.
Arduino Nano
The Nano also uses an ATmega 328 processor but it is a surface mounted
processor. Having a Uno with a removable processor can be helpful if you want to
program several processors and swap them, or if you believe your processor is bad
then it can be replaced. However, you must be careful as they can be damaged by
static electricity. You would need to use a grounded static wrist strap.
I do want to mention that the Arduino Mega 2560 is what the Google ADK
micro-controller is patterned after. It is a versatile board when you need a lot of
data outputs and inputs.
The Arduino micro-controller boards are made in Italy. While there may be
some knockoff copies they may not be the same quality. You can check online to
see what to look for in copies vs. the originals. You can also obtain datasheets on
all the microprocessors online.
My R3D4 robot uses all three of the micro-controllers listed above. In the
programming chapter I will explain the pins used with the sensors and motor pins
for input and output.
Sensors
There are many different types of sensors I mentioned in my previous eBook
which can be used for your robot. My R3D4 robot uses an ultrasonic sensor for
detecting distance, three infrared sensors (IR) for detecting obstacles, a PIR motion
detector sensor for detecting movement, and two 7 color LEDs for a light show. IR
sensors are used frequently in small robots to detect walls and low objects. I like to
have a little higher front sensor like the ultrasonic sensor for detecting greater
distances like open areas.
You can see on the HC-SR04 ultrasonic sensor there are four pins. Left to
right, is the VCC + 5 volt pin, the Trigger pin, Echo pin, and the ground (GND)
pin. I will identify which pins you can use for connections to the micro-controller
in the Programming the Robot Chapter.
The IR sensor shown below has three pins VCC +5 volts, GND, and the
output signal pin. The potentiometer allows you to adjust the distance from an
object from 1.6 inches to approximately 12 inches.
Next there is the PIR motion detector.
This sensor also has three pins, VCC + 5 volts, GND, and output signal pin. As
with the IR sensor the potentiometer allows you to adjust the distance to an object
giving the motion.
Finally while they are not sensors there is the 7 color LEDs used in R3D4
robot. Show in the picture below. The pins are identified in the Programming the
Robot Chapter.
Assembly
Now it is time to put everything together. You can start by soldering your
wires to your motors. Make sure you allow yourself enough wire to reach where
your micro-controllers will be mounted.
You can see the wires I have soldered to my motors. Mount your motors to
your platform, and then put your wheels on. Also you can see the IR sensors
mounted and their wires. I used female to male jumper wires here. Whenever I can
use jumper wires it saves on having to solder. Attach your caster on to the front
and if you need to on the back of your platform. The motors and casters were
mounted using ¾ inch 4-40 screws and nuts.
On the upper part of the platform you can mount your battery pack for the
motors and the battery holders for your nine volt batteries. I used standoffs to
create a second platform for mounting my micro-controllers and the motor
controller. I added rubber tubing to the front and back to serve as bumpers. The
front bumper was used to protect the IR sensors while I tested the robot navigation.
I placed Velcro on the bottom of my micro-controllers and the motor
controller which helps to conserve weight. Next you will want to place the MP3
Player shield on top of the Arduino UNO micro-controller. Mount the small bread
board on the front just before the second level platform.
Now you will start wiring things up to test the motors, the ultrasonic sensor,
and the IR sensors. The picture above is a picture of the motor controller.
The motor wires attach to each side of the motor controller into the green
terminals. Two female to male jumpers attach to each side of the front for your E1
and M1 on the left side. On the right side you attach your wires for E2 and M2. E1
and E2 male pins need to connect to the PWM pins 7 and 8 on your Mega 2560
micro-controller. The M1 and M2 male pins connect to pins 2 and 3 of your micro-
controller. You can check to make sure you have the right pins based on what you
have listed in your program.
The front green terminals are for your GND wire which needs to be
connected to the negative wire of your motor power battery pack and you also have
to have a GND wire go to your micro-controller ground pin or in my case I have
placed it on the bread board. A set of ground pins on the bread board are connected
with a wire to the micro-controller ground pin. While your micro-controller will
generally have at least two ground connectors, you only will have one +5 volt
connector. You can use a small bread board to give yourself extra connectors for
+5 volts and ground. I use one side of the bread board for GND and the other for
+5 volt connections. Then you have the middle in case you need any resistors,
transistors, or LEDs to show something is on.
Next to the GND terminal is the VS terminal that is used for your positive
motor battery pack wire. There is a VD connection to the left of GND which is for
+5 volts power to the controller if you need it. As long as your motor voltage is
around 12 volts you can leave that wire off and the motor battery pack will supply
voltage to the controller board. Bring your IR sensor wires up through the hole in
the center of the platform to connect the GNDs to the bread board along with the
+5 volt wires.
Your output wires from the sensors will attach to pins 32, 34, and 36 on your Mega
2560 micro-controller. I used a spare ultrasonic sensor to test the motors before
putting on the body of the robot.
Your VCC wire goes to the +5 volt connection on the bread board and the
GND wire goes to the GND connection on the bread board. The Echo wire from
the ultrasonic sensor must go to a PWM connector on the micro-controller and on
mine I used pin 6 and 5 was used for the Trig wire.
Once you have your wires connected for your motor, ultrasonic sensor, and
the IR sensors you are ready to load your navigation program using a USB cable
connected to your computer. Test your navigation program as I have suggested in
the Programming the Robot in the next chapter.
When the base of your robot working with the motors you can design and
build your robot body. You might be able to find or build a round body like
R2D2’s body and some people have made robots similar.
I have also seen those types of droid robots with an octagon shape. R3D4 has this
shape. I first measured the sides and the front and back. I figured I needed 7 inches
in width and length.
7 inches width
and length
This meant each section turned out to be 3 1/8 inch wide and 8 inches tall. The
shape above is of the top. Everything was cut out of ¼ inch foam board. I taped the
sides together and once the shape was formed I applied Elmer’s glue to the seams.
On the top I glued the plastic bowl.
Plumbing pipe and fittings make up the arms, shoulders and elbows. Clear
tubing is used to reflect the colors from the 7 color LEDs. You can see the PIR
motion detector just in front of the bowl and below that you can see the ultrasonic
sensor. As I mentioned before the wiring from the PIR and the ultrasonic sensors is
standard telephone wire.
Inside the robot body I have mounted the Arduino Nano micro-controller to
control the 7 color LEDs. Once again the micro-controller was mounted with
Velcro.
Mount the body to your base platform. I am going to use tie-wraps to secure
it and if I need to I can cut them to release the body to work on it.
The light show is really interesting. I cannot play the sounds and voice clips for
you, but they are sounds that R2D2 would make and a couple phrases from Yoda.
“Help you I can.”
“Do or don’t do, there is no try.”
Programming the Robot
Arduino IDE
There are two ways to create and load your programs on to your micro-
controllers. You could use the online IDE found at the Arduino website,
www.arduino.cc
Or you could download the IDE program and install it on your Windows or Mac
computer. I prefer to download and install the program.
In the following pictures I will show you what the program looks like.
When you first open the IDE it will give you a template for creating your
program by displaying the setup function area and the loop function area. You can
hit Enter several times to open an area at the top to put your comments.
Start your group of comments with /* and end them with */ . You can if you
like us an * at the beginning of each line. After the comments you will want to add
the include statements, starting with # for the libraries you want to attach to your
program when it is saved. Include statements do not need a semicolon after them.
Then after the include statements you will want to add your variable declarations.
Liberal comments help to document your program and will help you if you ever
need to go back and work on it.
It is generally a good idea to place all your functions before your setup
function. After you have added your functions that will be called within your
program you can put items in your setup function, like pin modes and serial baud
rate for your serial monitor. Finally in the main loop function you will list your
code, which will execute each time the loop repeats.
The edit menu is pretty much self-explanatory if you have used other
computer programs.
The picture below shows the Sketch menu with the Include Library
submenu. If you have installed a library on your computer for a set of functions it
will be listed here. Clicking on it will automatically add it to your program.
On the Sketch menu you can compile your code to see if there are any
errors. If your code is error free you then can upload it to your micro-controller.
There are also icons on the toolbar just below the set of menus. If you don’t have a
USB connector installed on your micro-controller, as is in the case of building your
own micro-controller you can Upload Using a Programmer.
The next menu is the Tools menu and there are several important submenus
found here.
Here you will find the Serial Monitor, which is very helpful for watching your
program execute. By placing Serial.print statements in your code you can tell if
you have entered a function or see what the value is of a variable.
The Board submenu will allow you to select what micro-controller you will
be using to load your program code. The micro-controller boards used in this
project are the Arduino Uno, Nano, and the Mega 2560. You can see in the picture
below that I have selected the Mega 2560. You must select the board you are
uploading to, or you will receive and error.
The Processor submenu will show you what type of microprocessor is
mounted on the board. In some cases, like for the Arduino Uno you may not see a
Processor submenu provided. The Nano offers several choices, shown below.
You have three choices for processor, however, if you experience errors you will
want to select the ATmega328P (Old Bootloader).
Next you will want to make sure the computer has recognized the port your
micro-controller board is on. If you see it shaded out like the picture above then
either you have not connected the USB cable or the board’s USB port may be bad.
I had a Mega 2560 micro-controller once that did have a bad port and there wasn’t
anything I could do with it. When you purchase a micro-controller, one of the first
things you should do is hook it up with your USB cable to see it the computer
identifies it has a good working USB connector or circuitry. If the Port is showing
and you click on it, it will tell you what com port it is using.
Now, you are probably asking why I did not use a programmer to upload the
code to my bad Mega 2560. I did try and it looked like it loaded properly, but there
may have been something else wrong with the board along with the USB port not
working. Many times in working with computers or robots you have to try and
track down the problem, by trying different strategies to find the answer. Below is
a picture of an AVR ISP Programmer, a converter, and the pins. It is a very handy
tool to have in your tool kit.
The picture above shows the programmer on the left and a converter that
will fit on the 6 pin connector. The pin outs are shown beneath the converter.
The final menu is the Help Menu. It too is pretty much self-explanatory, but
as you can see there is a lot of help.
The best way to test your navigation program is to use a tissue box or
something to raise your robot off its wheels. First test the program using the Serial
monitor without powering the motors. This way you can see what functions are
being called and entered along with variable values from the sensors. Once you are
satisfied the program seems to be working you can place it on the floor and power
the motors. This saves from having your robot bumping into walls.
When you have finished typing in your code you can use the toolbar shown
below to verify and upload your program.
The icons are Verify (to compile and check your code, upload (loads the program
to the board, New file, Open, and Save.
R3D4 Robot Navigation
The program to read the ultrasonic sensor and the IR sensors, and to
navigate the robot is below.
/*
* R3D4 Robot Navigation Program using L298N Motor Controller
*
* Modified by E.T.Bryant
* 4/6/19
*
* Uses ultrasonic sensor and IR sensors to avoid objects
*.
*/
// Right motors
// Left motors
// High is forward
// Low is reverse
// Varibles
int speed;
int inches; // variable for distance measured in inches
int E1 = 7; // pin value for motor 1 speed control
int M1 = 2; // pin value for motor 1
int E2 = 8; // pin value for motor 2 speed control
int M2 = 3; // pin value for motor 2
int value1 = 0;
int value2 = 0;
int value3 = 0;
return distance;
}
void forwardMotion()
{
double distanceF = checkDistance();
int distance = distanceF;
value2 = digitalRead(obstaclePin2);
Serial.print(distance);
Serial.println();
Serial.print("Inside forward function");
Serial.println();
Serial.print("Moving forward");
Serial.println();
value3 = digitalRead(obstaclePin3);
if (value3 == LOW)
{
Serial.print("Right sensor detected");
Serial.println();
halfLtTurn(); // if sensor detects something then turn a little to the left
void findOpen() // This function will cause robot to find an open space
{
allStop();
reverse();
rightTurn();
// Check front sensors
double distanceF = checkDistance();
int distance = distanceF;
Serial.print(distance);
Serial.println();
value1 = digitalRead(obstaclePin1);
value2 = digitalRead(obstaclePin2);
value3 = digitalRead(obstaclePin3);
allStop();
reverse();
leftTurn();
leftTurn();
void reverse()
{
speed = 125;
delay(750);
}
void allStop()
{
void rightTurn()
{
delay (1800);
void halfRtTurn()
{
delay (900);
}
void leftTurn()
{
delay (1800);
void halfLtTurn()
{
delay (900);
void setup()
{
Serial.begin(9600);
// IR Sensor PinMode
pinMode(obstaclePin1, INPUT);
pinMode(obstaclePin2, INPUT);
pinMode(obstaclePin3, INPUT);
}
void loop()
{
forwardMotion();
}
if ((distance < 20) || (value2 == LOW))
{
Serial.print(distance);
Serial.println();
Serial.print("Inside second if condition");
Serial.println();
findOpen();
// include statements
#include <stdlib.h>
#include <stdio.h>
#include <SPI.h>
#include <SdFat.h>
//#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#include <SoftwareSerial.h>
SdFat sd;
SFEMP3Shield MP3player;
void playtrack()
{
int trackNum = 0;
trackNum = rand()%9 + 1;
MP3player.playTrack(trackNum);
delay(5000);
MP3player.stopTrack(); //Close out this track
void setup()
{
pinMode(inputPin, INPUT);
void loop()
{
playtrack();
if (pirState == LOW)
{
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
} else
{
if (pirState == HIGH)
{
}
}
}// The main loop ends here
Program ends here.
LEDs
The program below is for testing the Radio Shack 7 color LED part number
276-0016.
/*
* Code from Arduino website forum by guest SpikedGoat. June 30 2011.
*
*
*/
int neg = 13; // +LED connected to digital pin 13
int pos = 12; // -LED connected to digital pin 12
int crtl = 11; // control coonected to digital pin 11
// The setup() method runs once, when the sketch starts
void setup()
{
// initialize the digital pin as an output:
pinMode(neg, OUTPUT);
pinMode(pos, OUTPUT);
pinMode(crtl, OUTPUT);
digitalWrite(pos, HIGH); // set the LED on
digitalWrite(neg, LOW); // set the LED off
digitalWrite(crtl,LOW); //ser the crtl off
delay(15000); // wait for 15 second
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(crtl, HIGH);
delay(250);
digitalWrite(crtl, LOW);
delay(250);
} // End of loop
Program ends here
You can see that the LED has three leads or pins. A ground pin, power, and
control. Center straight pin needs a 330 ohm resistor between it and +5 volts.
Right-angled pin goes to ground (GND).The 45-degree-angled pin goes to ground
through your digital pin on the micro-controller to trigger a color change.
However, if the pin is grounded it will sequence through all seven colors.
Code disclaimer:
My apologies if you find any errors in the code included in this chapter due
to formatting the book for publishing. The code listed is working in my robots, but
may have been changed slightly by the formatting. You are free to modify the
code, and I have listed, where I could, the original creators. I am sure experienced
programmers can even make the code better. I always recommend if you are
having a problem you can do a Google search and you will most likely find that
someone else has experienced the same problem and may have a solution.
Conclusion
You can see anyone with a little knowledge and some skill can make a robot.
The companies I have listed in the Reference chapter below offer kits and some
assembled robots for you to experiment and play with. While the robot I have
included in this book might be a little complex for some students I would suggest
you start small with a simple robotic platform.
I recently had read an article that stated by 2025 46% of all jobs will be
automated. It sounds scary, but many companies are looking for ways to reduce the
labor force. I know there is a lot of push to educate students in the Cyber Security
field, but like programming which was a hot field to be in in the 60s and 70s it too
will become over populated. I do say to my students it is not a bad field to go in as
you will need to have knowledge of databases, networking, and programming.
However, if it becomes difficult to find a job in Cyber Security you can find a job
in database management, networking, or programming. On the other hand while
there does not seem to be as much push to have students trained in Robotics there
is a lot of talk about Science Technology Engineering and Math (STEM).
We have gone through the industrial revolution where heavy equipment and
machinery played a key part in industry. Unfortunately, many of the jobs required
heavy manual labor. In many cases manufacturing work was repetitive and in some
cases dangerous.
When companies decided to outsource and offshore jobs many companies could
not be competitive and those jobs were gone. Many manufacturing jobs like those
found in the steel, and automobile industry disappeared. However, with the dawn
of the digital revolution in the 60s and 70s the emphasis was on information, and
as a result of having all that information available there was a need for people to be
trained in computers and technical fields.
We have just entered a new revolution in the late 90s and 2000s called the
Internet of Things, where just about everything can be controller through the
Internet. Most devices can be remotely controlled by your cell phones and tablets,
using Apps. You can see that this has also launched the beginning of the Robotic
Revolution. I am a firm believer of not putting all your eggs in one basket. While
with Cyber Security which will be in demand to counter all the threats that will
come from the Internet, we also need to have people trained in the field of robotics
as well.
Many years ago, well not so many, it was the 1950s when people thought
that in the future we would have robots and our homes would be automated. For a
while that was not a reality, because the science and technology was not ready.
Although, today “Tomorrow is here,” as they used to say.
Comments
I hope you have enjoyed and found this book an inspiration. I have worked
with electronics and computers for 39 years and enjoy teaching and relating many
of my experiences to my students about the computer industry. I want to give back
some of what I have learned to others so you might enjoy working with the
products I have mentioned in this book.
If you found this book interesting please leave a review. I appreciate all
comments as it will help me to provide the best experience for you.
Thank you!
References
Many of the photos found here are of my own components and equipment.
Some of the photos are of items you can find at any online store. I have listed
below some places I have purchase some of my components, but the listing is by
no means a complete one. Just be careful in your online purchases to make sure
they are using secure http connections if you are going to pay by credit card.
Listed below are references for companies and products I have mentioned in
my book. I apologize if I have left anyone out.
Robotshop – I have made some purchases through Robotshop which also is a good
source for robot parts and kits. https://www.robotshop.com/
Jameco – A source for electronic components and kits. I also have made purchases
from them. https://www.jameco.com/
https://www.sparkfun.com/
DFRobot – The 4 motor platform was sold by DFrobot. https://www.dfrobot.com/
Some websites are not secure, so be careful in supplying information and credit
card info on unsecure sites.