Skip to content

Commit

Permalink
Examples updated to Version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gfvalvo authored Jan 20, 2021
1 parent 016250f commit 91fce93
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions examples/SingleEncoderWithCallback/SingleEncoderWithCallback.ino
Original file line number Diff line number Diff line change
@@ -1,60 +1,72 @@
#include "Arduino.h"
#include "NewEncoder.h"

void ESP_ISR callBack(NewEncoder &enc);
void ESP_ISR callBack(NewEncoder *encPtr, const volatile NewEncoder::EncoderState *state, void *uPtr);

// Pins 2 and 3 should work for many processors, including Uno. See README for meaning of constructor arguments.
// Use FULL_PULSE for encoders that produce one complete quadrature pulse per detnet, such as: https://www.adafruit.com/product/377
// Use HALF_PULSE for endoders that produce one complete quadrature pulse for every two detents, such as: https://www.mouser.com/ProductDetail/alps/ec11e15244g1/?qs=YMSFtX0bdJDiV4LBO61anw==&countrycode=US&currencycode=USD
NewEncoder encoder(2, 3, -20, 20, 0, FULL_PULSE);

volatile int16_t encoderValue;
volatile bool newValue;
volatile int16_t prevEncoderValue;
volatile NewEncoder::EncoderState newState;
volatile bool newValue = false;

void setup() {
int16_t value;
NewEncoder::EncoderState state;

Serial.begin(115200);
delay(2000);
Serial.println(F("Starting"));
Serial.println("Starting");
if (!encoder.begin()) {

Serial.println(F("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting."));
Serial.println("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting.");
while (1) {
yield();
}
} else {
#if defined(__AVR__)
noInterrupts(); // 16-bit access not atomic on 8-bit processor
#endif
encoderValue = encoder;
value = encoderValue;
#if defined(__AVR__)
interrupts();
#endif
Serial.print(F("Encoder Successfully Started at value = "));
Serial.println(value);
encoder.getState(state);
Serial.print("Encoder Successfully Started at value = ");
prevEncoderValue = state.currentValue;
Serial.println(prevEncoderValue);
}
encoder.attachCallback(callBack);
}

void loop() {
int16_t currentEncoderValue;
int16_t currentValue;
NewEncoder::EncoderClick currentClick;

if (newValue) {
noInterrupts();
currentValue = newState.currentValue;
currentClick = newState.currentClick;
newValue = false;
currentEncoderValue = encoderValue;
interrupts();
Serial.print("Encoder: ");
Serial.println(currentEncoderValue);
if (currentValue != prevEncoderValue) {
Serial.println(currentValue);
prevEncoderValue = currentValue;
} else {
switch (currentClick) {
case NewEncoder::UpClick:
Serial.println("at upper limit.");
break;

case NewEncoder::DownClick:
Serial.println("at lower limit.");
break;

default:
break;
}
}
}
}

void ESP_ISR callBack(NewEncoder &enc) {
int16_t e = enc;
if (e != encoderValue) {
encoderValue = e;
newValue = true;
}
void ESP_ISR callBack(NewEncoder *encPtr, const volatile NewEncoder::EncoderState *state, void *uPtr) {
(void) encPtr;
(void) uPtr;
newState.currentValue = state->currentValue;
newState.currentClick = state->currentClick;
newValue = true;
}

0 comments on commit 91fce93

Please sign in to comment.