session05 - nsbejrFire/microbitRobot GitHub Wiki

Programming Session 05

This session is all about remote control. First we learn how to take analog measurements from the joystick on the GamePad. Then we learn how to convert those measurements into motor control commands. Finally, we use the wireless radio link to send data from the GamePad to the Maqueen robot.

Program Code

All the programming exercises can be found here:

  • First we reviewed some basics about how to read from buttons, which are digital inputs, and used those button measurements for conditional expressions to decide what to display on the screen in this example
  • Next we learned how to use the GamePad's joystick to control our robot
    • This example demonstrates how to use the read_analog() function to measure joystick position which we then display on the screen. This example needs to be flashed onto a Micro:Bit that is plugged into the GamePad to read the GamePad's joystick.
    • In this example we use a map() function to convert the joystick measurements, which range from 0 to 1023, to a range of 0 to 5 so we can represent the joystick position on the Micro:Bit's display. This example needs to be flashed onto a Micro:Bit that is plugged into the GamePad to read the GamePad's joystick.
    • Next we used a lot of math to convert our joystick measurements into numbers useful for motor commands in the ex04_radio_gamepad_scrolling.py example. The numbers are shown on the Micro:Bit display and broadcast via the Micro:Bit's radio. This example needs to be flashed onto a Micro:Bit that is plugged into the GamePad to read the GamePad's joystick.
  • Finally, we learned how to use the Micro:Bit radio on the Maqueen robot to allow us to remotely control Maqueen via the GamePad

Challenges for Next Week

The GamePad has more than just a joystick on it. Take a look at this example to learn how to read from the GamePad's colored buttons. Using the pinX.read_digital() instruction returns 1 if the corresponding button hasn't been pushed, but once the button has been pushed the pinX.read_digital() instruction returns 0 even after the button is no longer pushed! These are sometimes called "programmable buttons" and they need to be reset after the button press has been detected. The example shows how to do this.

Challenges:

  • Expand the code from ex07_radio_gamepad_no_display.py and ex06_radio_maqueen_control.py example so that pressing the GamePad's A button (left trigger) makes the Maqueen robot play a song.
    • Hint: make the message buffer larger by one byte. Have the GamePad's program send a number in the new buffer byte each time through its while loop to indicate if the A button has been pressed. Then have the Maqueen robot's program check that byte of the buffer to trigger playing a song.
    • Hint: check the hints in the challenges from Session 03 to see how to program the Micro:Bit to play a song while also continuing with other parts of its code.
    • Can you change the programs so that pressing the GamePad's B button (right trigger) changes which song to play? Perhaps you could show the selected song name on the GamePad's Micro:Bit's display.
  • Expand the code from ex07_radio_gamepad_no_display.py and ex06_radio_maqueen_control.py example so that pushing a colored button on the GamePad makes the Maqueen robot turn on its NeoPixels to the corresponding color.
    • Hint: make the message buffer larger by one byte. Have the GamePad's program send a number in the new buffer byte each time through its while loop to indicate if (and which) colored button was pressed. Then have the Maqueen robot's program check that byte of the buffer to see which (if any) color should be displayed on its NeoPixels.
    • Hint: we learned a lot about NeoPixels in Session 03 if you want to refresh your memory about how to use them.

You can upload a video showing off the extra features you implement for remote control. Uploading video will make you eligible to win a special end of season prize!