-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
26 deletions.
There are no files selected for viewing
64 changes: 38 additions & 26 deletions
64
examples/SingleEncoderWithCallback/SingleEncoderWithCallback.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¤cycode=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; | ||
} |