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 the Whisker Test Code Works

How the script whiskers_detect_test works

The main part of the program is contained within the while True: loop, this is so the program continuously checks whether or not the whiskers are being touched and therefore it knows whether the LEDs should be on or off.

while True:

 

The first thing that the script does once it enters the loop is checks the whisker states using the read_digital function and stores them as a boolean 0 (whisker pressed) or 1 (whisker not pressed) to the variables whisker_left for the P7 state, and whisker_right for the P9 state. 

whisker_left = bot(7).read_digital()
whisker_right = bot(9).read_digital()

 

The script then enters into the first if statement.  It checks to see if the value of whisker_left is 0, if it is 0, then the micro:bit module uses the display.set_pixel command to light up the LED located at (4,2) using the maximum brightness of 9.  Any other value, with the only other boolean option being 1, falls under the else statement.  The else statement turns off the pixel located at (4,2) with a brightness of 0.

if whisker_left == 0:
    display.set_pixel(4, 2, 9)
else:
    display.set_pixel(4, 2, 0)

 

The second if statement does the same process for the other whisker.  It checks to see if the value of whisker_right is 0, and if so, then the micro:bit module lights up the LED located at (0,2) with the maximum brightness of 9 using the display.set_pixel command.  Any other value, with the only other boolean option being 1, falls under the else statement which turns off the pixel at (0,2) with a brightness of 0.

if whisker_right == 0:
    display.set_pixel(0, 2, 9)
else:
    display.set_pixel(0, 2, 0)

 

After it runs through the two if statements, the while True: loop resets and starts over by checking the whisker states again.


Printer-friendly version
Testing the Whiskers
Prev
Navigating by Touch
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress