from machine import Pin
import time
btn_start = Pin(18,Pin.IN,Pin.PULL_DOWN)
btn_stop = Pin(19,Pin.IN,Pin.PULL_DOWN)
btn_reset = Pin(20,Pin.IN,Pin.PULL_DOWN)
sw_fault = Pin(21,Pin.IN,Pin.PULL_DOWN)
STATE_STOPPED = 0
STATE_RUNNING = 1
STATE_FAULT = 2
current_state = STATE_STOPPED
motor_relay = Pin(16,Pin.OUT)
print("System Ready : Press START to run.")
while True:
if sw_fault.value() == 1:
current_state = STATE_FAULT
if current_state == STATE_FAULT:
motor_relay.value(0)
print("Fault Detected : Motor Locked")
if sw_fault.value() == 0 and btn_reset() == 1:
current_state = STATE_STOPPED
print("System Reset : Ready to Start")
time.sleep(0.5)
elif current_state == STATE_STOPPED:
motor_relay.value(0)
if btn_start.value() == 1:
current_state = STATE_RUNNING
print("Motor Started (State : Running)")
time.sleep(0.3)
elif current_state == STATE_RUNNING:
motor_relay.value(1)
if btn_stop.value() == 1:
current_state = STATE_STOPPED
print("Motor Stopped (State : Stopped)")
time.sleep(0.3)
time.sleep(0.1)