首先是購買配件
nodemcu選擇便宜的esp8266
燈帶用了以前剩下的ws2801技肩,(大家如果買的話建議買ws2811/2812)
刷nodemcu固件
默認(rèn)固件不支持ws2801,去官網(wǎng)適配支持ws2801的固件刷入esp8266
win下開發(fā):http://www.reibang.com/p/af28ee8a3d06
win下開發(fā):http://www.reibang.com/p/a2482b542f45
mac刷固件詳細(xì)過程:http://www.reibang.com/p/e06c621d32fb
引出ws2801四個(gè)引腳
四根線分別是
vcc(我買的對(duì)應(yīng)5v)
ck (時(shí)鐘線)
si (信號(hào)線)
gnd (gcc)
其中注意vcc和gnd要外接usb電源(如果是12v的也需要額外的12v電源)不要讓nodemcu提供5v電源俗冻,會(huì)把板子燒了
接線
引出的ck線接D3口(gpio0)
引出的si線接D4口 (gpio2)
代碼
因?yàn)槭莾?nèi)建模塊,所以用法很簡單
初始化
ws2801.init(0,2)
將所有燈泡熄滅
ws2801.write(string.char(0,0,0):rep(100))
設(shè)置燈的顏色
ws2801.write(string.char(0,50,0))
其中數(shù)值第一位為紅色牍颈,第二位為藍(lán)色迄薄,第三位為綠色,數(shù)值的范圍是0-255煮岁,這和官方文檔不一樣讥蔽,官方文檔對(duì)應(yīng)的藍(lán)色和綠色是對(duì)調(diào)的,這不影響實(shí)現(xiàn)的最終效果
最后的代碼
ws2801.init(4,2)
ws2801.write(string.char(0,0,0):rep(100))
function HSL(hue, saturation, lightness)
local chroma = (1 - math.abs(2 * lightness - 1)) * saturation
local h = hue/60
local x =(1 - math.abs(h % 2 - 1)) * chroma
local r, g, b = 0, 0, 0
if h < 1 then
r,g,b=chroma,x,0
elseif h < 2 then
r,b,g=x,chroma,0
elseif h < 3 then
r,g,b=0,chroma,x
elseif h < 4 then
r,g,b=0,x,chroma
elseif h < 5 then
r,g,b=x,0,chroma
else
r,g,b=chroma,0,x
end
local m = lightness - chroma/2
return math.floor((r+m)*255),math.floor((g+m)*255),math.floor((b+m)*255)
end
turntype =4
loopNum = 0
direction = 1
--tmr.delay(1000000)
--ws2801.write(string.char(0,50,0))
ledNum = 25
ledColors = ledNum*3
ledPool = {}
ledPoolTo = {}
for i=1, ledColors,3 do
ledPool[i] = 0
ledPool[i+1] = 0
ledPool[i+2] = 0
local cr,cg,cb = HSL(math.random(0,255),1,.1)
ledPoolTo[i]=cr
ledPoolTo[i+1]=cg
ledPoolTo[i+2]=cb
-- ledPoolTo[i]=math.random(65,90)
-- print(cr,cg,cb)
end
function turnLed()
local str = ""
for i=1, ledColors, 3 do
-- ledPool[i]=ledPool[i]+(ledPoolTo[i]-ledPool[i])/10
-- ledPool[i+1]=ledPool[i+1]+(ledPoolTo[i+1]-ledPool[i+1])/10
-- ledPool[i+2]=ledPool[i+2]+(ledPoolTo[i+2]-ledPool[i+2])/10
str=str..string.char(ledPoolTo[i],ledPoolTo[i+1],ledPoolTo[i+2])
end
ws2801.write(str)
-- print(str)
end
function turnLedTo()
local str = ""
if turntype==1 then
for i=1, ledColors, 3 do
local cr,cg,cb = HSL(math.random(0,255),1,.05)
ledPoolTo[i]=cr
ledPoolTo[i+1]=cg
ledPoolTo[i+2]=cb
str=str..string.char(ledPoolTo[i],ledPoolTo[i+1],ledPoolTo[i+2])
end
elseif turntype==3 then
loopNum=(loopNum+1)%4
for i=1, ledColors, 3 do
if (i+loopNum)%4~=0 then
str=str..string.char(0,0,0)
else
local cr,cg,cb = HSL(math.random(0,255),1,.05)
ledPoolTo[i]=cr
ledPoolTo[i+1]=cg
ledPoolTo[i+2]=cb
str=str..string.char(ledPoolTo[i],ledPoolTo[i+1],ledPoolTo[i+2])
end
end
elseif turntype==2 then
loopNum=(loopNum+1)%ledNum
for i=1, ledColors, 3 do
if i~=(loopNum*3+1) then
str=str..string.char(0,0,0)
else
local cr,cg,cb = HSL(math.random(0,255),1,.05)
ledPoolTo[i]=cr
ledPoolTo[i+1]=cg
ledPoolTo[i+2]=cb
str=str..string.char(ledPoolTo[i],ledPoolTo[i+1],ledPoolTo[i+2])
end
end
elseif turntype==4 then
if math.random(0,100)>90 then
direction = -direction
end
loopNum=loopNum+direction
if loopNum<0 then
loopNum = ledNum-1
elseif loopNum>=ledNum then
loopNum = 0
end
for i=1, ledColors, 3 do
if i~=(loopNum*3+1) then
str=str..string.char(0,0,0)
else
local cr,cg,cb = HSL(math.random(0,255),1,.05)
ledPoolTo[i]=cr
ledPoolTo[i+1]=cg
ledPoolTo[i+2]=cb
str=str..string.char(ledPoolTo[i],ledPoolTo[i+1],ledPoolTo[i+2])
end
end
end
ws2801.write(str)
end
--tmr.stop(0)
--tmr.alarm(0, 50, tmr.ALARM_AUTO, turnLed)
tmr.stop(1)
tmr.alarm(1, 50, tmr.ALARM_AUTO, turnLedTo)
function onBtnEvent()
local tt = turntype-1
tt=(tt+1)%4
turntype=tt+1
print(turntype)
if turntype==2 then
tmr.stop(1)
tmr.alarm(1, 100, tmr.ALARM_AUTO, turnLedTo)
elseif turntype==4 then
tmr.stop(1)
tmr.alarm(1, 50, tmr.ALARM_AUTO, turnLedTo)
elseif turntype==3 then
tmr.stop(1)
tmr.alarm(1, 1000, tmr.ALARM_AUTO, turnLedTo)
else
tmr.stop(1)
tmr.alarm(1, 2000, tmr.ALARM_AUTO, turnLedTo)
end
end
gpio.mode(3, gpio.INT, gpio.PULLUP)
gpio.trig(3, "up", onBtnEvent)
--color1 = HSL(100,.5,.5)
--print(color1[0])
--print(color1[1])
--print(color1[2])
ps:我將io口從3画机,4改為2冶伞,4(gpio4,2)色罚,這是因?yàn)閚odemcu上的物理按鍵對(duì)應(yīng)的D3口碰缔,在不外擴(kuò)的情況下可以用來切換彩燈的顯示效果