Skip to content

Instantly share code, notes, and snippets.

@mrizvic
Last active March 22, 2017 09:25
Show Gist options
  • Save mrizvic/2a896d0c642730243feb5516382eb022 to your computer and use it in GitHub Desktop.
Save mrizvic/2a896d0c642730243feb5516382eb022 to your computer and use it in GitHub Desktop.
fadein-fadeout effect on nodeMCU using WS2812 LED
-- nodeMCU specific
ws2812.init()
-- 4 LEDS, 3 bytes per LED
buffer = ws2812.newBuffer(4, 3)
green,red,blue = 255,64,192
buffer:fill(green,red,blue)
function brightness (x, max)
return math.floor((math.floor((x * max) / 100)*2) + 1)/2
end
step = 3
dir = 1
cntr = 0
r = red
g = green
b = blue
function fader()
if cntr > 100 then
dir = -1
cntr = 100
elseif cntr < 0 then
dir = 1
cntr = 0
end
r = brightness(cntr, red)
g = brightness(cntr, green)
b = brightness(cntr, blue)
cntr = cntr + (step*dir)
buffer:fill(g,r,b)
ws2812.write(buffer)
end
tmr.alarm(0,25,1,fader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment