"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ LED Blinking Using Raspberry Pi (MicroPython) ┃
┃ ┃
┃ Design and implement in Raspberry Pi 4 a circuit with ┃
┃ 8 LEDs. Make a mechanism that allows the following ┃
┃ patterns: (LED Pattern 1, 2, & 3 in picture shown) ┃
┃ ┃
┃ Copyright (c) 2024 CPE4B - Group 1 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import Pin
from utime import sleep
pins = [28, 27, 26, 22, 21, 20, 19, 18]
leds = [Pin(i, Pin.OUT) for i in pins]
# Pattern 1
def pattern1():
for led in leds:
led.on()
sleep(0.5)
led.off()
for led in reversed(leds):
led.on()
sleep(0.5)
led.off()
# Pattern 2
def pattern2():
red_indices = [0, 2, 4, 6]
blue_indices = [1, 3, 5, 7]
patterns = [
(red_indices, blue_indices),
(blue_indices, red_indices),
(red_indices, blue_indices),
(blue_indices, red_indices),
(red_indices, blue_indices),
(blue_indices, red_indices),
]
for on_indices, off_indices in patterns:
for index in on_indices:
leds[index].on()
sleep(0.5)
for index in on_indices:
leds[index].off()
# Pattern 3
def pattern3():
pattern_steps = [
([0, 7], []),
([1, 6], []),
([2, 5], []),
([0, 7], []),
([3, 4], []),
([], []),
([0, 7], []),
([1, 6], []),
([2, 5], []),
([0, 7], []),
([3, 4], []),
([], []),
]
for on_indices, off_indices in pattern_steps:
for index in on_indices:
leds[index].on()
sleep(0.5)
for index in on_indices:
leds[index].off()
# Main loop
while True:
pattern1()
sleep(1)
pattern2()
sleep(1)
pattern3()
sleep(1)