Would like to add one more 74165 for some buttons but have yet to dig into the library to see if that option exists.
Added support for encoder buttons in v2.1.0. They are debounced by the Bounce2 library. Unfortunately the Bounce2 library included in Teensyduino is a bit outdated. You need to install the latest Bounce2 (>=2.53) via the library manager to get it working. The buttons work with all polled encoders. I.e. the multiplexed encoders and the PolledEncoder class. I didn't implement button support for the interrupt based Encoder class since that would make debouncing difficult. If you are using the interrupt based encoder it is best to handle the buttons manually with Bounce2
Here a usage example for the PolledEncoder class also showing the use of the new valueChanged and buttonChanged functions:
Code:
using namespace EncoderTool;
PolledEncoder enc;
void setup()
{
constexpr int pinA = 0, pinB = 1, pinBtn = 2;
enc.begin(pinA, pinB, pinBtn);
}
void loop()
{
enc.tick();
if (enc.valueChanged()) { Serial.printf("value: %d\n", enc.getValue()); }
if (enc.buttonChanged()) { Serial.printf("button: %s\n", enc.getButton() == LOW ? "pressed" : "released"); }
}
And here an example using the 74165 multiplexer and displaying button state and encoder value with callbacks:
Code:
#include "EncoderTool.h"
using namespace EncoderTool;
constexpr unsigned encoderCount = 8; // number of attached encoders (daisy chain shift regesters for more than 8)
constexpr unsigned QH_A = 0; //output pin QH of shift register B
constexpr unsigned QH_B = 1; //output pin QH of shift register A
constexpr unsigned QH_C = 2; //output pin QH of shift register C (buttons, optional)
constexpr unsigned pinLD = 3; //load pin for all shift registers)
constexpr unsigned pinCLK = 4; //clock pin for all shift registers
//74165 datasheet: http://www.ti.com/product/SN74HC165
EncPlex74165 encoders(encoderCount, pinLD, pinCLK, QH_A, QH_B, QH_C);
void buttonChanged(int state)
{
Serial.printf("button: %s\n", state == LOW ? "pressed" : "released");
}
void anyValueChanged(int idx, int value, int delta)
{
Serial.printf("Enc_%d: value: %d delta:%d\n", idx, value, delta);
}
void setup()
{
encoders.begin(CountMode::quarterInv);
// Attach callback invoked when any encoder value
encoders.attachCallback(anyValueChanged);
// setup encoder[0] for limited count rate and a button callback
encoders[0].setLimits(0, 10);
encoders[0].attachButtonCallback(buttonChanged);
}
void loop()
{
encoders.tick();
}