from machine import Pin
from utime import sleep
# Define LED pins connected to GP2, GP3, GP4, GP5, GP6
led_pins = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(4, Pin.OUT)]
# Define segment pins for the 7-segment display
segments = {
'A': Pin(26, Pin.OUT),
'B': Pin(22, Pin.OUT),
'C': Pin(19, Pin.OUT),
'D': Pin(20, Pin.OUT),
'E': Pin(21, Pin.OUT),
'F': Pin(27, Pin.OUT),
'G': Pin(28, Pin.OUT)
}
# Digit to segment mapping for the 7-segment display
digit_to_segments = {
0: ['A', 'B', 'C', 'D', 'E', 'F'],
1: ['B', 'C'],
2: ['A', 'B', 'D', 'E', 'G'],
3: ['A', 'B', 'C', 'D', 'G'],
4: ['B', 'C', 'F', 'G'],
5: ['A', 'C', 'D', 'F', 'G'],
6: ['A', 'C', 'D', 'E', 'F', 'G'],
7: ['A', 'B', 'C'],
8: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
9: ['A', 'B', 'C', 'D', 'F', 'G'],
'-': ['G'] # Corrected to use segment G for the minus sign
}
def light_right_to_left():
"""Light LEDs from right to left, skipping the 4th LED (index 3)."""
for i in range(len(led_pins)-1, -1, -1):
if i == 3: # Skip the 4th LED
continue
led_pins[i].value(1)
sleep(0.5)
led_pins[i].value(0)
def light_left_to_right():
"""Light LEDs from left to right."""
for i in range(len(led_pins)):
led_pins[i].value(1)
sleep(0.5)
led_pins[i].value(0)
def light_custom_pattern():
"""Light LEDs in a custom pattern as specified."""
# Start with 3rd LED (index 2)
led_pins[2].value(1)
sleep(0.5)
led_pins[2].value(0)
# 2nd & 4th LEDs (index 1 and 3)
led_pins[1].value(1)
led_pins[3].value(1)
sleep(0.5)
led_pins[1].value(0)
led_pins[3].value(0)
# 1st & 5th LEDs (index 0 and 4)
led_pins[0].value(1)
led_pins[4].value(1)
sleep(0.5)
led_pins[0].value(0)
led_pins[4].value(0)
# 2nd & 4th LEDs again
led_pins[1].value(1)
led_pins[3].value(1)
sleep(0.5)
led_pins[1].value(0)
led_pins[3].value(0)
# End with 3rd LED
led_pins[2].value(1)
sleep(0.5)
led_pins[2].value(0)
def display_digit(digit):
"""Turn off all segments and then display the given digit."""
# Turn off all segments first
for seg in segments.values():
seg.value(0)
# Turn on the required segments for the given digit
for seg in digit_to_segments.get(digit, []):
segments[seg].value(1)
def display_number(number_str):
"""Display each digit of the number string on the 7-segment display with blinking effect."""
for char in number_str:
if char == '-':
digit = '-'
else:
digit = int(char)
display_digit(digit)
sleep(0.5) # Display each digit for 0.5 seconds
# Turn off all segments to create blinking effect
for seg in segments.values():
seg.value(0)
sleep(0.3) # Blink off for 0.3 seconds
def main():
while True:
# Run LED patterns
light_right_to_left()
sleep(0.5)
light_left_to_right()
light_custom_pattern() # New pattern
sleep(0.5)
# Countdown from 0 to 9 with blink effect
for i in range(10):
display_digit(i)
sleep(0.5)
# Blink off for 0.5 seconds
for seg in segments.values():
seg.value(0)
sleep(0.5)
# Rest for 0.5 seconds before reversing countdown
sleep(0.5)
# Reverse countdown from 9 to 0 with blink effect
for i in range(9, -1, -1):
display_digit(i)
sleep(0.5)
# Blink off for 0.5 seconds
for seg in segments.values():
seg.value(0)
sleep(0.5)
# Rest for 0.5 seconds before displaying the number
sleep(0.5)
# Display the number on the 7-segment display
display_number("20275707-12345678")
sleep(1) # Wait a bit before starting the next cycle
if __name__ == "__main__":
main()