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