Disco Lights#
Note
This project is suitable for beginners to both CODAL and C++ development
This demo presents a rewrite of the classic Disco Lights example, rewritten in CODAL with a little extra.
In the original, we simply set all LED brightness to exactly the sound level we detect with the microphone which gives us an immediate response to sounds, but
does mean that the effect can flicker for short sounds.
Instead of simply reproducing this directly, here we will include a sustain
parameter which we can gently decrease to give us a smooth fade away effect.
Step 1. Initial Setup#
Create a new project as described in CODAL Projects and ensure that you can build a MICROBIT.hex
file without changing any code.
If you can build a MICROBIT.hex
, we should be left with a project structure all set up, and if we open source/main.cpp
in Visual Studio Code,
we should have the bare-bones of our new program.:
#include "MicroBit.h"
// The Micro:bit control object
MicroBit uBit;
// Our main function, run at startup
int main() {
// Loop forever
while( true ) {
// Scroll some text on the display
uBit.display.scroll( "Hello, Bootstrap!" );
}
// Will never return, but here so the compiler is happy :)
return 0;
}
Step 2. Turning on the screen#
The main way we talk to the Micro:bit hardware is through the codal::MicroBit
object.
This object automatically configures and creates interfaces to individual devices and systems on the board (generally on-demand,
so only features we actually use are set up).
In the program skeleton generated by codal-bootstrap
it has created a uBit
variable for us and we can use this to access
the codal::MicroBit::display
variable.
We can use this variable to control our display, although its worth noting at this stage that as we are using a Micro:bit v2, our
display includes the methods from codal::AnimatedDisplay
and codal::NRF52LEDMatrix
as well!
Note
If you see a Base Types
or Base Type
section on the API documentation for a class, you can click on the linked classes
to see the other class details.
CODAL is highly modular, so you may have to follow these links to find all the methods you need for your projects.
Thankfully, editors such as Visual Studio Code are clever enough to know this, and will prompt you with all actions you can use for the object you’re working with.
Step 3. Reading from the Microphone#
Step 4. Adding the Fade Effect#
The full application code:
#include "MicroBit.h"
// The Micro:bit control object
MicroBit uBit;
// An update to the basic "Disco Lights" demo found at:
// https://microbit.org/projects/make-it-code-it/disco-lights/
//
// This one adds a slight sustain time to the LED on-time, so they fade nicely
// rather than snapping off immediately.
int main()
{
// Turn on the audio subsystem
MicroBitAudio::requestActivation();
// Create an image with full brightness on all pixels
const char * const image ="\
255,255,255,255,255\n\
255,255,255,255,255\n\
255,255,255,255,255\n\
255,255,255,255,255\n\
255,255,255,255,255\n";
// Set the screen image to our new image
uBit.display.image = Image( image );
// Create a variable to hold the current brightness
float sustain = 0.0;
// Loop forever
while( true )
{
// Grab a sample and compare (and update) the sustain value if we're louder
// than it
float sample = uBit.audio.level->getValue();
if( sustain < sample )
sustain = sample;
// Actually write the brightness value to the entire screen
uBit.display.setBrightness( (int)sustain );
// Decay the brightness by 85% each loop if its non-zero
if( sustain > 0 )
sustain = sustain * 0.85;
// Delay a little, to slow down the effect
uBit.sleep( 10 );
}
// Will never return, but here so the compiler is happy :)
return 0;
}