5. Embedded Programming¶
This week I worked on learning about programming boards. Specifically I focused on the pi pico and the adat fruit.
Wokwi¶
Wokwi is a really fun site to help you learn micro python. It has a lot of interactive examples.
Example Code from Wokwi¶
Use the three backticks to separate code.
https://wokwi.com/projects/314265138001609280
import time
from neopixel import Neopixel
pixels = Neopixel(17, 0, 6, "GRB")
colors = [
(0xb6, 0xe4, 0x30),
(0x42, 0xd1, 0xe0),
]
pixel_index = 0
color_index = 0
while True:
pixels.set_pixel(pixel_index, colors[color_index])
pixels.show()
#print(pixel_index, colors[color_index])
pixel_index += 1
if pixel_index == 16:
pixel_index = 0
color_index = (color_index + 1) % 2
time.sleep(0.1)
Bellow is the modifications that I made to the code. Basically helping me understand the different setting for the neopixels. Likely I will use neopixels in my final project.
import time
from neopixel import Neopixel
pixels = Neopixel(17, 0, 6, "GRB")
colors = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(0, 255, 255),
(255, 0, 255),
(255, 255, 0),
(255, 255, 255),
(236, 85, 158),
]
#the color for neopixel will accept rgb format
#https://learn.adafruit.com/adafruit-kb2040/neopixel-led
#https://www.w3schools.com/colors/colors_picker.asp?colorhex=E8B5CE (good color picker)
#botton
#https://docs.wokwi.com/parts/wokwi-pushbutton
#https://wokwi.com/projects/312696687190606401
#https://stackoverflow.com/questions/77995955/how-to-code-a-cycle-of-options-for-a-button-in-micropython
pixel_index = 0
color_index = 0
while True:
pixels.set_pixel(pixel_index, colors[color_index])
pixels.show()
#print(pixel_index, colors[color_index])
pixel_index += 1 #This alternates to ever other led
if pixel_index == 16: #this is the number of leds in the strip
pixel_index = 0
#print(len(colors))
color_index = (color_index + 1) % len(colors) #Maybe I could count the array for this. So I was able to have it measure the size of the index so if I add and remove colors that I don´t have to import it here.
time.sleep(0.05) #this decides how fast this goes in the cycle
Chat GPT¶
Me - I need to add a momentary button for on off control to this micro python code. The Pin for the button is 21.
GPT - Sure! Please share your MicroPython code, and I'll modify it to include a momentary button for on/off control using Pin 21.
Me - Gave it the original code that I modified from above.
GPT - I've added a momentary button on Pin 21 to control the on/off state of your NeoPixel cycle. The button toggles the LED animation each time it is pressed. Here's the updated code:
Micropython Button
Changes & Features:
Added a Button on Pin 21:
Configured as an input with an internal pull-up resistor.
Implemented Interrupt for Button Press:
Uses an interrupt (irq) to toggle the running state when the button is pressed.
Includes a debounce delay.
Toggles Animation On/Off:
If running is True, the NeoPixel animation continues.
If running is False, the animation pauses.
Let me know if you need any modifications!
This was not exactly what I was looking for. It started in the on state and it work more as an off button.
Me - Can you make it an on off switch and the default state for the neopixels is off?
GPT - I've modified the code so that the NeoPixels start in an OFF state and act as an on/off switch when the button is pressed. If turned off, the pixels will be cleared. Let me know if you need further tweaks! 🚀
GPT Code -
import time
from neopixel import Neopixel
from machine import Pin
pixels = Neopixel(17, 0, 6, "GRB")
button = Pin(21, Pin.IN, Pin.PULL_UP) # Configure Pin 21 as input with pull-up resistor
colors = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(0, 255, 255),
(255, 0, 255),
(255, 255, 0),
(255, 255, 255),
(236, 85, 158),
]
pixel_index = 0
color_index = 0
running = False # Default state is OFF
def button_pressed(pin):
global running
time.sleep(0.1) # Debounce delay
if pin.value() == 0:
running = not running # Toggle the animation state
if not running:
pixels.clear()
pixels.show()
button.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed) # Interrupt on button press
while True:
if running:
pixels.set_pixel(pixel_index, colors[color_index])
pixels.show()
pixel_index += 1
if pixel_index == 16:
pixel_index = 0
color_index = (color_index + 1) % len(colors)
time.sleep(0.15)
Here is a video of this code running. I need to work on my coding skills and go look at more examples of working with mirco python.
Datasheets¶