Skip to content

Commit

Permalink
Add method blink_all_leds()
Browse files Browse the repository at this point in the history
Add source codes .py
  • Loading branch information
Mick3DIY committed Apr 22, 2024
1 parent 0e806eb commit 9bdf7d7
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.micropico
.vscode
84 changes: 84 additions & 0 deletions EasyPicoPCB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# EasyPicoPCB (2024-04-22)
# Raspberry Pi Pico : https://www.raspberrypi.com/products/raspberry-pi-pico/
# Documentation, tutorials : https://projects.raspberrypi.org
# Book1 : https://hackspace.raspberrypi.com/books/micropython-pico
# MicroPython : https://micropython.org
# Thonny IDE : https://thonny.org
import machine
import utime

# External button S1 (GPIO16, pin 21)
buttonS1 = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN)
# External button S2 (GPIO17, pin 22)
buttonS2 = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_DOWN)
# External button S3 (GPIO18, pin 24)
buttonS3 = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_DOWN)

# External LED LED1 (GPIO15, pin 20)
led1 = machine.Pin(15, machine.Pin.OUT)
# External LED LED2 (GPIO14, pin 19)
led2 = machine.Pin(14, machine.Pin.OUT)
# External LED LED3 (GPIO13, pin 17)
led3 = machine.Pin(13, machine.Pin.OUT)
# Pico on-board LED (GPIO25)
ledOnboard = machine.Pin(25, machine.Pin.OUT)

# Potentiometer P1 (ADC0, GPIO26, pin31)
pot1 = machine.ADC(26)
# Potentiometer P2 (ADC1, GPIO27, pin32)
pot2 = machine.ADC(27)
# Potentiometer P3 (ADC2, GPIO28, pin34)
pot3 = machine.ADC(28)
# 3.3V from Pico (12 bits)
# See page 92, the analogue-to-digital converter in [Book1]
conversionFactor = 3.3 / (65535)

# Handler for buttons IRQ
def buttons_handler(pin):
if buttonS1.value() == 1:
_led1 = led1.value()
if _led1 == 1:
led1.off()
else:
led1.on()
if buttonS2.value() == 1:
_led2 = led2.value()
if _led2 == 1:
led2.off()
else:
led2.on()
if buttonS3.value() == 1:
_led3 = led3.value()
if _led3 == 1:
led3.off()
else:
led3.on()

# Blink LEDs (at startup for example)
def blink_all_leds(all_leds):
for led in all_leds:
# Check if the object is a LED
if hasattr(led, "on"):
led.on()
utime.sleep(0.1)
led.off()

# Buttons triggers
buttonS1.irq(trigger=machine.Pin.IRQ_RISING, handler=buttons_handler)
buttonS2.irq(trigger=machine.Pin.IRQ_RISING, handler=buttons_handler)
buttonS3.irq(trigger=machine.Pin.IRQ_RISING, handler=buttons_handler)

# Check all LEDs
blink_all_leds((led1, led2, led3, ledOnboard))

# Pico Status
ledOnboard.on()

# Show graphics in Thonny plotter with text values
while True:
# https://mkaz.blog/code/python-string-format-cookbook/
valueP1 = "pot1:{:.2f}".format(pot1.read_u16() * conversionFactor)
valueP2 = "pot2:{:.2f}".format(pot2.read_u16() * conversionFactor)
valueP3 = "pot3:{:.2f}".format(pot3.read_u16() * conversionFactor)
print("Min:0", valueP1, valueP2, valueP3, "Max:3.5")
utime.sleep(0.3)
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The Fritzing project [EasyPicoPCB_fritzing.fzz](EasyPicoPCB_fritzing.fzz) is a c
The code with Thonny IDE and its plotter window :

```python
# EasyPicoPCB (2023-10-09)
# EasyPicoPCB (2024-04-22)
# Raspberry Pi Pico : https://www.raspberrypi.com/products/raspberry-pi-pico/
# Documentation, tutorials : https://projects.raspberrypi.org
# Book1 : https://hackspace.raspberrypi.com/books/micropython-pico
Expand Down Expand Up @@ -76,12 +76,24 @@ def buttons_handler(pin):
led3.off()
else:
led3.on()

# Blink LEDs (at startup for example)
def blink_all_leds(all_leds):
for led in all_leds:
# Check if the object is a LED
if hasattr(led, "on"):
led.on()
utime.sleep(0.1)
led.off()

# Buttons triggers
buttonS1.irq(trigger=machine.Pin.IRQ_RISING, handler=buttons_handler)
buttonS2.irq(trigger=machine.Pin.IRQ_RISING, handler=buttons_handler)
buttonS3.irq(trigger=machine.Pin.IRQ_RISING, handler=buttons_handler)

# Check all LEDs
blink_all_leds((led1, led2, led3, ledOnboard))

# Pico Status
ledOnboard.on()

Expand Down
89 changes: 89 additions & 0 deletions joystickXL/EasyPicoPCB_JoystickXL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# EasyPicoPCB code example in CircuitPython and JoystickXL (2024-02-16)
# Original project : https://github.com/Mick3DIY/EasyPicoPCB
# Raspberry Pi Pico : https://www.raspberrypi.com/products/raspberry-pi-pico/
# Documentation, tutorials : https://projects.raspberrypi.org
# CircuitPython : https://learn.adafruit.com/welcome-to-circuitpython
# JoystickXL : https://circuitpython-joystickxl.readthedocs.io
# Thonny IDE : https://thonny.org
import board
import time
import digitalio
import analogio

from joystick_xl.inputs import Axis, Button
from joystick_xl.joystick import Joystick

# External button S1 (GPIO16, pin 21)
buttonS1 = digitalio.DigitalInOut(board.GP16)
buttonS1.direction = digitalio.Direction.INPUT
buttonS1.pull = digitalio.Pull.DOWN
# External button S2 (GPIO17, pin 22)
buttonS2 = digitalio.DigitalInOut(board.GP17)
buttonS2.direction = digitalio.Direction.INPUT
buttonS2.pull = digitalio.Pull.DOWN
# External button S3 (GPIO18, pin 24)
buttonS3 = digitalio.DigitalInOut(board.GP18)
buttonS3.direction = digitalio.Direction.INPUT
buttonS3.pull = digitalio.Pull.DOWN

# External LED LED1 (GPIO15, pin 20)
led1 = digitalio.DigitalInOut(board.GP15)
led1.direction = digitalio.Direction.OUTPUT
# External LED LED2 (GPIO14, pin 19)
led2 = digitalio.DigitalInOut(board.GP14)
led2.direction = digitalio.Direction.OUTPUT
# External LED LED3 (GPIO13, pin 17)
led3 = digitalio.DigitalInOut(board.GP13)
led3.direction = digitalio.Direction.OUTPUT
# Pico on-board LED (GPIO25)
ledOnboard = digitalio.DigitalInOut(board.GP25)
ledOnboard.direction = digitalio.Direction.OUTPUT

# Potentiometer P1 (ADC0, GPIO26, pin31)
pot1 = analogio.AnalogIn(board.GP26)
# Potentiometer P2 (ADC1, GPIO27, pin32)
pot2 = analogio.AnalogIn(board.GP27)
# Potentiometer P3 (ADC2, GPIO28, pin34)
pot3 = analogio.AnalogIn(board.GP28)
# https://docs.circuitpython.org/en/latest/shared-bindings/analogio/index.html#analogio.AnalogIn
conversionFactor = pot1.reference_voltage / (65535)

js = Joystick()

js.add_input(
Button(buttonS1, active_low=False), Button(buttonS2, active_low=False), Button(buttonS3, active_low=False),
Axis(pot1), Axis(pot2), Axis(pot3),
)

# Pico Status
ledOnboard.value = True

# Show axis values or graphic lines in Thonny plotter with text values
while True:
# External button S1
if js.button[0].is_pressed:
led1.value = True
else:
led1.value = False
# External button S2
if js.button[1].is_pressed:
led2.value = True
else:
led2.value = False
# External button S3
if js.button[2].is_pressed:
led3.value = True
else:
led3.value = False

# Values from axis
#print("Min:0", js.axis[0].value, js.axis[1].value, js.axis[2].value, "Max:255")

# https://mkaz.blog/code/python-string-format-cookbook/
valueP1 = "pot1:{:.2f}".format(pot1.value * conversionFactor)
valueP2 = "pot2:{:.2f}".format(pot2.value * conversionFactor)
valueP3 = "pot3:{:.2f}".format(pot3.value * conversionFactor)
print("Min:0", valueP1, valueP2, valueP3, "Max:3.5")

js.update()
time.sleep(0.3) #Important !
2 changes: 1 addition & 1 deletion joystickXL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Then create a new file *code.py* in the Pico board with Thonny IDE with the code
![](assets/EasyPicoPCB_joystickXL_directories.png)

```python
# EasyPicoPCB code example in CircuitPython and JoystickXL
# EasyPicoPCB code example in CircuitPython and JoystickXL (2024-02-16)
# Original project : https://github.com/Mick3DIY/EasyPicoPCB
# Raspberry Pi Pico : https://www.raspberrypi.com/products/raspberry-pi-pico/
# Documentation, tutorials : https://projects.raspberrypi.org
Expand Down

0 comments on commit 9bdf7d7

Please sign in to comment.