Skip to content

Instantly share code, notes, and snippets.

@damiensawyer
Last active October 31, 2021 21:50
Show Gist options
  • Save damiensawyer/9796ce09019298c41943cfa3caf70c75 to your computer and use it in GitHub Desktop.
Save damiensawyer/9796ce09019298c41943cfa3caf70c75 to your computer and use it in GitHub Desktop.
Neopixels moving between rainbow colours
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()
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