I'm having some trouble with the following code. It runs but gets stuck for hours after the countdown reaches 00:00:01 for some reason.
I've looked through the code and used AI to check but i'm not sure it has fixed.
There is a lot of commented out code and as I am a beginner it is quite messy.
I would also like to add an updating temperature (every 10 mins) in the top right hand side of the screen
below is the code:
any help would be hugely appreciated
I've looked through the code and used AI to check but i'm not sure it has fixed.
There is a lot of commented out code and as I am a beginner it is quite messy.
I would also like to add an updating temperature (every 10 mins) in the top right hand side of the screen
below is the code:
Code:
from machine import ADC, Pin, I2Cimport machineimport timefrom lcd_api import LcdApifrom pico_i2c_lcd import I2cLcdI2C_ADDR = 39I2C_NUM_ROWS = 4I2C_NUM_COLS = 20i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)# Pin numbersfloat_switch_pin = Pin(14, Pin.IN, Pin.PULL_UP) # Pin connected to float switch with pull-up resistorpump_relay_pin = Pin(16, Pin.OUT) # Pin connected to relay#lights_relay_pin = Pin(17, Pin.OUT)#solenoid_relay_pin = Pin(18, Pin.OUT)led_pin = Pin(17, Pin.OUT) # Pin connected to LEDsoil_moisture_pin = ADC(Pin(26)) # Pin connected to soil moisture sensor (ADC pin)sensor_temp = machine. ADC(4)temp_conversion_factor = 3.3 / (65535)#Calibraton valuesmin_moisture=19200max_moisture=50204readDelay = 0.5# Define RGB LED pinsRED_PIN = 10 # Changed pinGREEN_PIN = 11 # Changed pinBLUE_PIN = 12 # Changed pin# Initialize RGB LED pins as outputsred_led = Pin(RED_PIN, Pin.OUT)green_led = Pin(GREEN_PIN, Pin.OUT)blue_led = Pin(BLUE_PIN, Pin.OUT)# Function to check float switch statusdef check_float_switch(): return not float_switch_pin.value() # Inverse logic for float switch# Function to control relaydef control_relay(on=True): pump_relay_pin.value(1 if on else 0) #lights_relay_pin.value(1 if on else 0) #solenoid_relay_pin.value(1 if on else 0)# Function to control LEDdef control_led(on=True): led_pin.value(1 if on else 0)# Function to read soil moisturedef check_soil_moisture(): return soil_moisture_pin.read_u16()# Function to blink RGB LEDdef blink_rgb(color, times): for _ in range(times): color.on() # Turn on the color time.sleep(1) # Wait for 1 second color.off() # Turn off the color time.sleep(1) # Wait for 1 second # Function to fast blink RGB LEDdef rapid_blink_rgb(color, times): for _ in range(times): color.on() time.sleep(0.5) color.off() time.sleep(0.5)def Pb_Switch_INT(pin): # PB_Switch Interrupt handler global Pb_Switch_State # reference the global variable Pb_Switch.irq(handler=None) # Turn off the handler while it is executing if (Pb_Switch.value() == 1) and (Pb_Switch_State == 0): # Pb_Switch is active (High) and Pb_Switch State is currently Low #if (Pb_Switch.value() == 1): # Pb_Switch is active (High) Pb_Switch_State = 1 # Update current state of switch pump_relay_pin.value(0) blue_led.value(1) lcd.move_to(0,4) lcd.putstr(" ") lcd.move_to(0,1) lcd.putstr(" ") lcd.move_to(0,2) lcd.putstr(" ") lcd.move_to(0,4) lcd.putstr("PUMP OVERRIDE ON") print("PUMP OVERRIDE ON") # Do required action here elif (Pb_Switch.value() == 0) and (Pb_Switch_State == 1): # Pb_Switch is not-active (Low) and Pb_Switch State is currently High #elif (Pb_Switch.value() == 0): # Pb_Switch is not-active (Low) Pb_Switch_State = 0 # Update current state of switch pump_relay_pin.value(1) blue_led.value(0) lcd.move_to(0,4) lcd.putstr(" ") lcd.move_to(0,1) lcd.putstr(" ") lcd.move_to(0,2) lcd.putstr(" ") lcd.move_to(0,4) lcd.putstr("PUMP OVERRIDE OFF") print("PUMP OVERRIDE OFF \n") # Do required action here time.sleep(2) lcd.move_to(0,4) lcd.putstr(" ") Pb_Switch.irq(handler=Pb_Switch_INT) #Creat an 'object' for our Pb_Switch change of statePb_Switch = machine.Pin(15,machine.Pin.IN,machine.Pin.PULL_DOWN)#Setup the Interrupt Request Handling for Pb_Switch change of statePb_Switch.irq(trigger=machine.Pin.IRQ_RISING | machine.Pin.IRQ_FALLING, handler=Pb_Switch_INT)#Preset the STATE variable for the Pb_SwitchPb_Switch_State = Pb_Switch.value()print("Pb_Switch State=", Pb_Switch_State)# Calculate total seconds for 4 hourstotal_seconds = 4 * 60 * 60# Initialize variables to keep track of the last displayed timelast_hours = -1last_minutes = -1last_seconds = -1# Print booting message and wait for 5 secondslcd.clear()lcd.move_to(0,4)lcd.putstr("Booting Up.")lcd.move_to(0,2)lcd.putstr("Please Wait...")control_relay(True) # Turn off relayblink_rgb(green_led, 3) # Blink green LED three times during bootingtime.sleep(2) # Wait for 2 seconds after booting#soil_moisture = check_soil_moisture()#print("Soil moisture:", soil_moisture)#time.sleep(2)#soil_moisture = check_soil_moisture()#print("Soil moisture:", soil_moisture)#time.sleep(2)#soil_moisture = check_soil_moisture()#print("Soil moisture:", soil_moisture)#time.sleep(2)#soil_moisture = check_soil_moisture()#print("Soil moisture:", soil_moisture)#time.sleep(2)#soil_moisture = check_soil_moisture()#print("Soil moisture:", soil_moisture)#time.sleep(2)#soil_moisture = check_soil_moisture()#print("Soil moisture:", soil_moisture)#time.sleep(2)# Main loopwhile True: lcd.clear() lcd.move_to(0,4) lcd.putstr("Checking float") lcd.move_to(0,1) lcd.putstr("switch...") time.sleep(1) #print("Checking float switch...") if check_float_switch(): # Water in tank lcd.clear() lcd.move_to(0,4) lcd.putstr("We got water ") lcd.move_to(0,1) lcd.putstr("in the tank folks.") #print("We got water in the tank folks.") rapid_blink_rgb(blue_led, 3)# Blue LED Flash to say water level ok reading = sensor_temp.read_u16() * temp_conversion_factor temperature = 27 - (reading - 0.706)/0.00172 lcd.clear() lcd.move_to(0,4) lcd.putstr("Ambient Temp = ") lcd.move_to(8,2) lcd.putstr("%.2f" % temperature + "*") time.sleep(3) lcd.clear() lcd.move_to(0,4) lcd.putstr("Checking soil ") lcd.move_to(0,1) lcd.putstr("moisture... ") time.sleep(2) #print("Checking soil moisture sensor...") soil_moisture = (max_moisture-soil_moisture_pin.read_u16())*100/ (max_moisture-min_moisture)#= check_soil_moisture() lcd.clear() lcd.move_to(0,4) lcd.putstr("Soil Moisture:" + "%.2f" % soil_moisture + "%" ) lcd.move_to(0,1) lcd.putstr("(adc: "+str(soil_moisture_pin.read_u16())+")") time.sleep(5) #print("soil_moisture: " + "%.2f" % soil_moisture +"% (adc: "+str(soil_moisture_pin.read_u16())+")") if soil_moisture < 80: # Soil is dry lcd.clear() lcd.move_to(0,4) lcd.putstr("Soil is dry!") lcd.move_to(0,1) lcd.putstr("Plants are thirsty!") lcd.move_to(0,2) lcd.putstr("Activating Pump") time.sleep(2) lcd.clear() control_relay(False) # Turn on relay blue_led.value(1) # Blue LED on for count in range(5, 0, -1): lcd.move_to(0,4) lcd.putstr("Activating Pump") lcd.move_to(0,3) lcd.putstr("Watering for: %d" % count) lcd.move_to(15,3); if (count < 10): lcd.move_to(15,3) lcd.putstr(' '); # alternatively: '0' time.sleep(1) #print("Soil is dry. Plants are thirsty! Activating Saviour Pump for 30 seconds...") blue_led.value(0)# Blue LED off control_relay(True) # Turn off relay lcd.clear() lcd.move_to(0,4) lcd.putstr("Pumping Complete") lcd.move_to(0,1) lcd.putstr("Happy Seedlings!") lcd.move_to(0,2) lcd.putstr("See you in 4!") time.sleep(2) #print("Pump has done it's business. The seedling's thank you! See you in 8 hours...") # Countdown timer lcd.clear() for seconds_remaining in range(total_seconds, 0, -1): hours = seconds_remaining // 3600 minutes = (seconds_remaining % 3600) // 60 seconds = seconds_remaining % 60 lcd.putstr("See you in:\n") lcd.move_to(12,3) lcd.putstr("%02d\n" % hours,) lcd.move_to(14,3) lcd.putstr(":%02d\n" % minutes) lcd.move_to(17,3) lcd.putstr(":%02d" % seconds) time.sleep(1) time.sleep(4 * 60 * 60) else: lcd.clear() lcd.move_to(0,4) lcd.putstr("Soil is Moist.") lcd.move_to(0,1) lcd.putstr("Happy Seedlings!") lcd.move_to(0,2) lcd.putstr("See you in 4!") time.sleep(2) for seconds_remaining in range(total_seconds, 0, -1): hours = seconds_remaining // 3600 minutes = (seconds_remaining % 3600) // 60 seconds = seconds_remaining % 60 lcd.move_to(0,3) lcd.putstr("See you in:\n") lcd.move_to(12,3) lcd.putstr("%02d\n" % hours,) lcd.move_to(14,3) lcd.putstr(":%02d\n" % minutes) lcd.move_to(17,3) lcd.putstr(":%02d" % seconds) time.sleep(1) #print("Soil is moist. Plant's are happy, see you in 8 hours...") blink_rgb(green_led,10) time.sleep(4 * 60 * 60) else: # No water in tank lcd.clear() lcd.move_to(0,4) lcd.putstr("Tank Empty") lcd.move_to(0,1) lcd.putstr("The end is neigh!!!") lcd.move_to(0,2) lcd.putstr("Alert and wait 4hrs") #print("No water in the tank. We're all going to die!!!! Triggering LED and waiting 8 hours...") control_led(True) # Turn on LED blink_rgb(red_led, 5) # Flash red LED twice time.sleep(2) for seconds_remaining in range(total_seconds, 0, -1): hours = seconds_remaining // 3600 minutes = (seconds_remaining % 3600) // 60 seconds = seconds_remaining % 60 lcd.move_to(0,3) lcd.putstr("See you in:\n") lcd.move_to(12,3) lcd.putstr("%02d\n" % hours,) lcd.move_to(14,3) lcd.putstr(":%02d\n" % minutes) lcd.move_to(17,3) lcd.putstr(":%02d" % seconds) time.sleep(1) control_led(False) # Turn off LED time.sleep(4 * 60 * 60) ########################################################################################Setup the Interrupt Request Handling for Pb_Switch change of statePb_Switch.irq(trigger=machine.Pin.IRQ_RISING, handler=Pb_Switch_INT)Pb_Switch.irq(trigger=machine.Pin.IRQ_FALLING, handler=Pb_Switch_INT)Pb_Switch.irq(trigger=machine.Pin.IRQ_RISING | machine.Pin.IRQ_FALLING, handler=Pb_Switch_INT)
any help would be hugely appreciated
Statistics: Posted by sattynav — Fri Aug 02, 2024 11:08 am