Inputs with Events
The preceding program will work well, but it is an example of polling. To monitor the state of the button, the switch continuously reads the value from the input port, meaning that the program is running at all times. This uses considerable processor time and may result in the flashlight battery draining before it has even been switched on.
A much better way to manage inputs of this kind is to use interrupts. An interrupt is initiated by a change in the state of a port. Special circuitry that generates a signal that causes the processor to pause the currently executing program and run code in an interrupt handler detects this change. Once the interrupt has been dealt with, the program will resume execution.
In conventional embedded ...
Get Embedded Programming with the Microsoft® .NET Micro Framework now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.