-
Hi @luni64, I am trying to create a button latch using the encoderButton example you provide in the repo but having a bit of trouble. I am coming from the bounce2 library over to using the EncoderTool library with the code below. However, when I try to implement with the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Here a straight forward solution: #include "EncoderTool.h"
using namespace EncoderTool;
PolledEncoder enc;
bool btnActive = false;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
enc.begin(0, 1, 2);
}
void loop()
{
enc.tick();
if (enc.buttonChanged() && enc.getButton() == LOW) // assume button = LOW when pressed
{
btnActive = !btnActive;
}
if (btnActive)
{
digitalWriteFast(LED_BUILTIN, HIGH);
}
else
{
digitalWriteFast(LED_BUILTIN, LOW);
}
} You can also use a button callback to get the state change code out of loop: #include "EncoderTool.h"
using namespace EncoderTool;
PolledEncoder enc;
bool btnActive = false;
void cbButton(int32_t state) // will be called whenever the button state changes
{
if(state == LOW) btnActive = !btnActive;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
enc.begin(0, 1, 2); // A=0, B=1, Button=2
enc.attachButtonCallback(cbButton);
}
void loop()
{
enc.tick();
if (btnActive)
{
digitalWriteFast(LED_BUILTIN, HIGH);
}
else
{
digitalWriteFast(LED_BUILTIN, LOW);
}
} |
Beta Was this translation helpful? Give feedback.
Here a straight forward solution:
You can also use a button callback to get the state change code out of loop: