Roaming with Light Sensors

Now, let's make the ActivityBot follow light!  The following code should allow your robot to follow a flashlight beam, or navigate out of a dark room through a brightly lit doorway.

Navigate by Light

  • Find a flashlight, the brighter the better.
  • Click SimpleIDE’s Open Project button.
  • Open Navigate by Light.side from ...Documents\SimpleIDE\Learn\Examples\Robots\ActivityBot. 
  • Click the Load EEPROM & Run button.
  • Disconnect your robot from its programming cable, and set it in an open area.
  • Set the power switch to 2, then press and release the reset button.
  • Shine the flashlight on the floor in front of the ActivityBot, which should turn towards the bright spot. If your flashlight isn't very strong, you may need to point it directly at the sensors.
  • Try shutting off the lights in the room, but opening a door to a brightly lit area outside. The ActivityBot should find its way out of the room.


How it Works

This program uses the simpletools and abdrive libraries.  After initializing some variables, the program consists of an infinite loop which receives values from the phototransistors and uses them to adjust the servos' speeds to navigate toward whichever side detects brighter light.

First, the now-familiar int variables lightLeft, lightRight, and ndiff are declared for taking and using the sensor measurements.  Then, int variables speedLeft and speedRight are declared for use with the drive_speed function later on. 

The first six lines in the main function's while(1) loop are straight out the test programs; they take the light sensor measurements and store the values in lightLeft and lightRight.

The next line is the same ndiff equation used in Test Light Sensor Graphical.  It divides lightRight by lightRight + lightLeft.  The result is multiplied by 200, and then 100 is subtracted from it.  This yields a value in the range of -100 and 100, which is assigned to ndiff.  Positive ndiff values mean the left photoresistor is sensing brighter light, and negative ndiff values mean the right photoresistor is sensing brighter light.    The farther the value is from zero, the greater the difference between what the two phototransistors are sensing.

The variables speedLeft and speedRight are then initialized to 100; keep this in mind when looking at the if code block that comes next.

The first condition translates to "if ndiff is greater than or equal to zero (brighter light on the the left), make speedLeft equal to 100 minus ndiff*4."   The new value for speedLeft would be something less than 100, while speedRight remains 100,  when those variables are used in the drive_speed function call right below the if block.   For example, if ndiff = 50, this statement is the same as speedLeft = 100 - (50*4), which equals -100.  The result is drive_speed(-100, 100) which makes the ActivityBot rotate to the left toward the light.

However, if ndiff is NOT positive, the code drops to else speedRight += (ndiff * 4).  This translates to "make speedRight equal to 100 plus ndiff*4."  This still yields a number smaller than 100; remember that ndiff is negative here.  For example, if ndiff = -25, this statement is the same as speedRight = 100 + (-25*4) so speedRight ends up equal to zero, while speedLeft is still 100.  This giving us drive_speed(100, 0), causing the ActivityBot to pivot right towards the brighter light.

  Navigate by Light.c

#include "simpletools.h"
#include "abdrive.h"

int lightLeft, lightRight, ndiff;
int speedLeft, speedRight;

int main()                    
    lightLeft = rc_time(9, 1);
    lightRight = rc_time(5, 1);

    ndiff = 200 * lightRight / (lightRight + lightLeft) - 100;

    speedLeft = 100;
    speedRight = 100;
    if(ndiff >= 0) speedLeft -= (ndiff * 4);
    else speedRight += (ndiff * 4);

    drive_speed(speedLeft, speedRight);


Did You Know?

Phototaxis — This is the term that describes an organism moving its whole body in direct response to light stimulus.  Moving towards brighter light is called positive phototaxis, and moving away from brighter light would then be negative phototaxis.  While the Navigate by Light program makes the ActivityBot mimic positive phototaxis, in nature, it seems this behavior is usually found in microscopic organisms.  Moths are attracted to light, but their varied and circling flight paths around a streetlamp demonstrate that their navigation systems are far more complex.

Try This

Would you like to make your ActivityBot mimic negative phototaxis? It can be done by replacing one single variable in the Navigate by Light code.

  • Click Save Project As, and make a copy of the project with the name Navigate By Dark.
  • In the ndiff equation, change the first use of lightRight to lightLeft, as shown below.

  • With the power switch in position 1, load the code into EEPROM.
  • Put the ActivityBot on the floor, move the switch to position 2, and push the reset button. 
  • Shine your flashlight at the ActivityBot. It should turn away from the light and seek shelter under a table or in a dark corner.