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.
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.
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.
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.
What other behaviors can you give your ActivityBot with light sensors?