UT 08: millis function
This example shows how to use the millis() function to perform time-controlled loops
Required Materials
1 x Waspmote 1 x MiniUSB wire 1 x Battery
Notes
This example can be executed in Waspmote v12 and Waspmote v15
Code
/*
* ------ [UT_08] - millis() function --------
*
* Explanation: This example shows how to use the millis() function
* to perform time-controlled loops
*
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Version: 3.0
* Design: David Gascon
* Implementation: Yuri Carmona
*/
// define variable to store number of milliseconds
unsigned long previous;
// define timeout: 10 seconds
unsigned long TIMEOUT = 10000; // ms units
void setup()
{
USB.ON();
USB.println(F("UT_08 example - millis function\n"));
// get millis() value
previous = millis();
USB.print(F("millis() function returns the execution time (ms units):"));
USB.println(previous);
USB.println();
}
void loop()
{
// 1. get actual time
previous = millis();
USB.print(F("1. Enter while loop with previous = "));
USB.println(previous);
// 2. enter while loop for specified TIMEOUT
while( (millis() - previous) < TIMEOUT )
{
// Do whatever...
USB.print(F("2. Inside while loop until timeout. millis() = "));
USB.println(millis());
delay(1000);
// Check overflow (mandatory sentence)
if( millis()
Output
H#
UT_08 example - millis function
millis() function returns the execution time (ms units):253
1. Enter while loop with previous = 470
2. Inside while loop until timeout. millis() = 766
2. Inside while loop until timeout. millis() = 1943
2. Inside while loop until timeout. millis() = 3120
2. Inside while loop until timeout. millis() = 4296
2. Inside while loop until timeout. millis() = 5473
2. Inside while loop until timeout. millis() = 6650
2. Inside while loop until timeout. millis() = 7826
2. Inside while loop until timeout. millis() = 9003
2. Inside while loop until timeout. millis() = 10180
3. Exit while loop
1. Enter while loop with previous = 16280
2. Inside while loop until timeout. millis() = 16576
2. Inside while loop until timeout. millis() = 17753
2. Inside while loop until timeout. millis() = 18930
2. Inside while loop until timeout. millis() = 20106
2. Inside while loop until timeout. millis() = 21283
2. Inside while loop until timeout. millis() = 22460
2. Inside while loop until timeout. millis() = 23636
2. Inside while loop until timeout. millis() = 24813
2. Inside while loop until timeout. millis() = 25990
3. Exit while loop
1. Enter while loop with previous = 32090
2. Inside while loop until timeout. millis() = 32386
2. Inside while loop until timeout. millis() = 33563
2. Inside while loop until timeout. millis() = 34740
2. Inside while loop until timeout. millis() = 35916
2. Inside while loop until timeout. millis() = 37093
2. Inside while loop until timeout. millis() = 38270
2. Inside while loop until timeout. millis() = 39446
2. Inside while loop until timeout. millis() = 40623
2. Inside while loop until timeout. millis() = 41800
3. Exit while loop
Last updated