This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2023/06/25 17:38] – created ktokarz | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/27 10:51] (current) – ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Timing ==== | + | ====== Timing ====== |
+ | {{: | ||
+ | Writing code that handles interrupts from internal peripherals, | ||
- | Writing code that handles interrupts that come from internal peripherals, | + | ==== Time-related functions ==== |
+ | Because this chapter presents | ||
- | === Delay === | + | ** Delay **\\ |
- | The simplest solution to make functions work for a certain | + | The simplest solution to make functions work for a particular |
- | //delay()// function | + | The '' |
- | The previously shown blinking LED code is a simple demonstration of delay functionality: | + | The blinking LED code is a simple demonstration of delay functionality: |
<code c> | <code c> | ||
digitalWrite(LED_BUILTIN, | digitalWrite(LED_BUILTIN, | ||
Line 14: | Line 17: | ||
delay(1000); | delay(1000); | ||
</ | </ | ||
+ | Using '' | ||
+ | <figure timers1> | ||
+ | {{ : | ||
+ | < | ||
+ | </ | ||
- | Limitations that come with using // | + | The alternative to using delay is to switch to the non-blocking method, based on timing with the use of '' |
- | === Millis | + | ** Millis |
- | //millis()// (( https:// | + | The '' |
+ | <figure timers2> | ||
+ | {{ : | ||
+ | < | ||
+ | </ | ||
- | Here is an example code of blinking LED using //millis()//. Millis is used as a timer. Every new cycle time is calculated since the last LED state change. If the time passed is equal to or greater than the threshold value the LED is switched: | + | Here is an example code of blinking LED using '' |
<code c> | <code c> | ||
- | //Unsigned long should be used to store time values as the millis() returns 32-bit unsigned number | + | //Unsigned long should be used to store time values |
+ | //as the millis() returns | ||
//Store value of current millis reading | //Store value of current millis reading | ||
+ | |||
unsigned long currentTime = 0; | unsigned long currentTime = 0; | ||
//Store value of time when last time the LED state was switched | //Store value of time when last time the LED state was switched | ||
+ | |||
unsigned long previousTime = 0; | unsigned long previousTime = 0; | ||
- | bool ledState = LOW; //Variable for setting LED state | + | bool ledState = LOW; //Variable for setting LED state |
const int stateChangeTime = 1000; //Time at which switch LED states | const int stateChangeTime = 1000; //Time at which switch LED states | ||
void setup() { | void setup() { | ||
- | pinMode (LED_BUILTIN, | + | pinMode (LED_BUILTIN, |
} | } | ||
void loop() { | void loop() { | ||
- | currentTime = millis(); //Read and store current time | + | currentTime = millis(); |
//Calculate passed time since the last state change | //Calculate passed time since the last state change | ||
Line 44: | Line 59: | ||
if (currentTime - previousTime >= stateChangeTime) { | if (currentTime - previousTime >= stateChangeTime) { | ||
| | ||
- | previousTime = currentTime; | + | previousTime = currentTime; |
- | ledState = !ledState; //Change LED state to oposite | + | ledState = !ledState; |
digitalWrite(LED_BUILTIN, | digitalWrite(LED_BUILTIN, | ||
} | } | ||
Line 51: | Line 66: | ||
</ | </ | ||
- | **Check Yourself** | + | ==== 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. | ||
- | 1. What are the drawbacks of using // | ||
- | * Program execution stops. | ||
- | * Can’t read sensors during // | ||
- | * Simple code. | ||
- | |||
- | 2. What is the difference between //delay()// and // | ||
- | |||
- | 3. Explain why // | ||
- | |