from machine import Pin
import time
RELAY_PIN = 15 # relay IN -> pump
STEP_BUTTON_PIN = 14 # step mode: flag the next station
AUTO_BUTTON_PIN = 18 # auto mode: run all stations in one go
RELAY_ACTIVE_HIGH = True # set False for an active-low relay board
PUMP_RUN_MS = 3000 # one small dose (tune once flow rate is calibrated)
MIN_GAP_MS = 10000 # lockout between doses for the same station (step mode)
AUTO_PAUSE_MS = 500 # short pause before each pot in auto mode
CHECK_ORDER = ["1a", "1b", "2b", "2a", "3a", "3b", "4b", "4a"]
LED_PINS = {
"1a": (0, 1),
"1b": (2, 3),
"2b": (4, 5),
"2a": (6, 7),
"3a": (8, 9),
"3b": (10, 11),
"4b": (12, 13),
"4a": (16, 17),
}
#check values based on whether thing need watering
needs_water = {s: False for s in CHECK_ORDER}
last_watered = {s: None for s in CHECK_ORDER}
relay = Pin(RELAY_PIN, Pin.OUT)
status_led = Pin(25, Pin.OUT) #I am in severe brain based pain
step_button = Pin(STEP_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
auto_button = Pin(AUTO_BUTTON_PIN, Pin.IN, Pin.PULL_UP)
leds = {s: (Pin(r, Pin.OUT), Pin(g, Pin.OUT)) for s, (r, g) in LED_PINS.items()}
#FAT LIST 'O FUNCTIONS
def update_leds():
"""Green = needs watering (True), red = fine (False)."""
for s in CHECK_ORDER:
red, green = leds[s]
need = needs_water[s]
green.value(1 if need else 0)
red.value(0 if need else 1)
def pump(on):
relay.value(1 if (on == RELAY_ACTIVE_HIGH) else 0)
status_led.value(1 if on else 0)
def water_station(station):
"""Run one dose for a station, then clear its flag (LED goes red)."""
print("[PUMP] Watering station", station)
pump(True)
try:
time.sleep_ms(PUMP_RUN_MS)
finally:
pump(False)
needs_water[station] = False
last_watered[station] = time.ticks_ms()
update_leds()
print("[PUMP] Done:", station)
def ready(station):
t = last_watered[station]
return t is None or time.ticks_diff(time.ticks_ms(), t) >= MIN_GAP_MS
#FULL AUTO MODE (because its funny)
def run_auto():
"""One click: flag every station, then water each in CHECK_ORDER."""
print("[AUTO] Starting full sweep:", CHECK_ORDER)
for s in CHECK_ORDER: # simulated detector: every pot reports "needs water", this is just so the simulation looks clean
needs_water[s] = True
update_leds() #start red, assumes all need water until proven otherwise
for s in CHECK_ORDER:
time.sleep_ms(AUTO_PAUSE_MS)
if needs_water[s]:
water_station(s) # LED turns red when done,so its false, no need for water
print("[AUTO] Sweep complete")
# ---------------- Test inputs (replace with real detector) ----------------
_prev_step = 1
_prev_auto = 1
_test_index = 0
def poll_buttons():
"""Step button flags the next station; auto button runs the full sweep."""
global _prev_step, _prev_auto, _test_index
step_now = step_button.value()
if _prev_step == 1 and step_now == 0: # falling edge = press
s = CHECK_ORDER[_test_index]
needs_water[s] = True
print("[STEP] Detector flagged", s)
_test_index = (_test_index + 1) % len(CHECK_ORDER)
time.sleep_ms(50) # debounce
_prev_step = step_now
auto_now = auto_button.value()
if _prev_auto == 1 and auto_now == 0:
time.sleep_ms(50) # debounce
run_auto()
# re-read so a press during the sweep isn't treated as a new click
auto_now = auto_button.value()
_prev_auto = auto_now
# ---------------- Main loop ----------------
pump(False)
update_leds()
print("Ready. STEP button flags stations one at a time; AUTO runs all 8.")
print("Order:", CHECK_ORDER)
while True:
poll_buttons()
update_leds()
for s in CHECK_ORDER:
poll_buttons()
update_leds()
if needs_water[s]:
if ready(s):
water_station(s)
else:
print("[SKIP]", s, "watered too recently")
needs_water[s] = False
update_leds()
# False -> already showing red, move on to the next station
time.sleep_ms(50)1a
2a
3a
4a
1b
2b
3b
4b