Your first real robot the easy way, learn all the basics while at it :)


Step 49:

Picture of


Enter this code into your editor, and press F5 while the robot is connected:


readadc 0, b0


goto main

Now take your hand in front of the robot´s head and notice how the variable b0 changes value. You can use the knowledge gained to decide what should happen when (how close things should get before..)

You may notice how things start to go "wrong" if stuff is too close to the "eyes"; The Sharp is made to work with stuff that is from 10-80 CM away. Things that are closer appears to be further way, which can be quite a challenge to program.

You can get many other distance sensors that do not have this problem. However the Sharp is the cheapest, and easiset to program, so that's why I made such a "bad" choise for you, sorry ;) Look around and see what everyone are using, before you decide for an upgrade.

Now I advise you to put your robot up on a matchbox or similar, as the wheels will start turning.

Enter this code into your editor, and press F5 while the robot is connected:

high 4

low 5

One of the wheels should turn in one direction. Does your wheels turn forward? If so, this is the instruction for that wheel to turn forward.

If the wheel is turning backwards, you can try this:

low 4

high 5

To turn the other wheel, you need to enter

high 6

low 7

(or the other way around for opposite direction.)

What happens here is that by using only the options available to the microcontroller; power on or off (High / Low) on it's pins, it is commanding the motor controller to set motor A or B in forward or reverse mode.


low 4

low 5

low 6

low 7


stops all motors

The servo you have already tried.

All the way to one side is:


servo 0, 75 wait 2


the other side is: 


servo 0, 225 wait 2

- and center:


servo 0, 150 wait 2

Here is a small program that will (should, if all is well, and you insert the right parameters for high/low to suit your wiring to the motors) make the robot drive around, stop in front of things, look to each side to decide which is the best, turn that way, and drive towards new adventures. In the code I have made socalled remarks: Stuff written to explain you what is going on.

You can write such comments or remarks yourself in the code, it is a good idea to keep track.

They are writen with a ' sign. However, copying this text from here might alter that to something else, and you will have to fix that manually, sorry. Your programming editor has color codes, that will help showing you what it recognizes as comments and what as code.


Symbol dangerlevel = 70 ' how far away should thing be, before we react?
symbol turn = 300 ' this sets how much should be turned
symbol servo_turn = 700 ' This sets for how long time we should wait for the servo to turn (depending on it´s speed) before we measure distance

main: ' the main loop
readadc 1, b1 ' read how much distance ahead
if b1 < dangerlevel then
gosub nodanger ' if nothing ahead, drive forward
gosub whichway ' if obstacle ahead then decide which way is better
end if
goto main ' this ends the loop, the rest are only sub-routines

nodanger:' this should be your combination to make the robot drive forward, these you most likely need to adjust to fit the way you have wired your robots motors
high 5 : high 6 : low 4 : low 7

gosub totalhalt ' first stop!

'Look one way:
gosub lturn ' look to one side
pause servo_turn ' wait for the servo to be finished turning
readadc 1, b1
gosub totalhalt

'Look the other way:
gosub rturn ' look to another side
pause servo_turn ' wait for the servo to be finished turning
readadc 1, b2
gosub totalhalt

' Decide which is the better way:
if b1
gosub body_lturn
gosub body_rturn
end if

high 6 : low 5 : low 7 : high 4 ' this should be your combination that turns the robot one way
pause turn : gosub totalhalt

high 5 : low 6 : low 4 : high 7 ' this should be your combination that turns the robot the other way
pause turn : gosub totalhalt

servo 0, 100 ' look to one side

servo 0, 200 ' look to the other side

low 4 : low 5 : low 6 : low 7 ' low on all 4 halts the robot!
Servo 0,150 ' face forward
wait 1 ' freeze all for one second



With some clever programming and tweaking, you can make the robot drive, turn it´s head, make decisions, make small adjustments, turn towards “interesting holes” such as doorways, all working at the same time, while driving. It looks pretty cool if you make the robot spin while the head is turning ;)



Fun time

You could also attach a lamp or LED to pin 2 & ground, and write (remember LED´s need to turn the right way around)

High 2


to turn on the lamp, and 


Low 2

to turn it off ;)

- How about a Laser-pen, mounted on an extra servo? Then you could make the robot turn the laser around, and turn it on and off, pointing out places.. you can do anything now :)

Pressing "Help" in the programming editors brings out all sorts of interesting tutorials and info!


Perhaps try this:

Pull out the servo, and take up the yellow chip. Insert the Darlington that you took out earlier. Hook up the speaker to the 2 pins above where the servo was, tat is output 1. And throw in the LED, or whatever it was that you found on output 2. Then program it something like this:


sound 1, (100, 30)

high 2

wait 1

low 2

sound 1, (105, 60)


That should make a sound and turn something on, make a new sound and turn it off again.


Or the more interesting, make sure the Sharp is still in, hook a speaker up to pin 1, and then program this:



readadc 0, b0

sound 1, (b0, 2)

goto noise


Here is the program I used to make the video in the intro :)

Ce ya!
(I hang out on letsmakerobots.com most of the time)