Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
I didn't specifically understand what is meant by "since the board began running the current program". Does it start to count as soon as a new sketch is uploaded or does it count only after the loop function starts?
P.S: What i want is to measure a sensor output in specific time intervals (for instance, 1s, 15s, 30s, 1min, 15min, 30min, 1h). But the sensor outputs data only when I request it. I want to start counting after I make a request. Therefore, I need to know the exact functionality of millis()
millis() starts counting from zero a few microseconds after the program is uploaded, or if there is already a program in the Arduino, a few microseconds after power is applied.
I need to know the exact functionality of millis()
All you need to know is that the value returned by millis() will increase by 1 for every millisecond when the program is running. millis() will be doing its stuff long before you are ready to take any sensor readings and you will be doing the timing by noting the millis() value at the start of a timing period and taking a reading when millis() has increased by a certain amount.
You don't actually care what the millis() value is, just that it has changed by a certain amount which means a period of time has elapsed. Done the right way, by using subtraction to do the comparison between start and current values, the calculation will even work when millis() rolls over to zero.
You can use a millis() as a software timer. If that is done right, then you don't need to worry about when millis() starts or when it rolls over, it will always work perfectly fine. It is possible to use a millis for every timing or just one millis() and retrieve the rest from that one millis().
The next code has the 1 second timing with a fixed pace, if there is a large delay in the code, it will generate a lot of those 1 second timings to catch up. Another configuration is that the timing will be delayed by the code.
Can you tell for each of your timing which version that you want ?
This example is just one of a number of good solutions.
// global part
unsigned long previousMillis;
void setup()
{
...
// at the end of setup
previousMillis = millis();
}
void loop()
{
...
if( millis() - previousMillis >= 1000UL) // one second, 'UL' means unsigned long
{
previousMillis += 1000UL;
// this part runs every second
...
static int count15s;
count15s++;
if( count15s >= 15)
{
count15s = 0;
// this part runs every 15 seconds.
...
}
static int count30s;
count30s++;
if( count30s >= 30)
{
count30s = 0;
// this part runs every 30 seconds.
...
}
static int count60s;
count60s++;
if( count60s >= 60)
{
count60s = 0;
// this part runs every minute.
...
static int count15m;
count15m++;
if( count15m >= 15)
{
count15m = 0;
// this part runs every 15 minutes.
...
}
... and so on...
}
}
}
I use this code in a project because I want to do something at a specific time every hour. For example at 37 minutes every hour. You don't need that, so this might not be the best solution for you.