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.

 

Light Navigation Code

  • Find a flashlight, the brighter the better.
  • In BlocklyProp, find your Light Sensor Test Graphical project from the last page.
  • Save your project as with a new name, such as Navigate by Light. 
  • Delete the blocks that print to the Terminal at the beginning and end of the program, and add blocks for the ActivityBot as shown below.
  • Be sure to set the Robot initialize block's dropdown to your particular robot: ActivityBot or ActivityBot 360° before you save it.

  • Set the power switch to 1, then click Load and Run (save to EEPROM).
  • 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 Robot blocks for the ActivityBot.  After initializing the ActivityBot, the program consists of a repeat forever 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 variables lightLeft, lightRight, and nDiff are setup for taking and using the sensor measurements.  Then, the variables speedLeft and speedRight are set up for use with the Robot drive speed block later on. 

The first two parts inside of the repeat forever loop are the same as our previous test program. 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 project.  It calculates nDiff, which will have a value in the range of -100 and 100.  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 in the amount of light that the two phototransistors are sensing.

The variables speedLeft and speedRight are initialized to 64. Keep this in mind when looking at the if...else 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 64 minus nDiff × 4."  The new value for speedLeft would be something less than 64, while speedRight remains 64.  These variables are then used in the Robot drive speed block after the if...else block.   For example, if nDiff = 32, this statement is the same as speedLeft = 64 - (32 × 4), which equals -64.  The result is Robot drive speed (left = -64, right = 64) which makes the ActivityBot rotate to the left toward the light.

However, if nDiff is NOT positive, the code drops to the else part of the if...else block, making speedRight equal to (nDiff × 4).  This translates to "make speedRight equal to 64 plus nDiff × 4."  This is still a number smaller than 64; remember that nDiff is negative here.  For example, if nDiff = -16, this statement is the same as speedRight equals 64 + (-16 × 4) so speedRight ends up equal to zero, while speedLeft is still 64.  This gives the Robot drive speed (left = 64, right = 0), causing the ActivityBot to pivot right towards the brighter light.


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 project.

  • Click Save As, and name the copy of the project 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, click Load and Run (save to 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. 

Your Turn

What other behaviors can you give your ActivityBot with light sensors?

  • Use additional conditions in the if...else block to add behaviors such as "stops when there is a lot of light."
  • Use additional variables, or set up another processor, to make your robot go to sleep after 30 seconds and wake back up when the light it detects changes.