Timed loop

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Timed loop

Post by liudr » Tue Apr 10, 2018 5:56 am

If I wanted to run timed loop, say collect data every 60 seconds, and quit if a button is pressed at any time, how do I do it properly so background tasks are not affected by an infinite loop?

On an arduino I would have to check every task I need to do within the loop() function and check the time tick to see if 60 seconds expired or not. If I do the same in micropython, I am afraid that it will waste a lot of cpu resources and don't know how the background routines will be treated, say if I have an infinite loop like this:

Code: Select all

f=open()
start=rtc.now()
while True:
    if now-start<60:
        if button_pressed:
            close data file
            break
    collect data and save to file
Also, if I use interrupt to capture a button push, then I can't act inside the interrupt routine to close file because the foreground routine could be using the file.
Last edited by liudr on Tue Apr 10, 2018 12:52 pm, edited 1 time in total.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Timed look

Post by OutoftheBOTS_ » Tue Apr 10, 2018 8:15 am

If I wanted to run timed loop, say collect data every 60 seconds
I do believe that your using Laboris firmware so it has timer callbacks for this see wiki https://github.com/loboris/MicroPython_ ... wiki/timer
Also, if I use interrupt to capture a button push, then I can't act inside the interrupt routine to close file because the foreground routine could be using the file.
I normally use a global flag where the interrupt will change the flag then in your main program you test the condition of the flag at an appropriate time in your program and take the appropriate action.

Post Reply