import time
from machine import Pin, ADC, enable_irq, disable_irq
import utime
NUM_OF_LEDS = 8
LED_PINS = [1, 2, 3, 4, 6, 8, 9, 10] # GPIO pin numbers
BUTTON_PINS = {'ButtonL': 5, 'ButtonR': 21} # GPIO pin numbers
POT_PIN = 26
class Leds:
"""A class to manage all LED setup and functions"""
def __init__(self):
# Initializes LEDs as digitalio leds
self.leds = [Pin(pin, Pin.OUT) for pin in LED_PINS]
def leds_states(self, binary_str):
# Sets LED values based on a binary_str having on/off states
for i, led in enumerate(self.leds):
led.value(int(binary_str[i]))
def leds_denitialize(self):
# Deinitializes LEDs and frees up pins
for led in self.leds:
led.deinit()
def ByteDisplay(self, decimal_number):
"""Takes a decimal number and shows binary representation on LEDs"""
binary_num = bin(decimal_number) # Convert decimal to binary and pad with zeros to 8 bits
binary_str = "0" * (8 - len(binary_num[2:])) + binary_num[2:]
self.leds_states(binary_str)
def Directed_Volta(self, N, speed, direction):
"""Walks the LED light for a number of rounds 'N', at given 'speed'
either from left to right or right to left"""
led_vs = [0, 0, 0, 0, 0, 0, 0, 0]
for i in range(N):
if direction.lower() == 'left':
for led in reversed(range(NUM_OF_LEDS)):
led_vs[led] = 1
self.leds_states(led_vs)
utime.sleep(speed)
led_vs[led] = 0
elif direction.lower() == 'right':
for led in range(NUM_OF_LEDS):
led_vs[7-led] = 1
self.leds_states(led_vs)
utime.sleep(speed)
led_vs[7-led] = 0
utime.sleep(0.1)
def rapid_counter(self, s):
"""Rapidly counts from 0-255 then restarts from 0;
If buttonL is pressed, counting is slower;
If buttonR is pressed, counting is faster"""
speed = s
counter = 0
flagger = False
button_debounce = {ButtonL: 0, ButtonR: 0}
def speed_change(pin):
"""Speed change function called from callback used to change the speed of the counting"""
nonlocal speed, flagger
if pin == ButtonL:
speed = 0.5 * speed # Double the speed if ButtonL pressed
print("Speed increased to", speed)
elif pin == ButtonR:
if time.ticks_ms() - button_debounce[pin] < 800: # checking double click within 800ms
flagger = not flagger # Toggle the flag to stop the count
print("Double press detected. Counting stopped." if flagger else "Counting resumed.")
speed = 2 * speed # Half the speed if ButtonR pressed
print("Speed decreased to", speed)
def callback(pin):
"""handler function for buttons interrupt"""
nonlocal button_debounce
current_time = time.ticks_ms()
if (current_time - button_debounce[pin]) > 500: # for debounce set at 500ms
speed_change(pin) # Handle speed change
button_debounce[pin] = current_time
ButtonR.irq(trigger=Pin.IRQ_FALLING, handler=callback)
ButtonL.irq(trigger=Pin.IRQ_RISING, handler=callback)
print("Starting speed is: ", speed)
while not flagger:
self.ByteDisplay(counter)
counter += 1
if counter == 256:
counter = 0
time.sleep(speed)
self.ByteDisplay(0)
def LEDPong():
"""1 player 1D pong. Left side of LEDs is a wall and right side is the player.
As ball aproaches player, POT has to be turned to send the ball back.
Points depend on how fast the ball returns which depends on how fast POT is turned."""
ball_speed = 1.5
player_score = 0
while player_score >= 0:
pass
# configuring buttonR as pull down, GP21 input
ButtonR = Pin(BUTTON_PINS['ButtonR'], Pin.IN, Pin.PULL_DOWN)
# configuring buttonL as pull UP, GP5 input
ButtonL = Pin(BUTTON_PINS['ButtonL'], Pin.IN, Pin.PULL_UP)
# configuring potentiometer
potentiometer = ADC(Pin(POT_PIN))
print("Welcome to Pi Pico W Lab 1 !! \n")
session = Leds()
# Loop for user input and mode selection
while True:
session.Directed_Volta(1, 0.1, "Left" )
utime.sleep(0.1)