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.
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.
/* * 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 }
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.