Last active
December 27, 2021 03:32
-
-
Save damiensawyer/3a7d8a606141a420a50d1a063263dac1 to your computer and use it in GitHub Desktop.
Micro:bit rotating rainbow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rotating Rainbow for GlowBit Rainbow and Micro:Bit (https://core-electronics.com.au/glowbit-rainbow.html) | |
// Can run with https://makecode.microbit.org | |
// Nb: You must import 'neopixel' extension into makecode before running | |
input.onButtonPressed(Button.A, function () { | |
maxb = maxb <= 0 ? 0 : Math.max(maxb - 10,0) | |
}) | |
input.onButtonPressed(Button.B, function () { | |
maxb = maxb >= 255 ? 255 : Math.min(maxb + 10,255) | |
}) | |
input.onLogoEvent(TouchButtonEvent.Pressed, function() { | |
reverse = !reverse | |
}) | |
input.onButtonPressed(Button.AB, function () { | |
speedIndex = speedIndex >= speedOptions.length - 1 ? 0 : ++speedIndex | |
showValueTime = input.runningTime() | |
basic.showNumber(speedIndex) | |
}) | |
let stretch = 2 // Set to 1, red will wrap instantlty from left edge to right. 2 will simulate a circle. Higher will stretch out the colours | |
let showValueTime = 0 | |
let displayTime = () => input.runningTime() - showValueTime | |
let speedOptions = [50,40,30,20,10,0] | |
let speedIndex = 3 | |
let maxb = 100 | |
let hslElementCount = 300 | |
let ledCount = 13 | |
let strip = neopixel.create(DigitalPin.P0, ledCount, NeoPixelMode.RGB) | |
let offset = 0 | |
let gap = Math.floor(hslElementCount / ledCount / stretch) | |
let reverse = true | |
basic.forever(function () { | |
const work = (offset:number)=>{ | |
if (displayTime() > 1000) basic.clearScreen() // hides the speed chnage number after a second | |
strip.setBrightness(maxb) | |
const p = (i: number) => { | |
} | |
for (let i = 0; i < ledCount; i++) { | |
let x = (gap * i) + offset | |
let y = x >= hslElementCount ? x - hslElementCount : x | |
let color = neopixel.hsl(y, 99, 10) | |
strip.setPixelColor(i, color) | |
} | |
strip.show() | |
basic.pause(speedOptions[speedIndex]) | |
} | |
if (reverse) | |
{ | |
for (let offset = 0; offset < hslElementCount; offset++) { | |
work(offset) | |
} | |
} | |
else{ | |
for (let offset = hslElementCount; offset > 0; offset--) { | |
work(offset) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment