0

$USD

$USD
PROJECTS micro:bitChristmas

Christmas Ornament with micro:bit Expansion Board

DFRobot Dec 23 2018 1364

Story

I made a christmas ornament with DFRobot micro:bit Circular RGB LED Expansion Board.




Christmas Ornament

When you press the A button, Christmas song will flow from buzzer with expansion board. The notation is displayed on the LED matrix of micro: bit, NeoPixel glows red and green.
Pressing the B button puts you in a mode that reacts with sound. I am using a microphone with expansion board. NeoPixel glows yellow when detecting sound, and "Merry Xmas" appears on LED matrix.


MakeCode

I used MakeCode for coding.

https://makecode.microbit.org/

MakeCode Blocks

This is the MakeCode block for Christmas ornament created below.



1. Initialize



Initialization of variable, NeoPixel input pin, cell count and brightness, BPM for melody playback are specified.

A Mode

This mode is executed when the A button is pressed.

2. A button detection
 



Press A button on micro:bit to set the variable state to 1.

 

3. NeoPixel lighting setting



When the variable state is 1 (when the A button is pressed), alternately repeat red / green blinking NeoPixel on the expansion board.

 

4. LED Matrix Setting
 



When the variable state is 1 (when the A button is pressed), two notes are alternately displayed in the micro: bit LED matrix.

 

5. Audio playback
 



When the variable state is 1 (when the A button is pressed), Christmas songs are played repeatedly from the buzzer of the expansion board. When the variable state becomes a value other than 1, it exits the loop and stops sound playback.

 

B Mode

This mode is executed when the B button is pressed.

6. B button detection
 



Press the B button of micro:bit to set the variable state to 0 and reset the LED matrix display.

7. Sound detection
 



When the variable state is 0 (when the B button is pressed), reset the NeoPixel of the expansion board and turn on MIC detection.

When voice is detected with MIC (more than 200 in analog value), set the variable state to 2, and light up 9 NeoPixels yellow.

When the variable state is 2 (at the time of speech detection), it rotates 9 NeoPixel's yellow lights.

 

8. LED Matrix Setting
 



When the variable state is 2 (when voice is detected), the character string "Merry Xmas" is displayed in the micro: bit LED matrix.

When the display is completed, the variable state is set to 0 and wait for voice detection.

 

9. Reset



Simultaneously press A button and B button to reset NeoPixel and LED matrix to stop operation.

 

MakeCode JavaScript

let sound = 0
let state = 0
input.onButtonPressed(Button.A, function () {
    state = 1
})
input.onButtonPressed(Button.B, function () {
    state = 0
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
    `)
})
input.onButtonPressed(Button.AB, function () {
    state = 3
    strip.clear()
    strip.show()
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
    `)
})
let range: neopixel.Strip = null
let strip: neopixel.Strip = null
state = 3
strip = neopixel.create(DigitalPin.P2, 24, NeoPixelMode.RGB)
strip.setBrightness(30)
music.setTempo(52)
basic.forever(function () {
    while (state == 1) {
        strip.clear()
        for (let index = 0; index < 24; index+=2) {
            range = strip.range(index, 1)
            range.showColor(neopixel.colors(NeoPixelColors.Red))
        }
        for (let index2 = 1; index2 <= 24; index2+=2) {
            range = strip.range(index2, 1)
            range.showColor(neopixel.colors(NeoPixelColors.Green))
        }        
        basic.pause(500)
        for (let index3 = 0; index3 < 24; index3+=2) {
            range = strip.range(index3, 1)
            range.showColor(neopixel.colors(NeoPixelColors.Green))
        }
        for (let index22 = 1; index22 <= 24; index22+=2) {
            range = strip.range(index22, 1)
            range.showColor(neopixel.colors(NeoPixelColors.Red))
        }
        basic.pause(500)
    }
})
basic.forever(function () {
    while (state == 1) {
        basic.showLeds(`
            . . # . .        
            . . # # .
            . . # . #
            # # # . .
            # # # . .
        `)
        basic.pause(500)
        if (!(state)) {
            break
        }
        basic.showLeds(`
            . # # # #
            . # . . #
            . # . . #
            # # . # #
            # # . # #
        `)
        basic.pause(500)
    }
})
basic.forever(function () {
    while (state == 1) {
        music.playTone(196, music.beat(BeatFraction.Quarter))
        music.playTone(330, music.beat(BeatFraction.Quarter))
        music.playTone(294, music.beat(BeatFraction.Quarter))
        music.playTone(262, music.beat(BeatFraction.Quarter))
        if (!(state)) {
            break
        }
        music.playTone(196, music.beat(BeatFraction.Whole))
        music.playTone(196, music.beat(BeatFraction.Quarter))
        music.playTone(330, music.beat(BeatFraction.Quarter))
        music.playTone(294, music.beat(BeatFraction.Quarter))
        music.playTone(262, music.beat(BeatFraction.Quarter))
        if (!(state)) {
            break
        }
        music.playTone(220, music.beat(BeatFraction.Whole))
        music.playTone(220, music.beat(BeatFraction.Quarter))
        music.playTone(349, music.beat(BeatFraction.Quarter))
        music.playTone(330, music.beat(BeatFraction.Quarter))
        music.playTone(294, music.beat(BeatFraction.Quarter))
        if (!(state)) {
            break
        }
        music.playTone(247, music.beat(BeatFraction.Whole))
        music.playTone(392, music.beat(BeatFraction.Quarter))
        music.playTone(392, music.beat(BeatFraction.Quarter))
        music.playTone(349, music.beat(BeatFraction.Quarter))
        music.playTone(294, music.beat(BeatFraction.Quarter))
        if (!(state)) {
            break
        }
        music.playTone(330, music.beat(BeatFraction.Whole))
    }
})
basic.forever(function () {
    if (state == 0) {
        strip.clear()
        strip.show()
        sound = pins.analogReadPin(AnalogPin.P1)
        if (sound > 200) {
            state = 2
            strip.range(0, 9).showColor(neopixel.colors(NeoPixelColors.Yellow))
        }
    }
    if (state == 2) {
        strip.rotate(1)
        strip.show()
        basic.pause(50)
    }
})
basic.forever(function () {
    if (state == 2) {
        basic.showString("Merry Xmas")
        strip.clear()
        strip.show()
        state = 0
    }
}) 

The entire JavaScript code is described below.

Operation

It is very cute when you put it on a Christmas tree.


Sound detection mode is also perfect.

micro: bit Circular RGB LED Expansion Board is equipped with a microphone and buzzer so we can quickly make something fun by further improving the usefulness of micro: bit!

 

This project is made by HomeMadeGarbage.