The Polling

Do you remember the second part of the Shrek animated movie? Shrek, Fiona and the donkey went to visit Fiona's parents to The Kingdom of Far Far Away. Do you remember what the donkey did all the time? He kept asking the same question: "Are we there yet?"

This is the very same approach we selected to read the value from the PIR sensor - every call of the loop() function starts by reading the output value from the PIR sensor. This all happens several times per one second. In order to slow down the whole process, the function ends with a short sleep period of 500ms. This ensures that the reading of the output from the PIR sensor occurs only 2 times per second.

Getting the output from the PIR sensor is exactly the same as with the donkey in Shrek - we are constantly asking the same thing. Over and over again. This approach, in the world of computer science, is called polling, because we poll. Its principle is to constantly ask the external device, whether the desired event or change of the state has occurred. Polling is a synchronous operation.

Polling is very simple and is often enough to solve most of the problems. It also has some disadvantages, one of these is the high probability of data loss. This may happen, for example, if the external device sends the data to the microcontroller shortly before it tried to read it. Another case is when there’s a change within the 500 ms sleep period, while the movement will also end within this period.

In the story about the pizza delivery at the beginning of the chapter, the problem may be, for example, if the pizza is delivered before the 60 min interval expires. In general, an approach where operator says that the pizza will arrive exactly after 60 minutes, and if customer won’t open, the goods wouldn’t be delivered (even though it was paid for), doesn’t represent a very good design.

There are, of course, requirements where such behavior is sufficient. The measurement of the outside temperature is a good example, it is measured in periodic (per minutes) regular intervals and the possible temperature fluctuations between two measurements can be considered as negligible.

Thanks to constant queries, the polling requires a larger amount of resources, especially the microcontroller time. Thus, instead of performing the necessary operations, it wastes own resources to constantly check the state of the external device. This can be directly proportional to the energy consumption because the more often the status of the external device is checked, the more energy is consumed.

results matching ""

    No results matching ""