The BlocklyProp project below is a stratightforward way to achieve basic remote control driving. In this example, the IR remote number values used correspond to the Channel Up (16) , Channel Down (17), Volume Up (18), and Volume Down ( 19) arrows on the Brightstar remote sold by Parallax. If you are using a different Sony remote, use the program on the previous page to identify the numbers that correspond to the buttons you'd like to use to drive your ActivityBot.
The code is very straightforward. After the required Robot initialize block, the rest of the code is in a repeat forever loop.
First, the Sony Remote value block checks the IR receiver on PIN 10, and then stores the number received in the Button variable. Then, a switch...case block does the rest. At switch, the Button variable's number is read, and then the program jumps to the case with the matching number. Four of the case numbers correspond to the four arrow buttons on the Brightstar remote: 16, 17, 18, and 19. The fifth case is -1, which is the value the Sony remote block provides when the IR receiver is not detetcting anything.
When the program jumps to a specific case number, the blocks in its do section gets executed. Here, that's always a Robot drive speeds block. Each one has left and right values to perform the maneuver that goes with the button pressed. The case -1 makes the ActivityBot wheels stop turning, so that the robot will only move while you hold down one of the other buttons.
If a button other than 16, 17, 18, 19, is pressed, the program jumps to case default at the bottom. It just holds a break block, which makes the program break out of the switch...case block. Also notice that every other case has the (then break _) checkmarked checked. So, only one case is executed at a time, then the program returns to the top of the repeat forever loop, to check if you are now pressing a different button!
If you are just one of several people doing this activity in a small space, you have likely run into a problem! You may find that you are unexpectedly controlling robots in addition to your own. Or, you may find your robot responding to another person's remote. The communication between the remote and the ActivityBot is not at all private.
Right now, you have control over the direction your ActivityBot drives and turns, but not over its speed. Let's use some of the number buttons as different speed settings that you can change during run time. It will take several steps:
Here are two suggestions for optional improvements that you may want to make to your project.
Default Speed
With the current IR Remote Drive Speeds code, when you first power up your ActivityBot, none of the drive buttons will make the robot move until you first press 1, 2 or 3 to select a speed value to use. That is because the variable speed defaults to zero until it is changed by executing one of those three cases.
Press & Hold vs Set & Forget
Right now, the ActivityBot only moves while you are pressing and holding a button on the remote. This is a wise strategy while you are developing code or working in a crowded place. But, with a simple change, you can make the motion buttons set-and-forget style.
The ActivityBot should now keep going after you briefly press an arrow button, and stop when you press the mute button. Keep in mind that if you change the speed with the 1, 2, or 3 button, the new setting will not be evident until you press an arrow button again.
Links
[1] http://learn.parallax.com/tutorials/robot/activitybot/blocklyprop-robotics-activitybot/navigate-infrared-flashlights/test-ir