from machine import Pin, PWM, Timer # importing needed parts from the machine libary
import time
import utime
# -----------------------------------------------------------------------------
# DEFINING PINS
# Defining motor controller module pins (L298N H Bridge)
# OUT1 and OUT2 on module (Motor 1)
In1 = Pin(10, Pin.OUT) # IN1
In2 = Pin(11, Pin.OUT) # IN2
EN_A = PWM(Pin(14)) # ENA
# OUT3 and OUT4 On Module (Motor 2)
In3 = Pin(12, Pin.OUT) # IN3
In4 = Pin(13, Pin.OUT) # IN4
EN_B = PWM(Pin(15)) # ENB
# Defining frequencyn of PWM for enable pins
EN_A.freq(1500)
EN_B.freq(1500)
# Defining Relay Pins
Indicator = Pin(6, Pin.OUT) # Relay 1 for Red/Green Indicator
Charge = Pin(7, Pin.OUT) # Relay 2 for Turning On the Prod
Shock = Pin(8, Pin.OUT) # Relay 3 for conecting the end and making live
Spare = Pin(9, Pin.OUT) # curently spare
# Defining the inputs pins
LSSW = Pin(17, Pin.IN, Pin.PULL_DOWN) # Live / Standby Switch
HMBU = Pin(16, Pin.IN, Pin.PULL_DOWN) # Manual prod trigger button
CPSW = Pin(18, Pin.IN, Pin.PULL_DOWN) # pressure switch from prod
# -----------------------------------------------------------------------------
# THIS IS FOR DEBOUNCE OF INPUTS
# -----------------------------------------------------------------------------
# DEFINING FUNCTIONS
# for motor control one pin must be hig and the other low
# swap for the other direction
# if both pins are the same NO movment
# Clockwise movment of BOTH motors
def Run_Clockwise():
In1.high()
In2.low()
In3.low()
In4.high()
# Anticlockwise movment of BOTH motors
def Run_Anticlockwise():
In1.low()
In2.high()
In3.high()
In4.low()
# Compleet Stop Of BOTH Motors
def Stop():
In1.low()
In2.low()
In3.low()
In4.low()
# live/standby switch state
def LS(pin):
if LSSW.value() == 1:
print("Machine LIVE")
time.sleep(1)
if LSSW.value() == 0:
print("Machine In Standby")
time.sleep(1)
# Function to make the (Indicator) output flash
# note if using this with diffrent output all Indocator words must be change to the correct output
def flash_indicator(interval_sec, duration_sec):
# Calculate the number of cycles based on the duration
num_cycles = int(duration_sec / (2 * interval_sec))
# Loop for the specified number of cycles
for _ in range(num_cycles):
# Toggle the Indicator state (on -> off, off -> on)
Indicator.value(not Indicator.value())
# Wait for the specified interval
utime.sleep(interval_sec)
def Armed (pin):
if CPSW.value() == 1:
flash_indicator(0.5, 10)
# Flash the Indicator pin every 0.5 seconds for 10 seconds
Indicator.value(1)
# Keep on
# -----------------------------------------------------------------------------
# CALLING FUNCTIONS
LSSW.irq(LS)
# when LSSW pin changes,interupt the code run(LS) function
CPSW.irq(Armed)
# when CPSW pin changes,interupt the code run(Armed) function