Jump to content

NodeMCU Projects: Difference between revisions

Line 58:
else:
print("Out of Range")
</syntaxhighlight>
 
= NeoPixel =
 
<syntaxhighlight lang="python">
import machine
import neopixel
import time
 
pin = machine.Pin(14, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, 16)
 
def cycle():
while True:
for i in range(16):
np[i] = (255,0,0)
np.write()
time.sleep_ms(25)
 
for i in range(16):
np[i] = (0,255,0)
np.write()
time.sleep_ms(25)
 
for i in range(16):
np[i] = (0,0,255)
np.write()
time.sleep_ms(25)
i += 10
 
def circle(x,y,z):
for i in range(16):
np[i] = (x,y,z)
np.write()
time.sleep_ms(25)
 
def demo(np):
n = np.n
 
# cycle
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 0)
np[i % n] = (255, 255, 255)
np.write()
time.sleep_ms(25)
 
# bounce
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 128)
if (i // n) % 2 == 0:
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
time.sleep_ms(60)
 
# fade in/out
for i in range(0, 4 * 256, 8):
for j in range(n):
if (i // 256) % 2 == 0:
val = i & 0xff
else:
val = 255 - (i & 0xff)
np[j] = (val, 0, 0)
np.write()
 
# clear
for i in range(n):
np[i] = (0, 0, 0)
np.write()
 
# Under Construction
def rainbow():
pass
 
def off():
for i in range(16):
np[i] = (0,0,0)
np.write()
 
off()
</syntaxhighlight>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.