Currently, the time in between each delay is 1 second. I want to change it to either 30 seconds, 1 minute or the maximum possible. How do I do it?
Do I change it like this?
so my project is a battery management system and it has 5 sensors measuring data.
My void loop has calculations to get other data. After I get the data I publish it to google sheet.
I was using a delay to slow down the publishing of data until my teacher told me about the interupt.
So I want to use interrupt to slow down the publishing of data to google sheets while not slowing down the measuring of data in the sensors
As a beginner, it is [b]incredibly unlikely[/b] that interrupts will be useful to you.
A common "newbie" misunderstanding is that an interrupt is a mechanism for altering the flow of a program - to execute an alternate function. Nothing could be further from the truth!
An interrupt is a mechanism for performing an action which can be executed in "no time at all" with an urgency that it must be performed immediately or else data - information - will be lost or some harm will occur. It then returns to the main task without disturbing that task in any way though the main task may well check at the appropriate point for a "flag" set by the interrupt.
Now these criteria are in a microprocessor time scale - microseconds. This must not be confused with a human time scale of tens or hundreds of milliseconds or indeed, a couple of seconds. A switch operation is in this latter category and even a mechanical operation perhaps several milliseconds; the period of a 6000 RPM shaft rotation is ten milliseconds. Sending messages to a video terminal is clearly in no way urgent,
Unless it is a very complex procedure, you would expect the loop() to cycle many times per millisecond. If it does not, there is most likely an error in code planning; while the delay() function is provided for testing purposes, its action goes strictly against effective programming methods. The loop() will be successively testing a number of contingencies as to whether each requires action, only one of which may be whether a particular timing criteria has expired. Unless an action must be executed in the order of mere microseconds, it will be handled in the loop().
So what sort of actions do require such immediate attention? Well, generally those which result from the computer hardware itself, such as high speed transfer of data in UARTs(, USARTs) or disk controllers.
An alternate use of interrupts, for context switching in RTOSs, is rarely relevant to this category of microprocessors as it is more efficient to write cooperative code as described above.
Well I would need my sensors to constantly measure the data while only wanting to send data to be printed on Google sheets every 30 secs.
So for this do you suggest I don't use interrupt ( quit new to interrupt) or just use delay ?
Don't use delay! Use millis() instead to time the publishing of the data. It can be as simple as this:
const unsigned long publishMs = 30000;
void setup()
{
}
void loop()
{
static unsigned long prevMs = millis();
unsigned long currMs = millis();
if (curMs - prevMs >= publishMs)
{
// PUBLISH DATA HERE!!!
prevMs = currMs;
}
// do all of your other stuff
}
The simple story on "delay()" is that it is not usable in serious applications. (Why? Because it does nothing! )
You have posted some random code about interrupts. You later gave a vague description of what you actually wanted to do. Have you actually generated any code so far? If so, we can perhaps discuss it.
What you need to do is to organise your project. You need to define a number of short "chunks" that will each take very little time and code each, then you can determine how to string them together with a simple scheduling system. In particular, you need to break up your "calculations" and "publishing" into workable sections so that they can be interspersed with things that you need to do on time. Then you can use timing with the clock (millis()) to get those time-critical parts done when needed and get the other things done in between.
All a matter of organisation, not a matter of trying to stick interrupts in to make up for disorganisation.