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

MicroPython • Re: Code getting stuck after count

$
0
0
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.
You might as well be poking through the entrails of a goat if my experience of AI solutions is anything to go on.

My tip would be start simple, enhance the code incrementally, and start with a design which suits what you want to do.

For a program which does something on a schedule I would go for polling time to see when a second has elapsed, call routines when it has which can determine if they need to run. Something like this where I have reduced the schedule to 1 minutes and every 20 seconds to see it operating more easily. Note times are as the Pico sees things, not necessarily real world local time -

Code:

import timeSECONDS        =  1MINUTES        = 60 * SECONDSHOURS          = 60 * MINUTESTWENTY_SECONDS = 20 * SECONDSONE_MINUTE     =  1 * MINUTESdef Report(msg=""):  t = time.time()  s = (t // SECONDS) % 60  m = (t // MINUTES) % 60  h = (t // HOURS  ) % 24  print("{:02}:{:02}:{:02} {}".format(h, m, s, msg).rstrip())twentySecondsTick = 0def EveryTwentySeconds():  global twentySecondsTick  if twentySecondsTick > 1:    twentySecondsTick -= 1    return  twentySecondsTick = TWENTY_SECONDS  Report("Do the every twenty seconds thing")oneMinuteTick = 0def EveryMinute():  global oneMinuteTick  if oneMinuteTick > 1:    oneMinuteTick -= 1    return  oneMinuteTick = ONE_MINUTE  Report("Do the every minute thing")Report("Started")ticks = time.time()while True:  # Run each possible task  EveryTwentySeconds()  EveryMinute()  # Wait for a new tick  while time.time() <= ticks:    pass  ticks += 1
That wait for new tick should be improved to handle 'time.time()' wraparound but is good enough for proof of concept

Code:

00:33:54 Started00:33:54 Do the every twenty seconds thing00:33:54 Do the every minute thing00:34:14 Do the every twenty seconds thing00:34:34 Do the every twenty seconds thing00:34:54 Do the every twenty seconds thing00:34:54 Do the every minute thing00:35:14 Do the every twenty seconds thing00:35:34 Do the every twenty seconds thing00:35:54 Do the every twenty seconds thing00:35:54 Do the every minute thingetc
I'll put my hands up to having observed the seconds display in that having shown 60 and above when testing. No idea how or why that's happening or how it's even possible. I'm looking into that.

Statistics: Posted by hippy — Sat Aug 03, 2024 1:53 pm



Viewing all articles
Browse latest Browse all 5093

Trending Articles