|
| 1 | +# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import board |
| 6 | +import alarm |
| 7 | +from digitalio import DigitalInOut, Direction, Pull |
| 8 | +# setup pins for the traffic light LEDs |
| 9 | +red_light = DigitalInOut(board.A1) |
| 10 | +yellow_light = DigitalInOut(board.SCL1) |
| 11 | +green_light = DigitalInOut(board.A2) |
| 12 | +# array of LEDs |
| 13 | +lights = [red_light, yellow_light, green_light] |
| 14 | +# the traffic light is common anode |
| 15 | +# the pins will need to be pulled down to ground |
| 16 | +# to turn on the LEDs. they are setup as inputs |
| 17 | +# so that the pull can be toggled |
| 18 | +# Pull.UP turns the LEDs off to start |
| 19 | +for light in lights: |
| 20 | + light.direction = Direction.INPUT |
| 21 | + light.pull = Pull.UP |
| 22 | +# button pin setup |
| 23 | +pin_alarm = alarm.pin.PinAlarm(pin=board.SDA1, value=False, pull=True) |
| 24 | +# count to track which light is on |
| 25 | +count = 2 |
| 26 | +# tracks the last light |
| 27 | +last_count = 1 |
| 28 | +while True: |
| 29 | + # increase count by 1, loop through 0-2 |
| 30 | + count = (count+1) % 3 |
| 31 | + # turn off the last LED |
| 32 | + lights[last_count].pull = Pull.UP |
| 33 | + # turn on the current LED |
| 34 | + lights[count].pull = Pull.DOWN |
| 35 | + # print(count) |
| 36 | + # delay to keep count |
| 37 | + time.sleep(1) |
| 38 | + # reset last LED for next loop |
| 39 | + last_count = count |
| 40 | + # go into light sleep mode until button is pressed again |
| 41 | + alarm.light_sleep_until_alarms(pin_alarm) |
0 commit comments