This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2023/11/17 18:50] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/27 13:51] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Timing ====== | ||
| + | {{: | ||
| + | Writing code that handles interrupts from internal peripherals, | ||
| + | |||
| + | ==== Time-related functions ==== | ||
| + | Because this chapter presents just an introduction to programming, | ||
| + | |||
| + | ** Delay **\\ | ||
| + | The simplest solution to make functions work for a particular time is to use the '' | ||
| + | The '' | ||
| + | |||
| + | The blinking LED code is a simple demonstration of delay functionality: | ||
| + | <code c> | ||
| + | digitalWrite(LED_BUILTIN, | ||
| + | delay(1000); | ||
| + | digitalWrite(LED_BUILTIN, | ||
| + | delay(1000); | ||
| + | </ | ||
| + | Using '' | ||
| + | <figure timers1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | The alternative to using delay is to switch to the non-blocking method, based on timing with the use of '' | ||
| + | |||
| + | ** Millis **\\ | ||
| + | The '' | ||
| + | <figure timers2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | Here is an example code of blinking LED using '' | ||
| + | |||
| + | <code c> | ||
| + | //Unsigned long should be used to store time values | ||
| + | //as the millis() returns a 32-bit unsigned number | ||
| + | //Store value of current millis reading | ||
| + | |||
| + | unsigned long currentTime = 0; | ||
| + | //Store value of time when last time the LED state was switched | ||
| + | |||
| + | unsigned long previousTime = 0; | ||
| + | |||
| + | bool ledState = LOW; //Variable for setting LED state | ||
| + | |||
| + | const int stateChangeTime = 1000; //Time at which switch LED states | ||
| + | |||
| + | void setup() { | ||
| + | pinMode (LED_BUILTIN, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | currentTime = millis(); | ||
| + | |||
| + | //Calculate passed time since the last state change | ||
| + | //If more time has passed than stateChangeTime, | ||
| + | if (currentTime - previousTime >= stateChangeTime) { | ||
| + | | ||
| + | previousTime = currentTime; | ||
| + | ledState = !ledState; | ||
| + | digitalWrite(LED_BUILTIN, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Sleep Modes ==== | ||
| + | Some IoT-dedicated microcontrollers have special features such as sleep modes that hold program execution for a predefined time or unless an external trigger occurs. This can be used for periodic, time-based activities. Its side effect is energy efficiency. The model of this behaviour and its features are very vendor-specific and vary much: e.g. Espressif MCUs have the only option to restart the code. At the same time, STM32 can hold execution and then continue. Because of the variety of models, modes and features, we do not present here any specific solution but rather a general idea. | ||