Testing the Whiskers

The next sketch tests the whiskers to make sure they are functioning properly, by displaying the binary values returned by digitalRead(7) and digitalRead(5).  This way, you can press each whisker against its 3-pin header on the breadboard, and see if the Arduino’s digital pin is sensing the electrical contact.   

When neither whisker is pressed up against its 3-pin header, you can expect your Serial Monitor to display two columns of 1’s, one for each whisker. If you press just the right whisker, the right column should report 0, and the display should read 10.  If you press just the left whisker, the left column should report 1 and the display should read 01.  Of course, if you press both whiskers, it should display 00.

Serial Monitor screencaptures displaying output states when right, left, an no whisker is pressed

Active-low Output
The whisker circuits are wired for active-low output, which means that they each send a low signal when they are pressed (active) and a high signal when they are not pressed.  Since digitalRead returns 0 for a low signal and 1 for a high signal, 0 is what tells your sketch that a whisker is pressed, and 1 tells it that a whisker is not pressed.

  • Enter, save, and upload TestWhiskers to your Arduino.
  • Reconnect the USB cable and set the 3-position switch to position 1.
  • As soon as the sketch is finished uploading, open the Serial Monitor.
  • Leave the USB cable connected so that the Arduino can send serial messages to the Serial Monitor.

Example Sketch: DisplayWhiskerStates

/*
 * Robotics with the BOE Shield - DisplayWhiskerStates
 * Display left and right whisker states in Serial Monitor.
 * 1 indicates no contact; 0 indicates contact.
 */

void setup()                                 // Built-in initialization block
{
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone
  pinMode(7, INPUT);                         // Set right whisker pin to input
  pinMode(5, INPUT);                         // Set left whisker pin to input  

  Serial.begin(9600);                        // Set data rate to 9600 bps
}  
 
void loop()                                  // Main loop auto-repeats
{                                            
  byte wLeft = digitalRead(5);               // Copy left result to wLeft  
  byte wRight = digitalRead(7);              // Copy right result to wRight

  Serial.print(wLeft);                       // Display left whisker state
  Serial.println(wRight);                    // Display right whisker state

  delay(50);                                 // Pause for 50 ms
}    
  • Look at the values displayed in the Serial Monitor.  With no whiskers pressed, it should display 11, indicating 5 V is applied to both digital inputs (5 and 7).
  • Press the right whisker into its three-pin header, and note the values displayed in the Serial Monitor.  It should now read 10.
  • Release the right whisker and press the left whisker into its three-pin header, and note the value displayed in the Serial Monitor again.  This time it should read 01.
  • Press both whiskers against both three-pin headers.  Now it should read 00.
  • If the whiskers passed all these tests, you’re ready to move on.  If not, check your sketch and circuits for errors.

These steps are important!  
Seriously, you’ve got to make sure your circuit and code pass these tests before continuing.  The rest of the examples in this chapter rely on the whiskers working correctly.  If you haven’t tested and corrected any errors, the rest of the examples won’t work right.