In honor of the upcoming National Robotics Week (April 5-13, 2014), I’ve created “CockroachBot” based on my Snap Circuits programmable robot I designed for last year’s robotics week. CockroachBot will try to run away when it detects a particular level of light falling on its light dependent resistor. I designed CockroachBot to be easy to build completely out of Snap Circuits parts and easy to program to inspire folks from seven to centenarian to get interested in robotics.

Parts Needed:

1 10 X 10 Plastic Base Grid
1 Rover
1 9 volt Battery Block B5
1 Motor Control IC U8
1 8 Pin Socket U14 With PICAXE 08m Installed
1 100K Ohm Rsistor R5
2 1K Ohm Resistors R2
1 Photo Resistor RP
1 Slide Switch S1
1 USB Programming Cable
2 Single Snaps
15 Two Snap Conductors
1 Three Snap Conductor
1 Four Snap Conductor
1 Six Snap Conductor
1 Seven Snap Conductor
1 Orange Jumper Wire
1 White Jumper Wire
1 Green Jumper Wire
1 Yellow Jumper Wire
1 Purple Jumper Wire
1 Gray Jumper Wire

Build the circuit shown:

Create the following flowchart in the PICAXE Programming editor.

The simplest way to describe what this program does is it waits for the light level in the room to reach a certain intensity and once that light level is reached, the program alternates switching the rover's right and left motors on and off. This will result in the rover turning right, then left, then right, then left, ad infinitum. Here is what the BASIC code looks like:

main:
label_6: 'While b0 is less than 201
readadc 4,b0 'Get value of b0 from pin 4 (amount of light falling on RP sensor)
serout 0,N2400,(#b0,13,10) 'output value of b0 (amount of light falling on RP sensor) to programming editor's serial terminal
wait 1 'wait one second before proceeding
if b0> 200 then label_51 'if value of b0 > 200 (if brightness of light on RP sensor reaches chosen level, CockroachBot starts trying to run)
goto label_6 'light isn't bright enough so, take another reading
label_51:
label_27:
high 0 'switch left CockroachBot motor on
low 1 'switch right CockroachBot motor off
pause 1000 'right motor turns for one second
low 0 'switch left motor off
high 1 'switch right motor on
pause 1000 'right motor turns for one second
goto label_27 'repeat until CockroachBot is switched off

The "readadc 4,b0" process reads the analog-to-digital converter value on pin 4 and assigns that value to b0 the possible values are from 0 to 255 depending on the amount of light falling on the photo resistor (RP) connected to pin 4 on the PICAXE.

The "serout 0,N2400,(#b0,13,10)" input/output is not necessary for the program to run. I inserted it so I could test the light levels in the room on the Programming Editor's Serial Terminal and adjust the program accordingly. When you convert the flowchart to BASIC and program the PICAXE you can display the Serial Terminal by pressing the F8 key. In the picture you can see that I tested the light levels in a room with a dimmer switch on the light.


Source

The "wait" process tells the program to wait one second before proceeding to the next step. On the on the Programming Editor's Serial Terminal I saw the values displayed once every second.

The "if b0> 200 then label_51" decision diamond says that if the value assigned to b0 is greater than 200 then goto the next step in the program otherwise go back to the "readadc 4,b0" process. If you look at the Serial Terminal picture what do you think happened when I raised the light level using the dimmer switch and the value in b0 jumped from 122 to 235?

The "high 0" input/output tells the PICAXE to set pin 0 high. This will start the left side motor on the rover to start turning forward.

The "low 1" input/output tells the PICAXE to set pin 1 low. This will tell the right side motor on the rover to stop turning.

The "low 0" input/output tells the PICAXE to set pin 0 low. This will tell the left side motor on the rover to stop turning.

The "high 1" input/output tells the PICAXE to set pin 1 high. This will start the right side motor on the rover to start turning forward.

To run the program, make sure the Snap Circuits Slide Switch S1 is switched off. You don’t want the program to execute while the program is still downloading to the PICAXE. This may accidentally disconnect the programming cable (if the light level falling on the RP sensor causes it to return a value greater than 200 causing the CockroachBot to try to run). Next, make sure that the Snap Circuits 9 volt Battery Block B5 is switched on or the program will not download to the PICAXE. Connect the Snap Circuits USB Programming Cable to your computer.

1. Connect the yellow snap to the S-In snap on the Snap Circuits 8 Pin Socket U14 block (the PICAXE chip carrier).

2. Connect the red snap to the S-Out snap on the Snap Circuits 8 Pin Socket U14 block (the PICAXE chip carrier).

3. Connect the black snap to the GND, or negative (-) snap on the 8 Pin Socket U14 block (the PICAXE chip carrier).

Before proceeding you will want to cover the RP sensor with your finger.

In the Programming Editor, click PICAXE and then click Run. A status bar will display to show the progress of the download of the program to the PICAXE.

Once the program finished downloading, disconnect the programming cable from your computer and from CockroachBot. Place the CockroachBot on the floor a safe distance from any obstacles. Then switch the Slide Switch S1 to on. When you are ready to watch your CockroachBot try to run, slowly remove your finger from the RP sensor. When enough light falls on the sensor to set the value of b0 to 201, CockroachBot will start turning right, then left, then right, then left until you turn the Slide Switch off.