import time #for time delay
import board #for pin definitions
import digitalio #for pin control
import pwmio
# LEDs Pinout
L = [0,0,0,0]
#Create a list with 4 elements as a "container" to store LED objects later
# To Declare the LEDs and button as PORT
LEDS = (board.GP0, board.GP1, board.GP2, board.GP3)
#Define a tuple containing the specific GP pin numbers for the LEDs.
for i in range(4):
L[i] = digitalio.DigitalInOut(LEDS[i]) #Activate pin as DigitalIO object
L[i].direction = digitalio.Direction.OUTPUT #Set as OUTPUT mode
switch1 = digitalio.DigitalInOut(board.GP18) # Activate GP18 pin as a digital IO object and name it 'switch'
switch1.direction = digitalio.Direction.INPUT # Set as INPUT mode
switch1.pull = digitalio.Pull.DOWN # When switch is closed, it is represent by 0
switch2 = digitalio.DigitalInOut(board.GP19) # Activate GP19 pin as a digital IO object and name it 'switch'
switch2.direction = digitalio.Direction.INPUT
switch2.pull = digitalio.Pull.DOWN
buzzer = pwmio.PWMOut(board.GP15, variable_frequency=True)
# Running Light
def running_light():
for j in range(4):
L[j].value = True #light on
time.sleep(0.2) #remain for 0.2s
L[j].value = False # closed light
for j in reversed(range(4)): #from right to left
L[j].value = True
time.sleep(0.2)
L[j].value = False
# Blink Light
def blink_light():
for j in range(4): #Turn on all 4 LEDs simultaneously
L[j].value = True
time.sleep(0.5)
for j in range(4): #Turn off all 4 LEDs simultaneously
L[j].value = False
time.sleep(0.5)
# OFF the Light
def off_light():
for j in range(4): # Ensure all LEDs are False/OFF
L[j].value = False
# Execution
while True:
if switch1.value == True:
print("Task 1.1")
for bl in range(5):
blink_light() # Blink Light
buzzer.frequency = 1000 # beep frequency
buzzer.duty_cycle = 32768 # sound
time.sleep(0.2) # beep for 0.2s
buzzer.duty_cycle = 0 # stop beep
time.sleep(0.5)
off_light() # OFF Light
elif switch2.value == True :
print("Task 1.2")
for rl in range(5):
running_light() # Running Light
time.sleep(0.5)
off_light() # OFF Light
else:
off_light()
time.sleep(0.5)