# Universal pumping system for both dams to intermediate tanks to top IBCs
# Sourced from Panel 3 v3 (on Pico in shed)
# Pumps v3 5/12/25
import machine
import utime
# Pump1 at dam
# Pump2 at house or tractor shed
# Tank is IBC at house or tractor shed
# Location 1 is house system, Location 2 is tractor shed system
# Float switch closed creates value of 0.
# TLSLo Switch is open (value = 1) when tank empty
# TLSHi Switch is open (value = 1) when tank filled
# Pump "On" Green LEDs are fed from the corresponding relay not a PIN. They have a different inline resistor
# to handle the 12v signal (at least for tractor shed)
Location = 2 # Change this parameter for relevant location
prn = True
prn2 = True
watch1 = 0
watch2 = 0
watch3 = 0
if Location == 1:
# House system
if prn: print("House system starting")
P1Start = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP) #Purple wire
P2Start = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP) #Light blue wire
P1P2Start = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) #Pink wire
CycleStop = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP) #Dark Blue wire
WiFiLEDOn = machine.Pin(6, machine.Pin.OUT) #Grey wire
WiFiLEDOff = machine.Pin(7, machine.Pin.OUT) #Orange wire
P1OnLED = machine.Pin(10, machine.Pin.OUT) #Dark Brown wire
P2OnLED = machine.Pin(11, machine.Pin.OUT) #Black wire
P1P2OnLED = machine.Pin(12, machine.Pin.OUT) #Yellow wire
TLSHi = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP) #White wire CHECK THESE DUE TO NEW LIMIT SWITCH ARRAY
TLSLo = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP) #Red wire
Pump1 = machine.Pin(18, machine.Pin.OUT) #Dark Blue wire
Pump2 = machine.Pin(19, machine.Pin.OUT) #Yellow wire
Relay_3 = machine.Pin(20, machine.Pin.OUT) #Orange wire NOT IN UNIVERSAL CODE ?
Relay_4 = machine.Pin(21, machine.Pin.OUT) #Purple wire
else :
# Tractor shed panel
if prn: print("Tractor shed system starting")
TLSHi = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) #Black wire
CycleStop = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP) #White wire
TLSLo = machine.Pin(6, machine.Pin.IN, machine.Pin.PULL_UP) #Light blue wire
P1Start = machine.Pin(10, machine.Pin.IN, machine.Pin.PULL_UP) #Orange wire
P2Start = machine.Pin(11, machine.Pin.IN, machine.Pin.PULL_UP) #Purple wire
P1P2Start = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP) #Pink wire
P1P2OnLED = machine.Pin(13, machine.Pin.OUT) #Grey wire
LowLED = machine.Pin(15, machine.Pin.OUT) #Red LED, Light Blue wire
MidLED = machine.Pin(16, machine.Pin.OUT) #Amber LED, Dark Brown wire
HiLED = machine.Pin(17, machine.Pin.OUT) #Green LED, Black wire
WiFiLEDOn = machine.Pin(18, machine.Pin.OUT) #Orange/white wire
WiFiLEDOff = machine.Pin(19, machine.Pin.OUT) #Blue/white wire
# Output to 4 Module relay
# The relay actuates and onboard LED lights when the input pin is LOW,
# and turns off on HIGH. So relay=0 is relay ON
Pump2 = machine.Pin(27, machine.Pin.OUT) #Shed pump, Dark Blue wire
Pump1 = machine.Pin(28, machine.Pin.OUT) #Dam pump, Yellow wire
# LEDs for Dam and Tank cycle are fed from respective relay, not from a Pin
# End of Pin assignments
i = 1
#stime = 1 #sleep time in seconds
stime = 0.1 # quick for simulation
# Global Variables:
# Can be Read in functions without referencing
# Need to be referenced in a function as global if writing or modifying
def LEDSOff(prn2):
#Turn off all LEDs controlled through Pico pins
global P1P2OnLED,LowLED,MidLED,HiLED,WiFiLEDOn,WiFiLEDOff
P1P2OnLED.value(0)
LowLED.value(0)
MidLED.value(0)
HiLED.value(0)
WiFiLEDOn.value(0)
WiFiLEDOff.value(0)
def LEDSRefresh(prn2):
#Update LEDS to ensure they are not left stuck on
global P1P2OnLED,LowLED,MidLED,HiLED,WiFiLEDOn,WiFiLEDOff
# global P1P2OnLED,LowLED,MidLED,HiLED,WiFiLEDOn,WiFiLEDOff,P12Status,TLSLo,TLSHi
#First turn them all off
LEDSOff(prn2)
if (P12Status==1): P1P2OnLED.value(1)
if (TLSLo.value()==1): LowLED.value(1)
if (TLSLo.value()==0 and TLSHi.value()==1): MidLED.value(1)
if (TLSHi.value()==0): HiLED.value(1)
#WiFiLEDOn.value(0)
#WiFiLEDOff.value(0)
def PumpsOff(prn2):
#Turn off all pump relays
global Pump1,Pump2
Pump1.value(1)
Pump2.value(1)
def Stop(prn2):
# Turn off both pumps, LED and reset Cycle status
global P1Status, P2Status, P12Cycle, P12Status, P1P2OnLED, Pump1, Pump2
P1Status = 0 #Set to OFF
Pump1.value(1) #Turn OFF relay
P2Status = 0 #Set to OFF
Pump2.value(1) #Turn OFF relay
P1P2OnLED.value(0) #Turn off LED
P12Cycle = 0
P12Status = 0
if prn2: print("Cycles reset/stopped")
# Pause to stop button press overflow
utime.sleep(1)
# Set all cycle status to Off and Pumps off
Stop(prn2)
#P1Status = 0 # 0=Off, 1=On
#P2Status = 0
#P12Status = 0
#P12Cycle = 0
#if prn: print("LEDs and Relays off")
if prn: print("Low High P1 P2 Both Stop")
if prn: print(TLSLo.value(),TLSHi.value(),P1Start.value(),P2Start.value(),P1P2Start.value(),CycleStop.value())
while True:
# THIS SHOULD BE ASYNCHRONOS CODE
utime.sleep(stime)
watch1 +=1
if prn: print("Watch 1 is ",watch1)
# Check for cycle start/stop requests. If any cycle has been requested then other cycle requests are ignored
# through variable P12Cycle
if prn: print("P12Cycle is ",P12Cycle)
while P12Cycle == 0:
# While a cycle has not been acknowledge, update LEDS and check each start/stop button
LEDSRefresh(prn2)
i +=1
# if prn: print(i,"P12Cycle is ",P12Cycle)
# Check if both cycle start requested
# if prn: print("P1P2Start = ",P1P2Start.value())
if P1P2Start.value() == 0:
if prn: print("Both cycles requested")
# Cycle to start Pump1, fill tank then start Pump 2 to empty tank requested
# Turn on LED, Remember button has been pressed
# if prn: print("Turning on LED")
P1P2OnLED.value(1)
P12Status = 1 # Set to ON
P12Cycle = 1 # Remember a cycle has been requested
# Check if Pump 1 cycle start requested
if P1Start.value() == 0:
# Cycle to start Pump 1 requested
# Remember button has been pressed
P1Status = 1 #Set to ON
P12Cycle = 1 #Remember a cycle has been requested
# Check if Pump 2 cycle start requested
if P2Start.value() == 0:
# Cycle to start Pump 2 requested
# Remember button has been pressed
P2Status = 1
P12Cycle = 1
# Check if cycle stop requested
if CycleStop.value() == 0:
Stop(prn2)
else:
utime.sleep(stime)
i +=1
# print("i is ",i)
# A button has been remembered, so first while loop is finished
while P1Status == 1:
# Pump 1 (Dam) requested to start
# Turn off Pump 2 to prevent both pumps operating at same time
P2Status = 0 #Set to OFF
Pump2.value(1) #Turn OFF relay
print("Pump 1 requested to operate")
while TLSHi.value() == 1:
# High level float open (down), tank can receive water as not completly filled
# Refresh the LEDs
LEDSRefresh(prn2)
# HiLED.value(0) # Tank not full, ensure LED turned off
# MidLED.value(1) # Tank somewhere above empty turn Mid level LED on
Pump1.value(0) #Turn ON relay
if prn: print(i,"Pump 1 is filling tank, Hi limit is",TLSHi.value())
i +=1
utime.sleep(stime)
# Check if cycle stop request received
if CycleStop.value() == 0:
Stop(prn2)
break
# if prn: print("TLSHi changed to ",TLSHi.value())
# Tank full now, so turn off Pump 1
if prn: print("Tank full now")
P1Status = 0 #Set to OFF
Pump1.value(1) #Turn OFF relay, this will also turn off pump indicator LED
# Refresh LEDs
LEDSRefresh(prn2)
# HiLED.value(1) # Turn on Hi level LED
# MidLED.value(0) # Turn off Mid level LED
# LowLED.value(0) # Turn off Low level LED (just in case)
# Following line was P12Cycle = 0
# If Both cycles previously selected, then set up Pump2 to start automatically
if P12Status==1: P2Status=1
break
# if prn: print("P1Status is ",P1Status)
while P2Status == 1:
# Pump 2 requested to start
# Turn off Pump 1 to prevent both pumps operating at same time
P1Status = 0 #Set to OFF
Pump1.value(1) #Turn OFF relay
if prn: print("Pump 2 requested to operate")
if prn: print("TLSLo is",TLSLo.value())
while TLSLo.value() == 0:
# Low level float closed, water in tank
# LowLED.value(0) # Tank not empty, ensure LED turned off
# MidLED.value(1) # Tank somewhere above empty, turn Mid level LED on
LEDSRefresh(prn2)
Pump2.value(0) # Turn ON relay
if prn: print(i,"Pump 2 is emptying tank")
i +=1
utime.sleep(stime)
# Check if cycle stop request received
if CycleStop.value() == 0:
# Turn off both pumps and LED
Stop(prn2)
break
# Tank must be empty now, so turn off Pump 2 and reset "Both" cycle flag
# if prn: print("Tank empty now")
P2Status = 0 #Set to OFF
Pump2.value(1) #Turn OFF relay
P12Cycle = 0
P12Status = 0
# LowLED.value(1) # Tank empty, turn on Low level LED
# MidLED.value(0) # Turn Mid level LED off
LEDSRefresh(prn2)
print("Ended 2")
break
# print("Ended 2")
print("Ended 1")