Remote Control
Invent Logo

We're now going to remote-control our robot! Your remote control is a complete computer module that can be programmed just like the main module of the Invent! robot. It doesn't have a speaker or the left and right LEDs, but is perfect for controlling other devices wirelessly.

We'll write a program on the remote to send key presses to the robot and we'll write a program on the robot to receive them and move in the desired direction.

On the left of your remote are 4 buttons for moving up, down, left and right.

Remote Control
The A and B buttons

On the right of your remote are two more buttons labelled 'A' and 'B'. You can use them for whatever you want - perhaps one as a 'Turbo' button to go faster!

The A and B buttons
Powering on

First turn on the remote control by sliding the power button to the right.

Just like your robot, your remote control should be configured either to connect to your WiFi network (in which case the 'Stat' LED will turn red and then green), or act as an access point and create its own WiFi network (and the LED will turn blue).

Powering on
Connecting

We won't explain how to connect to the remote as it is identical to connecting to the robot but make sure if you are using the 'WiFi' mode to connect to an existing WiFi network you enter the 6-character code for the remote control instead of the robot when you want to write a program for it.

Hint: You might want to pop up another browser tab to program the remote control and leave the existing tab for programming the robot. This way you can swap between them if you need to later.

Connecting
Testing

Get the 'Gamepad buttons' block. This block returns the buttons that are pressed on the remote.

Testing
Seeing how it works

You've previously used the 'Print' block in an infinite loop to test out new things you've tried like the line sensor or obstacle sensor.

Try out the gamepad buttons in the same way.

You should see that your test program prints things like 'U' if the up button is pressed, 'D' if the down button is pressed, 'L' for left and 'R' for right.

What happens when you press both the up and left button at the same time, or the down and the 'A' button?

Multiple buttons

You should see that if you press up and right, then it will print 'UR'. If you press down and left, it will print 'DL'.

If you can actually physically hit more buttons at once, you might be able to get it to print something like 'URA' which means these three buttons are pressed!

What it's doing is returning a 'string' where the first character is 'U' or 'D', the second character is 'L' or 'R', and the next two characters are 'A' and 'B' (if these are pressed).

Useful combinations

This is a simple way of handling multiple buttons. We can't detect if up and down are both pressed at the same time (or left and right), but that really wouldn't make sense anyway.

However, being able to detect up and left could be useful. It could also be useful to detect when we're hitting in our 'turbo' button (A or B) at the same time as moving the robot.

We're now ready to write our program for the remote control to send the button presses to the robot ...

Useful combinations
The remote control program
ESPNow

Create a new program and drag in these two blocks from the 'ESPNow' group.

'ESPNow' is a protocol (a way of communicating) that our main board can use to send messages directly between two devices. The 'ESPNow init' block initialises the communications system to get it ready.

The 'ESPNow add peer' block specifies which device we are connecting to. You'll need to enter the characters that are on the label on your robot. Previously we've just used the last 6 characters (the ones in bold), but this time you'll need all 12 characters starting with the 6 non-bold characters, then the 6 bold characters. Take care - if a single character is wrong, your code won't send the message to the right robot!

ESPNow
The basic programme

Now add some more blocks as shown.

Our basic code is fairly simple. We just need an infinite loop that sends the gamepad buttons to the robot. We'll do that about 20 times a second, so you'll need a 'delay 50 milliseconds' block. (50 milliseconds is 50 thousands of a second which is the same as 1/20th of a second).

The basic programme
Checking for changes of buttons

Actually, our program is a bit wasteful as we only have to send the buttons if something changes. To do this, we need to create a 'lastbuttons' variable and a 'buttons' variable (which will contain the currently pressed buttons), and compare the two. If they have changed, then we'll send the contents of the new 'buttons' variable.

This is what the code looks like. Modify yours to look the same. Can you see how it works? To create and use variables, use the blocks in the 'Variables' group.

Once you've written this program, make sure you save it, as we'll need it in a minute when we've written the robot's program that will receive these messages. Then open up a new tab for the code for your robot - keep this tab for your remote code.

Checking for changes of buttons
The robot program

Here's the basic program that you need to create for the receiver side (the robot) and how it works:

  • We continually try to receive a message into the 'packet' variable
  • If we have received a message, we check that it's length is 4 characters long (as our transmitter always sends 4 characters containing the buttons pressed)
  • If it is four characters, we check what's in the first and second letter of these characters (position zero and 1 as text strings count their first character as being in position zero), and move in an appropriate way.

You'll need to find all of these blocks and work out how to put them in the positions shown!

The robot program
The working RC robot!

Make sure you've saved a copy of this code to your computer, and then send it to the robot by pressing the play button.

Hopefully you've got your remote control code still up in another tab. Send the remote control code to your robot, and try it out - you should now be able to drive your robot around!

N.B. Can you see how we've used the 'no wait' movement blocks here? That's because we don't want the robot to finish moving 20cm forward until we check the buttons again. Actually the 20cm distance is ALMOST irrelevant. We could just have easily put 1cm or 5cm in here, but this distance limits how far the robot will travel if for some reason the remote control stops working or is turned off!

The working RC robot!
More advanced remote control

We're now going to modify our program so that the 'A' button makes it go into turbo mode. Can you work out how to do this? If you can, try it out now. If you need a tip, read on ...

Turbo mode

We've already got one enormous 'if-then' block that checks the direction we want to move.

Under this block, you can add another one that checks if you have pressed button A. If you have, then you'll want to set the speed to '10'.

Hints: You'll also want to set the speed back to defaut ('5') if the button isn't pressed. The 'A' button is in the third character of the string of button presses (which is position '2'!)

Police chase

Now we're going to add a 'police chase' option to our robot using both lights and sounds!

Police chase
Adding lights

Your first task is to use the 'B' button to flash the two lights on the main board red and green for 'police mode'! You can decide how long the lights flash for when the button is pressed.

Hint: The 'B' button is the fourth character of the 'buttons pressed string ' (so position '3').

Adding lights
Adding sounds

Can you now add a siren sound? You can choose the sound you make. The easiest would be a two-tone alternating siren. Perhaps the tones can change in time with the lights flashing? If you really want a challenge, see if you can make one that sweeps up (or down) in pitch.

Hint: For a sweeping-pitch siren, you'll need to create a variable that increases (or decreases) every time through a loop, and then use that variable to control the frequency of the speaker!

Adding sounds
Advanced police chase

If you really want a challenge, can you modify your program so that the lights and siren work whilst driving?

This is a really tricky one! Any time you use a 'wait' block in your code, your program wills top being run until the wait block finishes, so you need to come up with a way that doesn't wait around for long.

Hint: One idea is to add a delay (small one - let's say 10 milliseconds), every time you go through the main loop. Then create a variable that goes up every time through the loop. Then using the contents of this variable you can determine the times you want things like lights or sounds to change.