Make a loop run every minute after an interrupt flag

Hello,
I’ve been working on a project that require me to have a loop run after an interrupt has occured, and that loop needs to run every minute, or any other set time, after the interrupt has happened.
I’ve been struggling with this for some time, but I don’t think I’m using a very good process. I have some code, but it doesn’t really work and so I won’t post it here yet, as I’ll probably will have to use another way of doing it.
The way I tried it was to have a button act as the interrupt which would then change a flag to true, and when that flag is true it would start some kind of timer sequence with the millis() function.

This is for a school project which require the use of interrupts and timers, is it possible to use some timer interrupts?
I am using a ESP32 Dev Module

Thank you

Yes.

If you are stuck, ask your teacher, as they are paid to educate you, we are not.

missdrew:
Yes.

If you are stuck, ask your teacher, as they are paid to educate you, we are not.

Well yes I agree with you, but since we are in corona-times I would need to write emails for help anyway, and why write an email asking for help from just one person when I can just write a forum post and get multiple types of answers.

ruskenfook:
I've been working on a project that require me to have a loop run after an interrupt has occured, and that loop needs to run every minute, or any other set time, after the interrupt has happened.

That seems a strange use of an interrupt. Interrupts are usually used for things where microsecond timing matters - not minutes.

Please provide a good description of your project so we can understand the context of your questions.

Also please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. That way we can focus on the parts you need help with rather than wasting time on things that you can do.

...R

ruskenfook:
Well yes I agree with you, but since we are in corona-times I would need to write emails for help anyway, and why write an email asking for help from just one person when I can just write a forum post and get multiple types of answers.

That's not the sort of attitude that would encourage me to help you.

The reason for asking your teacher is because that is the person best placed to give YOU advice - and paid for doing so.

...R

How you have started sounds OK based on what you have said so far.
Probably make the flag, which you set in the Interrupt service routine, represent four states say:
InterruptDetected, waitLoop, performLoop, waitInterrupt

In the main loop() you test the flag and act accordingly.

InterruptDetected: set a timer by recording the value of millis() then change the state to waitLoop ;
waitLoop: check for expiry of the timer (1 minute). If expired, change state to performLoop.
performLoop: run your defined loop then, when finished, set the state to waitInterrupt.
waitInterrupt: do nothing.

Hint: variables which are set in a interrupt or timer call back routine and used elsewhere should be defined as volatile.

Google also for finite state machine

Robin2:
That seems a strange use of an interrupt. Interrupts are usually used for things where microsecond timing matters - not minutes.

Indeed. We see here from time to time some teacher's ham-fisted exercise set to "teach" the supposed use of interrupts in the most inappropriate way imaginable.

How about you simply post the complete description of the project/ test you have been set so we can see what is involved?

Now of course, if an "Arduino" is specified (and of course, an ESP32 is not an Arduino), you should expect a competent teacher to be monitoring this forum. :grinning: :roll_eyes:

Thats a long interval. Let’s see if it’s even possible!

Timer1 is 16-bit so can count to 65536.

Max divisor is f/1024 so 16,000 ticks per second at 16 MHz, that’s just over 4 seconds to overflow. Now if you reduce the processor clock speed to 1 MHz you get to 67.1 seconds between overflows. That’s more than 1 minute.

So it’s possible but I don’t think this is what your teacher has in mind.

You can indeed have a button trigger an interrupt (not the appropriate way in general to read a button), and then use a millis() based timing loop to delay the reaction to that button press. That makes for a terrible user interface as a user wants an instant reaction to know the button press has been registered. Also there’s the design question: what if the button is pressed again, halfway the delay time. Ignore it? Restart the delay? Something else?

Paul__B:
Indeed. We see here from time to time some teacher's ham-fisted exercise set to "teach" the supposed use of interrupts in the most inappropriate way imaginable.

How about you simply post the complete description of the project/ test you have been set so we can see what is involved?

Now of course, if an "Arduino" is specified (and of course, an ESP32 is not an Arduino), you should expect a competent teacher to be monitoring this forum. :grinning: :roll_eyes:

The project assignment can be just shorted down to simply build something using a ESP32 and an Arduino Uno that require the use of certain functions, like interrupts and timers. So the implementation of interrupts in this way was my solution to checking the interrupt-requirement box.
So what I want to do is I request ephimeris data from NASA Horizons service to get the azimuth and elevation of planets and stuff and then turn some motors to point a model of a hand towards the object. And to do that I connected to the server via telnet on the ESP32 and then I just send a list of commands with client.print. And since objects in space move I need to keep tracking them, so I'll just request the next position with sending another list of commands in a timed interval. I know this might be a very crude solution, but so far it works. I just need it to request data in timed intervals after the first set of commands has been sent.

wvmarle:
Thats a long interval. Let's see if it's even possible!

Timer1 is 16-bit so can count to 65536.

Max divisor is f/1024 so 16,000 ticks per second at 16 MHz, that's just over 4 seconds to overflow. Now if you reduce the processor clock speed to 1 MHz you get to 67.1 seconds between overflows. That's more than 1 minute.

So it's possible but I don't think this is what your teacher has in mind.

You can indeed have a button trigger an interrupt (not the appropriate way in general to read a button), and then use a millis() based timing loop to delay the reaction to that button press. That makes for a terrible user interface as a user wants an instant reaction to know the button press has been registered. Also there's the design question: what if the button is pressed again, halfway the delay time. Ignore it? Restart the delay? Something else?

I used an interrupt to read the button because there were some weird delays occuring after pushing the button if I just had like an if-statement for the button. Your point about if the button is pressed again is a good point. To just ignore the button I could just use like a flag maybe?

Interrupts and timers work VERY different on the ESP32 and the ATmega328p processors… different options, different code, possibly different results even.

ruskenfook:
I used an interrupt to read the button because there were some weird delays occuring after pushing the button if I just had like an if-statement for the button. Your point about if the button is pressed again is a good point. To just ignore the button I could just use like a flag maybe?

That’s possible - it depends on what you want to achieve.
If there’s a long delay after pressing a button that may mean you forgot to add a pull-up resistor (or enable the built-in one).

wvmarle:
Interrupts and timers work VERY different on the ESP32 and the ATmega328p processors... different options, different code, possibly different results even.
That's possible - it depends on what you want to achieve.
If there's a long delay after pressing a button that may mean you forgot to add a pull-up resistor (or enable the built-in one).

So if the button is pressed then I change a variable to say 1 and the function that runs after the button is pressed can only run if the said variable is 0.
Another reason for why I used the interrupt is that I can just press the button once and it the interrupt will then change the flag until it reaches an if-statement, while if I didn't use an interrupt I had to either almost time the button press or hold the button, depending on where it was in the program.

A properly written program can check the button state dozens of times within the time a super fast human can press and release it.

For starters, don't use delay().

ruskenfook:
I've been working on a project that require me to have a loop run after an interrupt has occurred, and that loop needs to run every minute, or any other set time, after the interrupt has happened.

So this implies a complete misunderstanding of the concept of the "loop()" function.

In a properly written program, nothing ever causes to loop() to stop continuously cycling for any length of time more than a few milliseconds for the most intensive calculation. Events do not "cause a loop to run", they allow pieces of the loop to execute conditionally. on "switch" criteria, that is, "if" statements.

Do you plan to get serious and having duly read the instructions for posting (notably point 7) show your attempt at coding?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.