from machine import Pin, PWM, Timer
InputPin1 = Pin(22, Pin.IN, Pin.PULL_UP)
InputPin2 = Pin(26, Pin.IN, Pin.PULL_UP)
InputPin3 = Pin(19, Pin.IN, Pin.PULL_UP)
InputPin4 = Pin(10, Pin.IN)
OutputPin1 = Pin(15, Pin.OUT)
OutputPin2 = Pin(14, Pin.OUT)
PWM_pin = PWM(Pin(16))
PWM_pin.freq(1400)
led1_state = False
initial_duty = 1000
debounce_timer = Timer()
button_state1 = 0
button_state2 = 0
button_state3 = 0
def motion_detected_handler(pin):
if pin.value():
print("Mvement simulation started")
OutputPin2.on()
else:
print("Movement simulation finished")
OutputPin2.off()
def handle_button1():
global led1_state
led1_state = not led1_state
OutputPin1.value(led1_state)
def handle_button2():
current_duty = PWM_pin.duty_u16()
new_duty = current_duty + 5000
new_duty = min(new_duty, 75535)
PWM_pin.duty_u16(new_duty)
print("Frequency updated to "+ str(new_duty))
def handle_button3():
current_duty = PWM_pin.duty_u16()
new_duty = current_duty - 5000
new_duty = max(new_duty, 0)
PWM_pin.duty_u16(new_duty)
print("Frequency updated to "+ str(new_duty))
def handle_buttons(timer):
global button_state1, button_state2, button_state3
if InputPin1.value()==0:
button_state1 += 1
else:
button_state1 = 0
if InputPin2.value()==0:
button_state2 += 1
else:
button_state2 = 0
if InputPin3.value()==0:
button_state3 += 1
else:
button_state3 = 0
if button_state1 == 4:
handle_button1()
if button_state2 == 4:
handle_button2()
if button_state3 == 4:
handle_button3()
InputPin4.irq(handler=motion_detected_handler, trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING)
debounce_timer.init(freq=40, mode=Timer.PERIODIC, callback=handle_buttons)