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
  • Touch Navigation for the cyber:bot

Touch Navigation for the cyber:bot

How Escaping Corners Works

The example project escaping_corners is an evolution of the original roaming_with_whiskers. If you didn’t read through How the Navigation by Touch Code Works, you might want to go back and do so now.

This project begins with the four familiar navigation function definitions for forward, left, right, and backwards.

Next comes an initialization section. There are five variable declarations. counter will be used to keep track of alternate whisker presses. w_l and w_r are shortened versions of the roaming code’s whisker_left and whisker_right; remember that long variable names take more memory! With “o” for “old,” the variables o_w_l and o_w_r will store the previous whisker states in order to compare them to the current whisker states each time through the main loop. It then plays a short tone to signal the program has started.

Notice that o_w_r is given an initial value of 1, not 0. This is necessary because the code always compares an alternating pattern; if o_w_l and o_w_r were both initialized to zero, the code would never find the second nested if statement to be true and would not count alternate corner presses.

The main routine starts by entering a forever loop. Just as with the roaming project, this loop begins by testing the whisker circuits’ input pin states and saving the resulting binary values to variables.

Now the fun begins! The outermost if statement uses the not-equals comparison operator ≠ to see if w_l and w_r are different, which indicates one of the whiskers is pressed—it does not matter which one! If this is true, the nested if statement right below it uses the not-equals operator again o_w_l ≠ w_l and to see if each whisker’s current input state is different from its old input state.

If it’s true that both current whisker input states are different from their previous input states, the cyber:bot must have gone from having one whisker pressed to having the opposite whisker pressed—in other words, it encountered a corner. This event is tracked by incrementing the variable counter by one. Then, the value of w_l is stored in o_w_l and w_r is stored in o_w_r to make another comparison next time through the loop.

Next comes the innermost of the three nested if statements. It checks to see if counter has exceeded four, pretty strong evidence that the cyber:bot is indeed stuck in a corner. If true, then a tone plays signalling an escape is imminent. Before that happens, counter, w_l, and w_r are all set to 1, so the cyber:bot will resume roaming after executing the backwards and two left function calls to escape.

These nested if statements end with an else that sets counter back to 1 before moving on to the regular roaming with whiskers behavior.

Finally, after exiting the outermost nested if block, the code moves on to a more familiar if…else if…else if…else similar to the one in roaming_with_whiskers. Note that only the first if…checks both whisker states, and the others use the minimal amount of logic needed to accomplish the navigation tasks.


Printer-friendly version

Leave a Comment Cancel reply

Escaping Corners
Prev

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress