This tutorial shows you how an IR remote can drive your ActivityBot, using BlocklyProp graphical programming. The only sensor needed is a single IR Receiver included with the ActivityBot Robot Kit. But, if your ActivityBot already has the PING))) sensor or Whisker switch circuits in place, leave them on and they can play too!
]
*These items are included with the ActivityBot Robot Kits (assembly required!)
If you have not used the IR Receivers with your ActivityBot before, take a look at the pictures below. The infrared receiver needs three connections: 5 V, GND, and an I/O pin to read the signal it sends. The circuitry inside the infrared receiver makes it safe to connect its signal pin to the Propeller I/O pin with a small resistor, even though the sensor is powered by 5 V.
The test program decodes signals received the SONY remote and displays them in the BlocklyProp Terminal.
You should see the remote button number displayed in the Terminal. Note that the Channel up/down, Volume up/down, and Mute buttons also return numbers. When no button is pressed you should see Remote button = none.” You may also see this message if the IR signal of the button that is pressed is not one that is recognized by this block’s library. Some remotes have a LOT of buttons!!!
The IR receiver is looking for infrared light (in the 980 nanometer range) that is pulsing at around 38 kHz. The IR remote sends short bursts of this pulsing 38 kHz infrared light, in a different on-off pattern for each button on the remote. While the IR receiver detects these bursts it sends a 0 the Propeller I/O pin, and a 1 when it does not. The C code underneath the BlocklyProp Sony Remote value block does the work of decoding the on-off pattern conveyed by the Propeller I/O pin.
The projects’s blocks are all inside a repeat forever loop (except for the comment at the top!)
The first action is for the Sony remote value block to check PIN 10, and then store the number it receives into the Remote Button variable. Next, a Terminal clear screen block is there to wipe away the old message each time through the repeat loop to make way for a new one.
Next, an if…do…else loop checks to see if Remote Button equals -1. If this is true, it means no recognized button on the remote is pressed, and the if…do block is executed. That is just a Terminal print text block with the message “Remote button = none. “
If Remote Button does NOT equal -1, the if…condition is NOT true, so a recognizable button must be pressed! In that case code execution drops to the else blocks. Here, a Terminal print text block and prints “Remote button = ” which is immediately followed by the decimal value stored in Remote Button.
After the if…do…else block, pause (ms) 200 gives the Terminal time to display the message before code execution returns to the top of the repeat forever loop.
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.
Right now, you can drive your ActivityBot right into an obstacle. But, if you already have the Ping))) Ultrasonic Distance Sensor or the whisker switches on board, you can program your ActivityBot for a little self-preservation!
The key is making the existing remote control loop conditional. That will let the remote control code run as long as no obstacle is detected. Placing this conditional loop inside of a repeat forever loop, and adding code for what to do If an obstacle is detected, is all it takes for the ActivityBot to defend itself!