To create a program will send a streak of light down the LED bar depending on what button is pressed
The Source code can be downloaded here here
#---------------------------------------------------------
# Jonah Watts
# Assignment 2, Project 1
# For CMPT 401
#---------------------------------------------------------
# First import the libraries needed
from machine import Pin, PWM
import time
# Using the given code for power managements from the tutourial and modifying it
class myPWM():
def __init__(self, pwm0: int=15, pwm1: int=2, pwm2: int=0, pwm3: int=4, pwm4: int=5, pwm5: int=18, pwm6: int=19, pwm7: int=21, pwm8: int=22, pwm9: int=23):
self._pwm0=PWM(Pin(pwm0),10000)
self._pwm1=PWM(Pin(pwm1),10000)
self._pwm2=PWM(Pin(pwm2),10000)
self._pwm3=PWM(Pin(pwm3),10000)
self._pwm4=PWM(Pin(pwm4),10000)
self._pwm5=PWM(Pin(pwm5),10000)
self._pwm6=PWM(Pin(pwm6),10000)
self._pwm7=PWM(Pin(pwm7),10000)
self._pwm8=PWM(Pin(pwm8),10000)
self._pwm9=PWM(Pin(pwm9),10000)
def ledcWrite(self,chn,value):
if chn==0:
self._pwm0.duty(value)
elif chn==1:
self._pwm1.duty(value)
elif chn==2:
self._pwm2.duty(value)
elif chn==3:
self._pwm3.duty(value)
elif chn==4:
self._pwm4.duty(value)
elif chn==5:
self._pwm5.duty(value)
elif chn==6:
self._pwm6.duty(value)
elif chn==7:
self._pwm7.duty(value)
elif chn==8:
self._pwm8.duty(value)
elif chn==9:
self._pwm9.duty(value)
def deinit(self):
self._pwm0.deinit()
self._pwm1.deinit()
self._pwm2.deinit()
self._pwm3.deinit()
self._pwm4.deinit()
self._pwm5.deinit()
self._pwm6.deinit()
self._pwm7.deinit()
self._pwm8.deinit()
self._pwm9.deinit()
# Setup the Pins and assign them what led they control
mypwm = myPWM(15,2,0,4,5,18,19,21,22,23)
chns=[0,1,2,3,4,5,6,7,8,9];
dutys=[0,0,0,0,0,0,0,0,0,1023,512,256,128,64,32,16,8,0,0,0,0,0,0,0,0,0];
delayTimes=50
# Define the pins for the buttons
button1 = Pin(12, Pin.IN,Pin.PULL_UP)
button2 = Pin(14, Pin.IN,Pin.PULL_UP)
# If all is correct, try the following loop
try:
while True:
# Try to cycle through the LEDs if there is a button clocked
if not button1.value():
for i in range(0,16):
for j in range(0,10):
mypwm.ledcWrite(chns[j],dutys[i+j])
time.sleep_ms(delayTimes)
if not button2.value():
for i in range(0,16):
for j in range(0,10):
mypwm.ledcWrite(chns[9-j],dutys[i+j])
time.sleep_ms(delayTimes)
except:
mypwm.deinit()
The goal of this assignment is to create a program that will display colors on a neopixel in the order of a rainbow when the button is clicked
The Source code can be downloaded here here
#---------------------------------------------------------
# Jonah Watts
# Assignment 2, Project 2
# For CMPT 401
#---------------------------------------------------------
from machine import Pin,PWM
from random import randint
import neopixel
import time
pin = Pin(5, Pin.OUT)
np = neopixel.NeoPixel(pin, 8) #pin for Neopixel
pins=[15,2,0] #pins for RGB LED
button = Pin(13, Pin.IN,Pin.PULL_UP) #button
current = 0 #current color
#setup the array of rainbow colors in ROYGBIV order
colors=[[255,0,0], #Red
[255,30,0], #Orange
[255,180,0], #Yellow
[0,255,0], #Green
[0,0,255], #Blue
[75,0,130], #Indigo
[138,30,138]] #Violet
#setup the PWM pins for the RGB LED
pwm0=PWM(Pin(pins[0]),10000)
pwm1=PWM(Pin(pins[1]),10000)
pwm2=PWM(Pin(pins[2]),10000)
#to set the color of the RGB LED
def setColor(r,g,b):
pwm0.duty(1023-r)
pwm1.duty(1023-g)
pwm2.duty(1023-b)
try:
while True:
#if the button is pressed change to next color
if not button.value():
red = colors[current][0]
green = colors[current][1]
blue = colors[current][2]
setColor(red,green,blue)
for j in range(0,8):
np[j]=colors[current]
np.write()
time.sleep_ms(50)
current+=1
if current==7:
current=0
except:
pwm0.deinit()
pwm1.deinit()
pwm2.deinit()