Skip to content

Instantly share code, notes, and snippets.

@mrizvic
Last active March 26, 2017 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrizvic/2a4566d8938090f94debc1e400bd84a0 to your computer and use it in GitHub Desktop.
Save mrizvic/2a4566d8938090f94debc1e400bd84a0 to your computer and use it in GitHub Desktop.
fadein-fadeout effect using PWM in nodeMCU LUA for use with ESPtoy for example
redPin = 2
greenPin = 1
bluePin = 4
pwm.setup(redPin,500,0)
pwm.setup(greenPin,500,0)
pwm.setup(bluePin,500,0)
pwm.start(redPin)
pwm.start(greenPin)
pwm.start(bluePin)
green,red,blue = 1000,0,0
function brightness (x, max)
return math.floor((math.floor((x * max) / 100)*2) + 1)/2
end
step = 2
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)
pwm.setduty(redPin,r)
pwm.setduty(bluePin,b)
pwm.setduty(greenPin,g)
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