This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:programming_fundamentals_rtu:timing [2018/01/31 10:19] – Agrisnik | en:iot-open:programming_fundamentals_rtu:timing [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Timing ====== | ====== Timing ====== | ||
| - | There are two functions for the use of timing in the code of Arduino - //delay()// and // | + | There are two functions for the use of timing in the code of Arduino - //delay()// and // |
| - | ===== Delay() ===== | + | ===== delay() ===== |
| The //delay()// function pauses the execution of a program for a determined amount of time that is defined by milliseconds in the brackets. The down side of the delay() function is that none of the other program functions can execute while the time defined in the function is not passed. | The //delay()// function pauses the execution of a program for a determined amount of time that is defined by milliseconds in the brackets. The down side of the delay() function is that none of the other program functions can execute while the time defined in the function is not passed. | ||
| - | The Delay() | + | The syntax of the function |
| + | <code c> | ||
| + | delay(ms) | ||
| + | </code> | ||
| - | ===== Millis() ===== | + | The parameter |
| - | The //millis()// function works a bit differently, | + | The function |
| - | This function also has the version of using microseconds instead of milliseconds - // | + | The example code: |
| + | <code c> | ||
| + | int ledPin = 13; // LED connected to digital pin 13 | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | pinMode(ledPin, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | digitalWrite(ledPin, | ||
| + | delay(1000); | ||
| + | digitalWrite(ledPin, | ||
| + | delay(1000); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The delay() function also has the version that is using microseconds instead of milliseconds and it is called - // | ||
| + | |||
| + | ===== millis() ===== | ||
| + | The // | ||
| + | |||
| + | The syntax of the function is following: | ||
| + | <code c> | ||
| + | time = millis() | ||
| + | </ | ||
| + | |||
| + | This function does not have the input parameters in brackets. | ||
| + | The return value of the function is the number of milliseconds that has passed since the program was started. The type is //unsigned long//. | ||
| + | |||
| + | The example of the function in the code: | ||
| + | <code c> | ||
| + | int period = 1000; //the interval | ||
| + | unsigned long time_now = 0; //variable that stores the last timing value | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | if(millis() > time_now + period){ //testing, if the time that has passed exceeds 1000 milliseconds (1 second) | ||
| + | time_now = millis(); | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | //Some other code that executes each time the loop is run | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | This function also has the version of using microseconds instead of milliseconds - // | ||