Hi there
I am new at Arduino and at this forum.
I do not understand completely the Arduino sketch philosophy.
Assume I would need to call 3 functions to run my code, my question is how many Timer.setInterval
functions should I use in void setup()?
Thank for some help.
Thank you Delta_G
What really happens is that I am learning about using Arduino with Blynk apps.
They put something like this in the setup() function:
void setup()
{
// Debug console
Serial1.begin(56700);
delay(5000); // Allow board to settle
Blynk.begin(auth);
// Setup a function to be called every second
timer.setInterval(1000L, sendAmp);
timer.setInterval(1000L, SEmail);
//Blynk.email("Email address ", "High Amps limit reached", "Amps >7.0");
}
void loop()
{
Blynk.run();
timer.run();
}
The function definition for sendAmp() and SEmail() are located before void setup()
But what stuck me is I am setting timer.setInterval with two subsequent arguments.
It seem to me that is not correct.
Thank you very much for your help.
1 Like
The setInterval (or something like it) is only needed for functions that should be run at specific intervals.
loop runs much faster and can call any functions like @Delta_G showed you.
BTW sending EMails each second seems a rather strange idea to me (and probably your provider).
You don't have to use setInterval if you manage the timing yourself.
const unsigned long emailAll = 1000;
const unsigned long ampAll = 1000;
const unsigned long someFuncAll = 60000UL;
unsigned long lastEmail;
unsigned long lastAmp;
unsigned long lastSomeFunc;
void setup() {
Serial1.begin(250000);
Blynk.begin(auth);
}
void loop() {
unsigned long topLoop = millis();
Blynk.run();
if (topLoop - lastAmp >= ampAll) {
lastAmp = topLoop;
sendAmp();
}
if (topLoop - lastEmail >= emailAll) {
lastAmp = topLoop;
SEmail();
}
if (topLoop - lastSomeFunc >= someFuncAll) {
lastSomeFunc = topLoop;
someFunc();
}
}
1 Like
Hi Whandall
Thank you very much for tour clear explanation.
I understood your code and .....where are initialized these variables before void setup() ?:
unsigned long lastEmail;
unsigned long lastAmp;
unsigned long lastSomeFunc;
1 Like
As global variables they are initialized to zero.
1 Like
Thank you very much Whandall I forgot that!!!!
1 Like