Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses

(Legacy Version) Propeller C Programming with the ActivityBot

Curriculum

  • 1 Section
  • 48 Lessons
  • Lifetime
Expand all sectionsCollapse all sections
  • (Legacy Version) Propeller C Programming with the ActivityBot
    48
    • 1.1
      Mechanical Assembly
    • 1.2
      Check your Hardware
    • 1.3
      Step 1 – Prepare your Encoders
    • 1.4
      Step 2 – Prepare the Tires
    • 1.5
      Step 3 – Prepare the Chassis
    • 1.6
      Step 4 – Prepare the Servos
    • 1.7
      Step 5 – Mount the Right Servo
    • 1.8
      Step 6 – Mount the Left Servo
    • 1.9
      Step 7 – Mount the Right Encoder
    • 1.10
      Step 8 – Mount the Left Encoder
    • 1.11
      Step 9 – Mount the Battery Pack
    • 1.12
      Step 10 – Mount the Tail Wheel
    • 1.13
      Step 11 – Mount the Drive Wheels
    • 1.14
      Step 12 – Mount the Activity Board
    • 1.15
      Electrical Connections
    • 1.16
      Software and Programming
    • 1.17
      Circuit Practice
    • 1.18
      Powering & Connecting Circuits
    • 1.19
      Blinks
    • 1.20
      Beeps
    • 1.21
      Navigation Basics
    • 1.22
      Test Feedback 360° Servos
    • 1.23
      Calibrate Feedback 360° Encoders
    • 1.24
      Test the External Encoders
    • 1.25
      Calibrate External Encoders
    • 1.26
      Go Certain Distances
    • 1.27
      Set Certain Speeds
    • 1.28
      Navigate by Touch
    • 1.29
      Build the Whiskers
    • 1.30
      Test the Whiskers
    • 1.31
      Inside the Whisker Circuit (Optional)
    • 1.32
      Add Whisker Indicator Lights
    • 1.33
      Whisker-Wheel Response
    • 1.34
      Roaming with Whiskers
    • 1.35
      Navigate by Ultrasound
    • 1.36
      Build and Test the Ping))) Sensor Circuit
    • 1.37
      Roaming with Ultrasound
    • 1.38
      Follow Objects with Ultrasound
    • 1.39
      Navigate by Visible Light
    • 1.40
      Build the Light Sensor Circuits
    • 1.41
      Using the Measurements
    • 1.42
      Roaming with Light Sensors
    • 1.43
      Navigate by Infrared Flashlights
    • 1.44
      Build the IR Sensor Circuits
    • 1.45
      Test the IR Sensor Circuits
    • 1.46
      Roaming with Infrared Flashlights
    • 1.47
      Extras
    • 1.48
      Troubleshooting

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 …DocumentsSimpleIDELearnExamplesRobotsActivityBot. 
  • 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()                    
{
  while(1)
  {
    high(9);
    pause(1);
    lightLeft = rc_time(9, 1);
    
    high(5);
    pause(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. 

 


Printer-friendly version
Using the Measurements
Prev
Navigate by Infrared Flashlights
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2026 Parallax Learn • Built with GeneratePress