-
Notifications
You must be signed in to change notification settings - Fork 0
Usage Scenarios
I wanted to have a place to document interesting usage scenarios that come up either in my own experiences or through issues that people might log into this project. A wiki is an excellent place to document such things, keeping a reference to them convenient and concise.
An issue was logged with a request being made to implement a timing function into this library so that if the user had not engaged an encoder for some desired period of time, then some other code would execute, such as code that turns an LED on.
However, adding such code to this library would not be practical nor would it keep the library consistent in its original intent. But there is a way to accomplish this capability very easily.
I wrote another Arduino library called BlockNot which is a library that makes it easy to implement non-blocking timers in your Arduino sketches. Seasoned Arduino programmers know that the delay()
method is one that should almost never be used in a sketch because it completely seizes control over the processor, blocking the sketch from running while it is counting down the time passed into the method argument. Microcontroller programs should never stop executing because they typically involve interaction with multiple different connected hardware and your code should always be able to handle connected hardware without any delays at all.
Enter the non-blocking timer.
I won't go into the details of non-blocking timers here, but you can read my blog post on the topic to get a better understanding of what they are and why we use them.
So, utilizing BlockNot in addition to SimpleEncoder, it becomes easy to have a sketch that does something when the user has not engaged the encoder after some time has passed. We will call this time the timeout
period.
Here is an example sketch that will accomplish the goal:
#include <Arduino.h>
#include <SimpleEncoder.h>
#include <BlockNot.h>
const int BUTTON = 10;
const int PIN_A = 11;
const int PIN_B = 12;
const int LED = 9;
SimpleEncoder encoder(BUTTON, PIN_A, PIN_B);
BlockNot timeout(60, SECONDS);
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
if(encoder.CLOCKWISE) {
timeout.RESET;
digitalWrite(LED, LOW);
//Code to execute if encoder turned clockwise.
}
if(encoder.COUNTERCLOCKWISE) {
timeout.RESET;
digitalWrite(LED, LOW);
//Code to execute if encoder turned counterclockwise.
}
if(encoder.BUTTON_PRESSED) {
timeout.RESET;
digitalWrite(LED, LOW);
//Code to execute if the encoder button is pressed.
}
if(timeout.TRIGGERED) {
digitalWrite(LED, HIGH);
}
}
The sketch is simple; we create our instance of SimpleEncoder with the correct pin assignments. Those pins get set up properly by the library so no need to set them up in the setup()
method. However, it is necessary to set up the pin that controls the LED.
We also create an instance of BlockNot and define it as a 60-second timer which we simply call timeout
in order to make the code easier to read and write.
In the loop(), any action performed with the encoder, whether that action is turning it clockwise, counterclockwise, or even pressing the button, causes the timeout timer to be reset so that it will not trigger until 60 seconds after the last time someone engaged the encoder. It also turns off the LED.
Finally, if nothing has been done with the encoder to trigger any of those if-then statements; after 60 seconds have passed, the LED will turn on.