Test the Whiskers

It’s important to test the whisker contacts before programming your robot to navigate with them.  That way, you can be sure the whiskers are sending the right messages.  Otherwise, they might be telling the Propeller that they detect something even though they didn't, or vice-versa. 

  • Make sure the robot's battery pack is plugged into the board.
  • Set the PWR switch to 1.
  • Click SimpleIDE’s Open Project button.
  • Navigate to SimpleIDE > Learn > Examples > ActivityBot.
  • Open Test Whiskers with Terminal.side.
  • Click the Run with Terminal button.  Without pressing any whiskers, it should display wL = 1 wR = 1.

If either of the numbers are zeros, or if either of them flicker between 1 and 0, there's probably a circuit error. 

  • If you have a circuit error, go back and double check to make sure all of your connections are correct.

Next, let's check to make sure the Propeller chip detects when the right whisker is pressed or makes contact with an obstacle. 

  • Press the right whisker so that it makes contact with the 3-pin header's front post (see 2 in the picture). 
  • Verify that the SimpleIDE Terminal displays wL = 1 wR = 0, which means that the right whisker is pressed.
  • If the SimpleIDE Terminal displays anything other than a steady wL = 1 wR = 0 while you have the right whisker pressed, find and fix your circuit error.
  • Repeat for the left whisker.  You'll want to see wL = 0 wR = 1 in the Parallax Serial Terminal.  Make sure it displays that way.  If it doesn't, find and fix any circuit errors.
  • If you press and hold both whiskers against posts, the Parallax Serial Terminal should display wL = 0 wR = 0.  Try that too.

 

How it Works

The main function starts with our now-familiar reset indicator code: freqout(4, 2000, 3000) makes the speaker connected to P4 beep for 2 seconds at a frequency of 3 kHz. 

After that, the program enters a while(1) loop that repeats endlessly.  Inside that loop, int wL = input(7) copies the value that input(7) returns into an int variable named wL (short for whisker-Left).  The input(7) call might return a 1, which would indicate that the left whisker is not pressed.  Or, it might return a 0, indicating that it is pressed.  In either case, that 1 or 0 value gets copied to the wL variable.  The int wR = input(8) statement works roughly the same way, except that it stores the 1 or 0 value for the right whisker circuit, which is connected to P8, into a variable named wR (short for whisker-Right). 

/*
  Test Whiskers with Terminal.c
 
  Display whisker states in terminal.  1 = not pressed, 0 = pressed.
 
*/

#include "simpletools.h"                      // Include simpletools header

int main()                                    // main function
{
  freqout(4, 2000, 3000);                     // Speaker tone: P4, 2 s, 3 kHz
  while(1)                                    // Endless loop
  {
    int wL = input(7);                        // Left whisker -> wL variable
    int wR = input(8);                        // Right whisker -> wR variable
    print("%c", HOME);                        // Terminal cursor home (top-left)
    print("wL = %d  wR = %d", wL, wR);        // Display whisker variables
    pause(50);                                // Pause 50 ms before repeat
  }
}

After storing the whisker states in the wL and wR variables, print("%c", HOME) sends the cursor to the home position.  The %c format string tells the print function to send the value of a char variable.   HOME is the number 1, and when print sends that value to the SimpleIDE Terminal, the terminal moves its cursor to the top-left “home” position.  After that, print("wL = %d  wR = %d", wL, wR) displays the values of the wL and wR variables.  This time, the %d format string made the print function display values as decimal characters, in this case, either 1 or 0 for the whisker states. 

 


Did You Know?

The simpletools library has 16 control characters you can use to do things like clear the screen and position the cursor. 

NameValueDescription
HOME1 Send cursor to the top-left (home) position
CRSRXY2Position the cursor.  Follow with x and y values.
Example places cursor 10 spaces in and 5 lines down:
print("%c%c%c ", CRSRXY, 10, 5);
CRSRLF3Move cursor one space to the left
CRSRRT4Move cursor one space to the right
CRSRUP5Move cursor one line up
CRSRDN6Move cursor one line down
BEEP7Make the host computer beep
BKSP8Backspace
TAB9Tab
NL10Send cursor to next line
CLREOL11Clear text to right of cursor
CLRDN12Clear text below cursor
CR13Carriage return
CRSRX14Position cursor x spaces to the right
CRSRY15Position cursor y lines down
CLS16Clear the display

 

Try This

Here is a program you can try that positions the cursor at 10 spaces over and 5 lines down before printing the whisker states.


  • Click the Save As Project button. 
  • Name the project Display Whiskers with CRSRXY and save it to the ActivityBot Tutorial folder.
  • Update the main function so that it matches the one above.
  • Click the Run with Terminal button and examine the table the SimpleIDE Terminal displays. 

 

Your Turn

  • Write a for loop with a print call that uses CRSRXY create this output.