Last active
March 22, 2017 09:25
-
-
Save mrizvic/2a896d0c642730243feb5516382eb022 to your computer and use it in GitHub Desktop.
fadein-fadeout effect on nodeMCU using WS2812 LED
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
-- 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