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
- This example shows the sequence 0, 1, 2, 3, 4, ...
- This example shows the sequence 5, 10, 15, 20, 25, ...
- This example shows the sequence 3, 10, 17, 24, 31, ...
- We took a look at how to control NeoPixels which requires the use of list variables
- Simple NeoPixel control is shown in this example. NeoPixel display colors are specified using decimal RGB color codes.
- Next we wrote a program that cycles through the primary colors of light in this example
- In this example we made the color cycle code simpler to implement using user-defined functions
- We briefly covered how
for
loops work in this example - Finally, we combined our user-defined functions, knowledge of variables,
while True
loops, andfor
loops to make our robot fade between colors in this example
- 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()
, andstop()
- 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 optionalwait
argument like thismusic.play(music.WAWAWAWAA,wait=False)
to allow your program to do other instructions while the "song" is playing
- hint:
- 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:
- Write a program to drive around a box on the ground
- hint: use the
forward()
,turnLeft()
, andturnRight()
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!
- hint: use the
- Write functions called
spinLeft()
andspinRight()
that make the robot spin in place at a user specified speed.- note the
turnLeft()
andturnRight()
functions make the robot gradually turn
- note the
You can upload a video for your best robot dance or your best robot driving around a box example.
- Your username is: First_name.Last_name
- FlipGrid link for Team 1
- FlipGrid link for Team 2
- FlipGrid link for Team 3
- FlipGrid link for Team 4
- FlipGrid link for Team 5