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

1
/*
2
* ------ [UT_08] - millis() function --------
3
*
4
* Explanation: This example shows how to use the millis() function
5
* to perform time-controlled loops
6
*
7
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see .
22
*
23
* Version: 3.0
24
* Design: David Gascon
25
* Implementation: Yuri Carmona
26
*/
27
28
// define variable to store number of milliseconds
29
unsigned long previous;
30
31
// define timeout: 10 seconds
32
unsigned long TIMEOUT = 10000; // ms units
33
34
35
void setup()
36
{
37
USB.ON();
38
USB.println(F("UT_08 example - millis function\n"));
39
40
// get millis() value
41
previous = millis();
42
43
USB.print(F("millis() function returns the execution time (ms units):"));
44
USB.println(previous);
45
USB.println();
46
}
47
48
49
50
void loop()
51
{
52
// 1. get actual time
53
previous = millis();
54
55
USB.print(F("1. Enter while loop with previous = "));
56
USB.println(previous);
57
58
59
// 2. enter while loop for specified TIMEOUT
60
while( (millis() - previous) < TIMEOUT )
61
{
62
// Do whatever...
63
USB.print(F("2. Inside while loop until timeout. millis() = "));
64
USB.println(millis());
65
delay(1000);
66
67
// Check overflow (mandatory sentence)
68
if( millis()
Copied!

Output

1
H#
2
UT_08 example - millis function
3
millis() function returns the execution time (ms units):253
4
1. Enter while loop with previous = 470
5
2. Inside while loop until timeout. millis() = 766
6
2. Inside while loop until timeout. millis() = 1943
7
2. Inside while loop until timeout. millis() = 3120
8
2. Inside while loop until timeout. millis() = 4296
9
2. Inside while loop until timeout. millis() = 5473
10
2. Inside while loop until timeout. millis() = 6650
11
2. Inside while loop until timeout. millis() = 7826
12
2. Inside while loop until timeout. millis() = 9003
13
2. Inside while loop until timeout. millis() = 10180
14
3. Exit while loop
15
1. Enter while loop with previous = 16280
16
2. Inside while loop until timeout. millis() = 16576
17
2. Inside while loop until timeout. millis() = 17753
18
2. Inside while loop until timeout. millis() = 18930
19
2. Inside while loop until timeout. millis() = 20106
20
2. Inside while loop until timeout. millis() = 21283
21
2. Inside while loop until timeout. millis() = 22460
22
2. Inside while loop until timeout. millis() = 23636
23
2. Inside while loop until timeout. millis() = 24813
24
2. Inside while loop until timeout. millis() = 25990
25
3. Exit while loop
26
1. Enter while loop with previous = 32090
27
2. Inside while loop until timeout. millis() = 32386
28
2. Inside while loop until timeout. millis() = 33563
29
2. Inside while loop until timeout. millis() = 34740
30
2. Inside while loop until timeout. millis() = 35916
31
2. Inside while loop until timeout. millis() = 37093
32
2. Inside while loop until timeout. millis() = 38270
33
2. Inside while loop until timeout. millis() = 39446
34
2. Inside while loop until timeout. millis() = 40623
35
2. Inside while loop until timeout. millis() = 41800
36
3. Exit while loop
Copied!
Last modified 1yr ago