Sept. 25, 2023
Connect two buttons, so pressing one will gradually and temporarily light up the LED bar from from left to right and right to left, respectively.
Component | Amount |
---|---|
ESP32-S3-WROOM | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
LED Bar Graph | 1 |
Wire M-M | 12 |
Big Push Button | 2 |
Resistor - 220 Ω | 8 |
Resistor - 10K Ω | 4 |
from machine import Pin,PWM
from pwm import myPWM
import time
button_a = Pin(46, Pin.IN,Pin.PULL_UP) # set up buttons
button_b = Pin(3, Pin.IN,Pin.PULL_UP)
mypwm = myPWM(21,47,38,39,40,41,42,2) # set up pwm system
chns = [0,1,2,3,4,5,6,7]
dutys = [0,0,0,0,0,0,0,0,1023,512,256,128,64,32,16,8,0,0,0,0,0,0,0,0]
delayTimes = 50
light_active = false
def grad_lighting(bool lr): # actualize the lighting
for i in range(0,16):
for j in range(0,8):
if not lr: # for going left-to-right
mypwm.ledcWrite(chns[j],dutys[i+j])
else: # for going right-to-left
mypwm.ledcWrite(chns[7-j],dutys[i+j])
time.sleep_ms(delayTimes)
while True:
if not light_active:
if not button_a.value(): # button A pressed
time.sleep_ms(20) # account for bounce
if not button_a.value(): # if button A still pressed
light_active = true
grad_lighting(True) # turn LEDs on
light_active = false
if not button_b.value(): # button B pressed
time.sleep_ms(20) # account for bounce
if not button_b.value(): # if button B still pressed
light_active = true
grad_lighting(False) # turn LEDs on
light_active = false
Create a system that iterates through the “roygbiv” colours and displays them on an RGB LED and the 8RGB Module when a button is clicked.
Component | Amount |
---|---|
ESP32-S3-WROOM | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
RGB LED | 1 |
8 RGB LED Module | 1 |
Push Button | 2 |
Wire M-M | 6 |
Wire M-F | 3 |
Resistor - 220 Ω | 3 |
Resistor - 10k Ω | 2 |
from machine import Pin, PWM
import neopixel
import time
LEDpins = [2,42,41] # note RGB LED locations
pwm0 = PWM(Pin(LEDpins[0]),1000) # connect them with a PWM system
pwm1 = PWM(Pin(LEDpins[1]),1000)
pwm2 = PWM(Pin(LEDpins[2]),1000)
NEOpin = Pin(3, Pin.OUT) # initialize the NEO-Pixel
np = neopixel.NeoPixel(NEOpin, 8)
button = Pin(46, Pin.IN,Pin.PULL_UP)
colours = [[255,0,0], # red
[255,30,0], # orange
[255,100,0], # yellow
[0,255,0], # green
[0,0,255], # blue
[20,0,40], # indigo
[90,10,100]] # violet
count = 0
def setLEDColour(r,g,b): # set RGB LED light
red = 1023-(r*4)
green = 1023-(g*4)
blue = 1023-(b*4)
pwm0.duty(red)
pwm1.duty(green)
pwm2.duty(blue)
def setNEOColour(c): # set Neopixel light
for i in range(0,8):
np[i] = c
np.write()
time.sleep_ms(30)
def roygbiv():
setLEDColour(colours[count][0],colours[count][1],colours[count][2])
setNEOColour(colours[count])
try:
while True:
if not button.value(): # button pressed
time.sleep_ms(20) # account for bounce
if not button.value(): # if button still pressed
roygbiv() # make colour change
count+=1
if count == len(colours):
count=0
while not button.value():
time.sleep_ms(20)
except:
pwm0.deinit()
pwm1.deinit()
pwm2.deinit()