Now let's try a program that makes the robot back up while you push and hold a whisker up against its breadboard post.
- Log into your BlocklyProp account and create a new project named Whisker Wheel Response.
- Set the Activity Board's power switch to 1, and build the program shown below.
- Be sure to set the Robot initialize block's dropdown to your particular robot: ActivityBot or ActivityBot 360°.
- Save the project, then click the Load and run (save to EEPROM) button.
- Disconnect your robot from the USB cable and place it on the floor.
- Set the power 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
The first two blocks may look familiar now and are going to be at the start of all the navigation programs from here on out. First, the frequency out block makes a 3000 Hz, 1-second beep on the piezospeaker connected to P4. Remember, that's the brownout indicator that lets you know if the program restarts due to low batteries. The second is the Robot ActivityBot initialize block, which must be at the top of the program come before any other Robot drive... blocks.
The rest of the code is inside a repeat forever loop. Two variables are declared next, each with a check PIN block attached. The variable left wheel stores the input state of the left whisker circuit connected to P7, and right wheel stores the input state of the right whisker circuit connected to P8. Remember, these whisker circuits provide active low outputs. If a whisker is pressed, the I/O pin connected to that circuit will sense 0 volts so that the check PIN block would provide a 0. If a whisker is NOT pressed, check PIN would provide a 1.
First thing in the loop, an If...do...else block checks to see if either whisker is pressed. The attached if condition blocks could be read as "if left wheel equals zero or right wheel equals zero, do..." The condition is built using a boolean comparison block set to or. Inside that are two compare value blocks, each individually checks whether a variable (left wheel or right wheel) equals zero.
If either whisker is pressed, the if condition is true, and the block enclosed by if...do gets executed. In this case, that's the Robot drive speed, with both wheels turning at -64 ticks per second causing the ActivitBot to back up.
If neither whisker variable stores a 0, the program execution skips the block enclosed by if...do and moves on to the block enclosed by else...if. There, a Robot drive speed left 0 right 0 block makes both wheels stop turning.
Did You Know?
Check early, check often! — Notice there is no pause block in the repeat forever loop above which allows the loop to execute very rapidly, monitoring the sensors and therefore updating which drive speed block is in use frequently. This allows the robot to quickly respond to sensor input with the appropriate motion. It's something to keep in mind when navigating with sensors.
...and don't block me! — Any code inside a loop that is blocking - code that does not let any other process proceed until complete - will slow down that loop. Examples of "blocks that block" are pause (ms) and frequency out. But, you can use this to your advantage by placing pause or frequency out right after robot drive speed to control how long the robot performs an evasive maneuver inside of a larger loop.
Try This
Here is a modified loop for your main function. It replaces the if...else statements with code that allows 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 backward.
- Save a copy of your project and name something like Whisker Wheel Try This.
- Update the repeat forever loop so that it matches the one above.
- Put the robot's power switch in position 1.
- Click Save, then click Load and run (save to EEPROM).
- Disconnect the cable and put the power switch in position 2.
- Press and hold each whisker one at a time, then press and hold both at once.
Your ActivityBot should turn to the side away from whichever whisker is pressed. If you press both whiskers at once, it should back up.
Your Turn
Right now, if you press both of your robot's whiskers at the same time, it will awkwardly wiggle as it backs up with pivoting turns. Can you fix that?
- Save another copy of your project and name it something like Whisker Wheel Your Turn.
- Modify the project so that your AcitivtyBot backs up smoothly when you press both whiskers, but still turns to the side away from whichever whisker is pressed individually.