Simple IR Remote Control Code
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.
- Log into your BlocklyProp account and make a new project named IR Remote Drive.
- Build and save the project shown below.
- Be sure to set the Robot initialize block to the right setting for your robot: ActivityBot or ActivityBot 360°.
- Load the program to your ActivityBot's EEPROM, then put it on the floor in an open place.
- Press and hold the arrow keys to make the ActivityBot move.
How it Works
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!
Did you Know?
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.
- If the ActivityBot robots were instead heavy equipment operating in the real world, what dangers could this pose?
- If you just had a remote and no robot, could you still interfere with another person's robot? How could this cause problems in a real-world situation?
- If you just had an IR receiver and no remote or robot, could you still "spy" on the moves another person's robot was making? How could this cause problems in a real-world situation?
- Recall Your Turn - Test for IR Interference from the ActivityBot tutorial. In a real-world situation, what dangers could this vulnerability pose?
- Can you think of a way to make your ActivityBot respond only to your remote?
Try This - Add Speed Control
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:
- Save a copy of your project as IR Remote Drive Speeds.
- Make a new variable named speed.
- In the first four Robot drive speed blocks, replace every instance of 64 with the variable speed.
- In the first four Robot drive speed blocks, replace every instance of the -64 with the math operation block speed x -1. Here's an example for case 18.
- Add three more cases to your switch...case block, and attach number values 1, 2, and 3. Keep the (then break_) box checked.
- For each new case's do section, insert a Robot set acceleration for speed block.
- Then add a set variable speed block for each new case. For case 1 use speed = 32, for case 2 use speed = 64, and for case 3 use speed = 128.
- Adjust each acceleration block so speed = 32 uses 1200 ticks/s2, speed = 64 uses 600 ticks/s2, and speed = 128 uses 400 ticks/s2. Here's an example for case 1:
- Save the project and load it to your ActivityBot's EEPROM.
- On the IR remote, first press 1, 2 or 3, then press the arrow keys to drive.
Your Turn - Default Options
Here are two suggestions for optional improvements that you may want to make to your project.
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.
- Above the repeat forever loop, add blocks to initialize the variable speed to 32, 64, or whatever you like. This will be the default speed upon start-up until you press button 1, 2, or 3.
- If you choose a default speed that's either very fast or very slow, you may also want to add a default Robot set acceleration for speed block that's well matched to your chosen speed.
- Save, load, and test-drive your modified code.
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.
- Change case -1 to case 20.
- Save, load, and test-drive the modified code.
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.
- Do you have a piezospeaker and either whisker switches or a PING))) sensor on your ActivityBot already? Follow the link below to add them into the project.