Skip to content

Button Latch with encoder.buttonChanged() function? #6

Answered by luni64
m-r-m-s asked this question in Q&A
Discussion options

You must be logged in to vote

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;

v…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@m-r-m-s
Comment options

Answer selected by m-r-m-s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants