# Time_expiring_toggle.py, an example of using the built-in Timer function, to call up
# another function in this case to toggle the LED.
#
# Phil J March 24
from machine import Timer, Pin
LED = Pin(0, Pin.OUT)
LED2 = Pin(22, Pin.OUT)
Sw1 = Pin(14, Pin.IN, Pin.PULL_UP) # so active LOW
Sw2 = Pin(18, Pin.IN, Pin.PULL_UP) # ""
def blink(_): # a parameter is required, so use the '_' with the callback function
if (Sw1.value() == False) and (Sw2.value() == False):
LED.value(0)
LED2.value(0)
if (Sw1.value() == False) and (Sw2.value() == True):
LED.value(1)
LED2.value(0)
if (Sw1.value() == True) and (Sw2.value() == False):
LED.value(0)
LED2.value(1)
if (Sw1.value() == True) and (Sw2.value() == True):
LED.value(1)
LED2.value(1)
'''sets up a hardware timer that will use the callback function 'blink'
that is executed in the background, the other code does not wait for the Timer
it is independant, one instantiated (the object "tm", is created and started)
'''
tm = Timer(period = 500, mode = Timer.PERIODIC, callback = blink)
while True:
name = input("Type 'exit' to quit ? ")
if name == 'exit': # if logically equal, will :-
break # Break out of this loop when 'exit' is entered
tm.deinit() # stops the timer instance
LED.value(0) # if necessary clears the LED
print('bye')