Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5725

MicroPython • Re: Code getting stuck after count

$
0
0
Thank you so much,
You have three countdown timers. Each for the same 4 hours.
Let me just clarify that there should only be one count-down, and they are all else/if statements based on the decision tree below:

Is there water in the tank?

If no wait four hours and restart loop

if yes, does the soil need watering?

If no wait four hours and restart loop

if yes water, then wait four hours and restart loop.
Maybe it is doing what you asked. But what you asked is not what you actually wanted...
So let me check;

What I want is that it checks each of those and then counts down 4 hours until it checks again. How would I change the code to make that happen?

Again, thank you so much for this

I'd forget about doing the wait in your code. Instead I'd use the following pesudo code called from cron every four hours.

Code:

if tank contains water and soil needs watering:water itexit
Let cron handle the timing for you. It'll simplify your code significantly. Because cron will start a new instance of your code each time, you crash recovery for free. With the delay in your code you need some mechansim external to it to cope with that.

As for your code, judging by lines 289 - 303

Code:

        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)
You're actually sleeping for eight hours. Four hours in the loop while updating the LCD then an additional four hours after the loop completes (line 303).

A couple of other points:
  • On line 126 you "Calculate total seconds for 4 hours". On several other lines (e.g. line 303) you calculate it again rather than using the stored value.
  • Your four (eight) hour intervals will increase from four hours over time. You're using a set interval and not accounting for the time it takes to execute the code prior to entering the sleep.
  • As you're using the for loop I included above several times it will simplfy maintaining your code significantly if you move it into a function.
  • Lines 305 - 309 will never be executed. They need to be moved to before the main loop.
[shameless self promotion]
In case you need help with cron: Cron – A Beginner's Guide
[/shameless self promotion]


Edited to strikeout wrong information and incorrect suggestions

Statistics: Posted by thagrol — Fri Aug 02, 2024 4:43 pm



Viewing all articles
Browse latest Browse all 5725

Trending Articles