NodeMCU Projects: Difference between revisions
Content added Content deleted
Line 58: | Line 58: | ||
else: |
else: |
||
print("Out of Range") |
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> |
</syntaxhighlight> |
||
Revision as of 20:53, 21 January 2018
Analog Temperature Meter
Requirements:
DS18B20 Temperature sensor Servo Motor Micropython based NodeMCU
Wiring details:
Servo motor = D4 => GPIO 2 DS18B20 = D7 => GPIO 13
import time
import machine
import onewire, ds18x20
# the device is on GPIO13
dat = machine.Pin(13)
# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)
# servo is connected to GPIO2
servo = machine.PWM(machine.Pin(2), freq=50)
# Function for mapping range
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
# print all temperatures
while True:
ds.convert_temp()
time.sleep_ms(1000)
for rom in roms:
print(ds.read_temp(rom))
temp = ds.read_temp(rom)
srv = translate(temp, 20, 35, 115, 40)
print(int(srv))
if (temp>=20) & (temp<=35):
servo.duty(int(srv))
else:
print("Out of Range")
NeoPixel
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()
- References
{{#widget:DISQUS
|id=networkm
|uniqid=NodeMCU Projects
|url=https://aman.awiki.org/wiki/NodeMCU_Projects
}}