Different time settings under the same loop

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
tusabez
Posts: 9
Joined: Sun Jul 25, 2021 10:00 am

Different time settings under the same loop

Post by tusabez » Sun Jul 25, 2021 10:23 am

Hello all,
I'm very new at using Python/MicroPython and currently have a Raspberry Pi Pico connected to a 3 channel relay. This will be used as a indoor plant watering system with an LED lamp. The code I pieced together is set to cycle every 12 hours checking for dryness before turning on the water pump. I have a DS3231 clock set and code to tell the lamp when to turn on or off. Unfortunately the lamp is also under the same loop so it won't check to see if it needs to be turned on or off until the next cycle. I was hoping to figure out a way to separate the lamp from the rest of the code so if can turn on and off at specific times. The way I have it set now is as a window of times for the lamp to turn on and off. As far as I understand, everything needs to be on a loop to repeat the process indefinitely. Any help or suggestions would be appreciated.

Code: Select all

# Import libraries
import time, utime
from machine import Pin, I2C
from ds3231_i2c import DS3231_I2C

# Set DS I2C ID, SDA, SCL respective pins and uses default frequency (freq=400000)
ds_i2c = I2C(0,sda=Pin(16), scl=Pin(17))
ds = DS3231_I2C(ds_i2c)
# Uncomment the two lines below to set time        0   1   2     3                              4    5    6    
#current_time = b'\x00\x11\x13\x07\x24\x07\x21' # sec\min\hour\week(Sunday = 1 - Saturday = 7)\day\month\year
#ds.set_time(current_time)
# Once time is set, you can delete or recomment the 2 lines above.
# Define the name of week days list. Uncomment if you want to change what days the External LED turns on. Otherwise the current code will turn on every day
#w  = ["Sunday","Monday","Tuesday","Wednesday","Thurday","Friday","Saturday"];

# Set GP25 onboard LED output
led = Pin(25, Pin.OUT)
# Set GP22 as moisture sensor input
moisture = Pin(22,Pin.IN)
# Set GP1 as moisture sensor power relay 2 output
moisturepower = Pin(1,Pin.OUT)
# Set GP0 for water pump relay 1 output
pump = Pin(0,Pin.OUT)
# Set GP2 for external LED relay 3 output
extled = Pin(2, Pin.OUT)
# Interval time in minutes and Watering time in seconds
interval = 720
water = 10

while True:
    led(True)
    t = ds.read_time()
    # Replace numbers inside green quotation marks with military time to set External LED on and off times
    hour_on = int("7", 16)
    minute_on = int("00", 16)
    hour_off = int("19", 16)
    minute_off = int("00", 16)
    extled(False)
    if (t[2]) >= hour_on and (t[2]) < hour_off:
        extled(True)
    elif (t[2]) >= hour_off and (t[2]) < hour_on:
        extled(False)
    moisturepower(True)
    time.sleep(1)
    if moisture.value() == 1:
        pump(True)
        time.sleep(water)
        pump(False)
    moisturepower(False)
   #Wait for interval period, flashing LED every 30 seconds
    count_AA = 0
    while count_AA < (interval * 2):
        count_BB = 0
        while count_BB < 5:
            led(True)
            time.sleep(0.5)
            led(False)
            time.sleep(0.5)
            count_BB = count_BB + 1
        time.sleep(25)
        count_AA = count_AA + 1

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Different time settings under the same loop

Post by Roberthh » Sun Jul 25, 2021 10:31 am

You can set up timers which call at regular intervals a function for doing what should done. Set the class machine.Timer.
https://docs.micropython.org/en/latest/ ... ne%20timer
These timer have to set up once and works completely independent from other tasks.

tusabez
Posts: 9
Joined: Sun Jul 25, 2021 10:00 am

Re: Different time settings under the same loop

Post by tusabez » Sun Jul 25, 2021 11:33 am

Roberthh wrote:
Sun Jul 25, 2021 10:31 am
You can set up timers which call at regular intervals a function for doing what should done. Set the class machine.Timer.
https://docs.micropython.org/en/latest/ ... ne%20timer
These timer have to set up once and works completely independent from other tasks.
Thanks for the link. I'm having trouble understanding how to implement into my code. From what I read, I need to define and initiate as a periodic timer? For example, if I want to place the following code for a pump and sensor into a timer, how would I group it together to follow one 12 hour timer? Would the timer be placed outside of the While True loop? I just started learning to code a couple of weeks ago so all of this is still new to me. Thanks again.

Code: Select all

moisturepower(True)
    time.sleep(1)
    if moisture.value() == 1:
        pump(True)
        time.sleep(water)
        pump(False)
    moisturepower(False)

tusabez
Posts: 9
Joined: Sun Jul 25, 2021 10:00 am

Re: Different time settings under the same loop

Post by tusabez » Sun Jul 25, 2021 12:00 pm

Ah I think I figured it out. I added this to the code and it seems to work :) Thanks again for pointing me in the right direction.

Code: Select all

tim = Timer()
m = 60
h = 60*m
downtime = m
def tick(timer):
    moisturepower(True)
    time.sleep(1)
    if moisture.value() == 1:
        pump(True)
        time.sleep(water)
        pump(False)
    moisturepower(False)
tim.init(freq=1/downtime, mode=Timer.PERIODIC, callback=tick)

tusabez
Posts: 9
Joined: Sun Jul 25, 2021 10:00 am

Re: Different time settings under the same loop

Post by tusabez » Sun Jul 25, 2021 1:56 pm

I have one additional question. How can I set up a second timer? I tried to see if I could create another one but it seems like I'm not doing it correctly by typing this. I get an error message.

Code: Select all

tim1 = Timer(1)
tim2 = Timer(2)
def tick(timer):
    moisturepower(True)
    time.sleep(1)
    if moisture.value() == 1:
        pump(True)
        time.sleep(water)
        pump(False)
    moisturepower(False)
tim1.init(freq=1/downtime, mode=Timer.PERIODIC, callback=tick)
def LED(timer):
    led(True)
    time.sleep(5)
    led(False)
tim2.init(freq=1/30, mode=Timer.PERIODIC, callback=LED)  
Traceback (most recent call last):
  File "<stdin>", line 29, in <module>
ValueError: Timer doesn't exist

tusabez
Posts: 9
Joined: Sun Jul 25, 2021 10:00 am

Re: Different time settings under the same loop

Post by tusabez » Sun Jul 25, 2021 2:28 pm

Apparently I really need to play around with the code before I post anything :lol: I just cleared timers back to Timer() and that did the trick.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Different time settings under the same loop

Post by pythoncoder » Tue Jul 27, 2021 3:30 pm

An alternative to using timers is schedule which uses a similar approach to Unix cron.
Peter Hinch
Index to my micropython libraries.

Post Reply