5. Maqueen LEDs
The maqueen buggy has 4 LEDs below the buggy.
5.1. Set up LEDs
Set up the maqueen buggy’s 4 LEDs.
- class MaqueenNeoPixels
- Set up the buggy’s LEDs for use.Use
leds = maqueen.MaqueenNeoPixels()
to use the buggy’s LEDs.
The code below imports the maqueen module and sets up the LEDs.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
5.2. MaqueenNeoPixels
The module makes it easy to use the front LEDs, the rear LEDS, and the front LEDs as indicators such as when turning.
When setting up the LEDs, the front, indicator and rear color settings are set to the default values below.
- class MaqueenNeoPixels(front=(20, 20, 20), indicator=(35, 25, 0), rear=(20, 0, 0))
front=(20, 20, 20)
sets a low level white light.indicator=(35, 25, 0)
sets a low level yellow light.rear=(20, 0, 0)
sets a low level red light
When setting up the LEDS, the default LED colors can be set to different values.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels(front=(0, 20, 0), indicator=(35, 25, 0), rear=(50, 0, 0))
leds.front_lights()
5.3. Buggy lights and indicators
There are four convenient methods that use the default LED settings.
They all use the default red color for the rear lights.
They are named based on their effect on the front LEDs.
- front_lights()
- Shows white at the front and red at the back.
- left_indicator()
- Shows yellow front left, white front right and red at the back.
- right_indicator()
- Shows yellow front right, white front left and red at the back
- both_indicators()
- Shows yellow at the front and red at the back.
The code below shows white at the front and red at the back.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
leds.front_lights()
Tasks
Write code to alternate between both front LEDs being white and yellow. Use a 500ms sleep.
Write code to blink the left indicator on and off each second.
Write code to blink the right indicator on and off each second.
Write code to alternate the left and right indicators each second.
5.4. Primary and secondary colors
Primary and secondary colors are shown below.
The Red Green Blue (RGB) values for them are listed.
They are tuples of 3 integers from 0 to 255, where 0 is off and 255 if the brightest.
white = (255, 255, 255)
red = (255, 0, 0)
yellow = (255, 255, 0)
green = (0, 128, 0)
cyan = (0, 255, 255)
blue = (0, 0, 255)
magenta = (255, 0, 255)
5.5. Set buggy lights and indicators
The default light settings can be altered.
5.5.1. set_front
- set_front(rgb=(20, 20, 20))
- Set the front light LED color to be used when
front_lights()
,left_indicator()
orright_indicator()
are used.rgb
is a tuple of 3 integers from 0 to 255, where 255 is full brightness.If no value for rgb is passed the default value of (20, 20, 20) will be set.
The code below sets the white lights at the front to full brightness.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
leds.set_front(rgb=(255, 255, 255))
leds.front_lights()
Tasks
Write code to set the front LEDs to cyan.
Write code to set the front LEDs to blue.
5.5.2. set_indicator
- set_indicator(rgb=(35, 25, 0))
- Set the front light LED color to be used when
left_indicator()
,right_indicator()
orboth_indicators()
are used.rgb
is a tuple of 3 integers from 0 to 255, where 255 is full brightness.If no value for rgb is passed the default value of (35, 25, 0) will be set.
The code below sets the indicator to yellow at the front to full brightness.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
leds.set_front(rgb=(255, 255, 0))
leds.front_lights()
Tasks
Write code to set the indicator color to magenta.
Write code to set the indicator color to green.
5.5.3. set_rear
- set_rear(rgb=(20, 0, 0))
- Set the rear light LED color to be used when when
front_lights()
,left_indicator()
,right_indicator()
orboth_indicators()
are used.rgb
is a tuple of 3 integers from 0 to 255, where 255 is full brightness.If no value for rgb is passed the default value of (20, 0, 0) will be set.
The code below sets the red lights at the back to full brightness.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
leds.set_rear(rgb=(255, 0, 0))
leds.front_lights()
Tasks
Write code to set the rear LEDs to green.
Write code to set the rear LEDs to blue.
5.6. Set LEDs
Each LED can be set separately.
5.6.1. set_led
- set_led(led_number, rgb=(20, 20, 20))
- Set and show the LED color.
led_number
is 0 for front left, 1 for rear left, 2 for rear right and 3 for front right.rgb
is a tuple of 3 integers from 0 to 255, where 255 is full brightness.
The code below sets the front right LED to blue.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
leds.set_led(3, rgb=(0, 0, 255))
Tasks
Write code to set the rear left LED to magenta.
Write code to set the rear right LED to yellow.
5.6.2. set_leds
- set_leds(rgb=(20, 20, 20))
- Set and show the same color for all the LEDs.
rgb
is a tuple of 3 integers from 0 to 255, where 255 is full brightness.
The code below sets all the LEDs to cyan.
from microbit import *
import maqueen
leds = maqueen.MaqueenNeoPixels()
leds.set_leds(rgb=(0, 255, 255))
Tasks
Write code to set all the LEDs to green.
Write code to set all the LEDs to blue.