With the nights beginning to get darker we need to ensure the :MOVE mini can continue to explore its surroundings by coding some automatic headlights, so that no matter what time of day or the ambient light levels, :MOVE mini can still find its way.
The idea was to see if we could use the micro:bit’s light sensors to detect when its surrounding become more gloomy and then use this to turn on the ZIP LEDs as well as having them glow brighter as light levels drop further. You can find a link to the completed code at the foot of this resource.
What you will need:
You will need to install the Neopixel blocks in the MakeCode
Editor for this one but don't worry as it barely takes a second! Simply click on the Advanced options on the left hand side of the coding menu. At the bottom of the new panel there will be an option to add Extensions, select the Neopixels in the window that opens and you are all set.
Automatic Headlights For :MOVE mini:
We need to write a code that will:
- Turn the ZIP LEDs on and off and dim or brighten them depending on the surroundings.
The on-start section is defining the ZIP LEDs on the Servo:Lite board, all nice and straightforward here. It also creates our first variable of the code namely “pixel array”. This is simply the variable to describe the LEDs we are using.
The Forever loop contains the bulk of the code and continually checks the light level and alter the variable we have entitled “headlights” which sets the brightness of the ZIP LEDs. Both the light level and the brightness of the LEDs we are using in the pixel array can be set from 0 to 255.
So, to set the brightness we first have the micro:bit detect the light level and minus this from 255 to reach the value of “headlights”. Then the micro:bit works out if we need to have the LEDs on or not. The 'else if' statement at the bottom ensures that if the light levels are rated at 85 or above the ZIP LEDs will be switched off again for all that energy saving goodness.
If it is dark however we use the headlights value to set the brightness of the LEDs. If this brightness is so low that it would cause a flicker (around the 44-47 mark) then the code uses a loop to ensure that the LED brightness is always above a set value.
The other thing you might notice is that the “else if” statement turns the LEDs off again at a higher threshold than when they are switched on, this is again to reduce the flickering of the LEDs.
You can either produce the code yourself or download the zip file below which contains the code for the head lights. Once you unzip the file you can either drop it straight onto microbit in file explorer or drop it into the MakeCode Editor for tweaking/viewing.
The code and LED Matrix respond better to natural light than artificial and some tweaking may be required to make them work more effectively in your surroundings.
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.