Why do we need this SmallProjects library
SmallProjects library collects all complicated projects to demonstrate the usage of Khoi Hoang's libraries, such as ISR-based timers for ESP8266, ESP32 and Arduino Mega, Nano, etc. These projects are much more complicated than the ordinary libraries' examples. Some can even be used directly in real-life.
ISR-based Fire Smoke Alarm demonstrate how to use ESP8266TimerInterrupt, ESP32TimerInterrupt and TimerInterrupt Library
These are examples how to use, design and convert the code from normal software timer
to ISR-based timer
.
Why do we need this Hardware Timer Interrupt
?
Imagine you have a system with a mission-critical
function, measuring water level and control the sump pump or doing something much more important using extensive GUI, such as medical equipments, security and/or fire-smoke alarm, etc. You normally use a software timer to poll
, or even place the function in loop(). But what if another function is blocking the loop()
or setup()
.
So your function might not be executed, and the result would be disastrous.
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
The correct choice is to use a Hardware Timer with Interrupt
in cooperation with an Input Pin Interrupt
to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis()
or micros()
. That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked
by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
The catch is your function is now part of an ISR (Interrupt Service Routine)
, and must be lean / mean
, and follow certain rules. More to read on:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
-
Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
-
Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.
The design principles are as follows:
- Fire or Smoke measurement and alarm activation are considered
mission-critical
, and must not be interfered or blocked by any other bad tasks,intentionally or unintentionally
. This principle can be applied to any of your projects. Please check the way ISR-based are designed ( very lean and mean ), no delay() and no unnecessary baggage. - The sound alarm is also considered critical. Alarm without sound has no meaning in life-threatening dangerous situation.
- Blynk is considered just a
Graphical-User-Interface (GUI)
. Being connected or not must not interfere with the alarm detection / warning.
Certainly, with Blynk GUI, we can achieve many more great features, such as remote check and control, configurable test case and value
, etc. when possible.
This can be applied in many projects requiring reliable system control, where good, bad, or no connection has no effect on the operation of the system
.
Initial v1.0.0 release of sample codes to demonstrate the usage of ISR-based timers, designed for Arduino (Mega, Nano, UNO, etc.), ESP8266 and ESP32-based boards, by using these Hardware Timers libraries:
- TimerInterrupt for Arduino (Mega, UNO, Nano, etc. ) boards
- ESP8266TimerInterrupt for ESP8266 boards
- ESP32TimerInterrupt for ESP32 boards
Sample codes:
- FireSmokeAlarm
- FireSmokeAlarm_Arduino
- ISR_FireSmokeAlarm
- ISR_FireSmokeAlarm_Arduino
- ISR_Timer_4_Switches
The corresponding codes using Software Timers are also included to help understand the steps taken in order to convert those codes to be ISR-based.
The best and easiest way is to use Arduino Library Manager
. Search for SmallProjects, then select / install the latest version.
You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate to SmallProjects page.
- Download the latest release
SmallProjects-master.zip
. - Extract the zip file to
SmallProjects-master
directory - Copy whole
SmallProjects-master
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install SmallProjects library by using Library Manager. Search for SmallProjects in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
Submit issues to: SmallProjects issues
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under MIT
Copyright 2019- Khoi Hoang