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 Infrared Flashlights

With your IR object sensors built and tested, you are ready to make your ActivityBot roam and avoid obstacles without bumping into them.

IR Roaming Code

The IR Roaming code example makes the ActivityBot go forward until it detects an obstacle.  If it sees an obstacle on the left, it’ll turn right.  Likewise, if it sees one on the right, it’ll turn left, and if it sees obstacles on both left and right, it’ll back up.

Not all obstacles are visible. 
Many black objects will absorb infrared light instead of reflecting it.  If in doubt, use your LED indicator program from the Build and Test the IR Detectors “Try This” section.  The LEDs will show you if the object is visible to the IR object detectors.

  • Click SimpleIDE’s Open Project button.
  • Open IR Roaming from …DocumentsSimpleIDELearnExamplesActivityBot.
  • Click the Load EEPROM & Run button.
  • Let your robot loose on the floor, or in an obstacle course you have created and see how it does.

How it Works

This example is almost identical to Test IR Detectors.c from Build and Test the IR Sensor Circuits.  The code changes are:

  • A drive_setRampStep call was added to the initialization.
  • The print statement was replaced by an if…else if…else if…else statement.
  • The pause(100) was removed.
/*
  IR Roaming.c

  Use IR LEDs and IR receivers to detect obstacles while roaming.

*/

#include "simpletools.h"                        // Library includes
#include "abdrive.h"

int irLeft, irRight;                            // IR variables

int main()                                      // main function
{
  low(26);                                      // D/A0 & D/A1 to 0 V
  low(27);

  drive_setRampStep(12);                        // Max step 12 ticks/s every 20 ms

  while(1)
  {
    freqout(11, 1, 38000);                      // Check left & right objects
    irLeft = input(10);

    freqout(1, 1, 38000);
    irRight = input(2);

    if(irRight == 1 && irLeft == 1)             // No obstacles?
      drive_rampStep(128, 128);                 // ...full speed ahead
    else if(irLeft == 0 && irRight == 0)        // Left & right obstacles?
      drive_rampStep(-128, -128);               // ...full speed reverse
    else if(irRight == 0)                       // Just right obstacle?
      drive_rampStep(-128, 128);                // ...rotate left
    else if(irLeft == 0)                        // Just left obstacle?
      drive_rampStep(128, -128);                // ...rotate right
  }
}

The drive_rampStep function is designed to be used in loops.  After our call to drive_setRampStep(12), if a loop calls drive_rampStep(128, 128) it will increase speed 12 ticks/second at a time toward a final 128 ticks per second every time it’s called.  If it’s calling 50 times per second (20 ms pauses), it will take 11 repeated calls (11/50ths of a second) to get to full speed.  After the ActivityBot reaches full speed, repeated calls to drive_rampStep(128, 128) don’t change the speed any because it’s already there.  If the loop then starts repeating drive_rampStep(-128, -128) calls, it’ll take 22/50ths of a second (getting close to half a second) to ramp from full speed forward to full speed reverse.

The if…else if…else if…else statement is what uses the irLeft and irRight detection result values for navigation.  For example, if(irRight == 1 && irLeft == 1) it means that no objects are detected, so drive_rampStep(128, 128) sends the ActivityBot a 12 ticks/second step toward 128 ticks per second if it both wheels aren’t already running at that speed.  If the ActivityBot sees an obstacle on it’s right, the else if(irRight == 0) condition becomes true, so each time through the loop, it takes 12 ticks/second steps toward rotating left in place every 50th of a second.  Rotating in in place is achieved with the left wheel turning at full speed backwards at -128 ticks/second and the right turning at 128 ticks/second forward.

As the ActivityBot rotates left to avoid the right obstacle, the obstacle will disappear from the right detector’s view.  At that point, the detection states will go back to irLeft == 1 and irRight == 1, which will make the first if statement true again, and drive_rampStep(128, 128) will start ramping back to full speed forward.

A pause between while(1) loop repetitions is not needed because the drive_rampStep function delays 1/50th of a second before returning.

 


Did You Know?

drive_setRampStep — This function sets the maximum change in speed that drive_rampStep can cause every 1/50 of a second.  The abdrive library defaults to 4, which means that the largest change in speed allowed is 4 ticks/second every 50th of a second.  Although that default makes drive_ramp and drive_rampStep maneuvers nice and smooth, it’s not enough change in a small amount of time to detect and avoid obstacles at full speed.  So, drive_setRampStep(12) triples the speed change allowed every 50th of a second.  Not quite as smooth, but responsive enough to avoid the obstacles.


Try This

You can reduce the roaming speeds by changing the 128’s in the program to lower values.

  • Use the Save Project As button to save a copy of your project in …DocumentsSimpleIDEMy Projects.
  • Try changing all the 128 values in your program to 64.  Make sure to leave all the negative signs in place.
  • Run the modified program and let the ActivityBot roam at half speed.

 

Your Turn

Increasing the value used in the drive_setRampStep function call to a higher value will make the robot more responsive, but also more twitchy.  Reducing the value will make it smoother, but at some point, it won’t respond quickly enough and it will run into obstacles.

  • Re-open the original, unmodified IR Roaming example from …DocumentsSimpleIDELearnExamplesActivityBot.
  • Click the Save Project As button to save another copy and give it a new name.
  • Experiment with increasing and decreasing the drive_setRampStep parameter.
  • How low can you go before it starts running into the same obstacles it used to be able to see and avoid.
  • How large can you make it before the ActivityBot’s behavior becomes noticeably twitchy?

Printer-friendly version
Test the IR Sensor Circuits
Prev
Extras
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress