Christmas Information: Kitronik will be closing 24th Dec December 2024, reopening on 2nd of January 2025. See our delivery schedule for more information.

Guide For Automatic Headlights For :MOVE mini
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. automatic headlights for move mini microbit main 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.  

Downloads:

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.   Note: 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.

2 comments

Mark Donnison

Mark Donnison

Hi David, thanks for the feedback and the code suggestion. There are many ways to skin the cat, but some are more efficient than others. I've flagged it for a revisit when it's possible.

David

David

You should replace your loop with a direct assignment as it's faster (although a good compiler may optimise your redundant loop). While (headlights < 50) Change headlights by +1 can be replaced with If (headlights < 50) Set headlights to 50 …which makes the change in one operation instead of a potential of 50 counts up to the required minimum level.

Leave a comment

All comments are moderated before being published