Whisker-Wheel Response

Now let's try a program that makes the robot back up while you push and hold a whisker up against its breadboard post. 

  • Click SimpleIDE's Open Project button.
  • Navigate to ...Documents\SimpleIDE\Learn\Examples\Robots\ActivityBot360.
  • Open Whiskers Push Bot.side
  • Set the PWR switch to 1.
  • Click the Load EEPROM & Run button. Wait for it to finish loading and then switch PWR to 0 (off).
  • Disconnect your Bot from the USB cable and place it on the floor (do not let it roam on a desk or other elevated surface).
  • Set the PWR switch to 2.
  • Press and hold one of the whiskers against its contact post.  The robot should back up as long as you keep the whisker pressed. 

 

How it Works

Before starting the while(1) loop, the program has its usual freqout call to make the speaker beep.

Inside the main function’s while(1) loop, the first two lines should look familiar: they test the whisker input states and assign the result to wL and wR

Next, we have if((wL == 0) || (wR == 0)) followed by a code block.  It means, “if wL stores 0 OR wR stores 0, do what is inside the code block.”  So, if either variable does store 0, then, drive_speed(-64, -64) runs the robot's servos backwards for 20 ms. 

If neither whisker variable stores a 0, the program execution skips that if... code block and moves on to the else code block below.  There, it stops both servos with drive_speed(0, 0)

/*
  Whiskers Push Bot.c
  Push the whiskers to make the Propeller ActivityBot back up.
*/

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

int main()                                    // main function
{
  freqout(4, 2000, 3000);                     // Speaker tone: 2 s, 3 kHz

  while(1)
  {
    // Check whisker states.
    int wL = input(7);                        // Left whisker -> wL variable
    int wR = input(8);                        // Right whisker -> wR variable

    // If whisker pressed, back up
    if((wL == 0) || (wR == 0))                // Either whisker detects
    {
      drive_speed(-64, -64);                  // Back up
    }
    else                                      // Neither whisker detects
    {
      drive_speed(0, 0);                      // Stay still
    }
  }
}

Try This

Here is a modified loop for your main function.  It replaces the if...else statements with code that allow you to push one whisker at a time to make it turn away to one side or the other, or both whiskers to make it move straight backwards.

  • Click the Save As Project button. 
  • Name the project Whiskers Push Bot Improved.
  • Add the new variables speedLeft and speedRight above main.
  • Modify the while (1) loop so that it uses the two new if... statements and variables, matching the one above.
  • Click the Load EEPROM & Run button.
  • Press and hold each whisker. Holding individual whiskers should make it turn, and holding both whiskers should make it back up.

 

Your Turn

  • Modify Whiskers Push Bot.side so that it makes the robot back up for one full second each time you press a whisker.