session02 - nsbejrFire/microbitRobot GitHub Wiki

Programming Session 02

Welcome back! In this session, we put together the Maqueen robot. We learn how to flash Maqueen's red LEDs, use variables to keep track of information, and finally send commands to make Maqueen's wheels spin.

Variables

This session introduces variables. Variables are a way to store information, like a number, in a program. We can later retrieve that information, modify it, or replace it.

In example 05, we count by storing a number in a variable. Then we use a while True loop to repeatedly add one to the number in the variable.

You can name a variable (almost) anything you want. It is really helpful to give your variables a useful name that reminds you of the purpose of the variable. Example 06 has several variables in it; it wouldn't be very helpful if we named the variables variable1, variable2, and variable3. Instead, we use the variable name ADDR_MOTOR_CNTRL to store the numerical address of the motor controller, we use DIR_BACKWARD to store the number associated with a "spin the wheel backward" command, and speed to store the number used for wheel spin speed commands.

As mentioned previously, you can name a variable almost anything you want. Here are the rules for python variable names:

  • A variable name must start with a letter or the underscore character ( _ )
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age, and AGE are three different variables)
  • A variable name cannot be a python keyword
    • The python keywords are: and, as, assert, break, class, continue, def, del, elif, else, except, finally, False, for, from, global, if, import, in, is, lambda, nonlocal, None, not, or, pass, raise, return, True, try, with, while, yield
    • The python keywords are special words that already have meaning for python

Legal (allowed) variable names:

  • count
  • wheelSpeed
  • wheel_speed
  • WheelSpeed
  • Wheel23
  • super_long_variable_name

Illegal variable names:

  • while
  • import
  • wheel speed
  • 23wheel
  • wheel-speed

Program Code

All the programming exercises can be found here:

  • First we reviewed the code from last week to blink an LED on the display as this week's example 01
  • We then modified this week's example 01 to instead blink Maqueen's left red LED in example 02
  • We then modified the code even more to make both of Maqueen's front red LEDs turn on and off like the flashing lights of an emergency vehicle in example 03
  • We tried to make a program that would count on the display in example 04, but it only counts as high as we type out individual instructions in the program code
  • In example 05, we wrote a program that counts as long as the Micro:Bit is powered up by using a variable (a chunk of the Micro:Bit's memory).
  • We finally got Maqueen's wheels spinning in example 06. The program will only run properly if the robot is switched on. The i2c.write() command only works if the robot is powered by its batteries. A USB cable can power the Micro:Bit, but won't power the robot's motors or motor controller chip.

Challenges for Next Week

  • Write a program that makes your Maqueen robot dance
    • Try to make your robot: move forward, move backward, spin left, spin right
    • Can you use the front red LEDs and the Micro:Bit's display to enhance 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: 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 circle the box and stop in roughly the same spot that it started

You can upload a video for your best robot dance or your best robot driving around a box example. Uploading video(s) will make you eligible to win a special end of season prize!