NodeMCU Projects: Difference between revisions

Content added Content deleted
Line 176: Line 176:
import urandom
import urandom


pin = machine.Pin(14, machine.Pin.OUT)
pin = machine.Pin(14, machine.Pin.OUT) # WS2812 data pin connected to GPIO 14 or Pin D5
np = neopixel.NeoPixel(pin, 16)
np = neopixel.NeoPixel(pin, 16) #16 Pixels or LEDs in strip


def rand():
def rand():
Line 301: Line 301:
bounce()
bounce()
off()
off()
</syntaxhighlight>

= Morse Code =

<syntaxhighlight lang="python">
import machine
import sys
import time

CODE = {'A': '.-', 'B': '-...', 'C': '-.-.',
'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..',
'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-',
'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', ' ': ' ',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.'
}

pin = machine.Pin(14, machine.Pin.OUT) # Laser Module connected to GPIO 14 or Pin D5
pin.off()
a = 300

def morse(x):
c = []
for i in x:
c.append(CODE[i.upper()])
c.append(" ")

for i in range(len(c)):
for j in c[i]:
if j == ".":
pin.on()
print("DOT")
time.sleep_ms(a)
pin.off()
time.sleep_ms(a*2)
elif j == "-":
pin.on()
print("DASH")
time.sleep_ms(a*3)
pin.off()
time.sleep_ms(a*2)
elif j == " ":
pin.off()
print(" ")
time.sleep_ms(a*4)
else:
print("ERROR")
pin.off()
print("END")

morse("amandeep")
</syntaxhighlight>
</syntaxhighlight>