session03 - nsbejrFire/microbitRobot GitHub Wiki

Programming Session 03

This session focuses on user-defined functions. We use these to make programming the NeoPixels easier, but it also helps simplify how we use motor commands. We also talk about python lists and for loops.

Program Code

All the programming exercises can be found here:

  • First we wrote some programs that show specific numeric sequences on the display
  • We took a look at how to control NeoPixels which requires the use of list variables
  • Lastly, we returned to robot motor control
    • We looked at basic motor control commands in this example
    • In this example we write user-defined functions for motor control. This allows us to deal with the complication of the i2c.write() command inside our user-defined functions. Later in our program when we want to program robot motion, we can use our short and easy-to-read user-defined functions.

Challenges for Next Week

  • Enhance your Maqueen robot dance program
    • Last week's challenge used the clunky i2c.write() instruction to command the robot's motors. This week, try to use all the different motor command functions to make dance part of your program easier to read. Try to use all of these functions: forward(), backward(), turnLeft(), turnRight(), and stop()
    • Can you use the RGB LEDs in your robot's dance program?
    • The Maqueen robot has a tiny speaker that allows its Micro:Bit to play songs. Can you make Maqueen play its own music and dance to it?
      • hint: music.play(music.WAWAWAWAA) will first play then entire "song" before allowing other instructions to be performed; this is called a blocking function since it blocks other instructions from happening until the blocking function has completed its task. You can instead use the optional wait argument like this music.play(music.WAWAWAWAA,wait=False) to allow your program to do other instructions while the "song" is playing
  • Write a program to drive around a box on the ground
    • hint: use the forward(), turnLeft(), and turnRight() functions. You'll need to figure out how long the robot should drive forward, how long it should turn, then how long it should drive forward, etc...
    • Try to have your robot completely around the box and stop in roughly the same spot that it started. Can you program the robot to show red on its NeoPixels while driving around the box, then turn green when it thinks it has returned to its original spot? Maybe play a victory song at the end too!
  • Write functions called spinLeft() and spinRight() that make the robot spin in place at a user specified speed.
    • note the turnLeft() and turnRight() functions make the robot gradually turn

You can upload a video for your best robot dance or your best robot driving around a box example.