My question is in the title. I am using a Teensy 4.0 to count the pulses from a single-channel (i.e., no quadrature encoding) optical incremental encoder. I am using the example sketch "Basic.pde" provided with the Encoder.h library (see sketch copied below). My encoder is attached to Pin 5 but nothing is attached to Pin 6.
When I run the example code and rotate the encoder slowly by hand, I see numbers 0, 1, 2, or 3 in the Serial monitor, but the count doesn't increase beyond those numbers. I suppose that is because there is no encoder Channel B attached to Pin 6. Hence my question: can I use Encoder.h without the second channel?
Alternatively, if you could point me to an example of an interrupt-driven encoder pulse counter without using the Encoder.h library, then that would be just as well.
Many thanks,
Johann
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
When I run the example code and rotate the encoder slowly by hand, I see numbers 0, 1, 2, or 3 in the Serial monitor, but the count doesn't increase beyond those numbers. I suppose that is because there is no encoder Channel B attached to Pin 6. Hence my question: can I use Encoder.h without the second channel?
Alternatively, if you could point me to an example of an interrupt-driven encoder pulse counter without using the Encoder.h library, then that would be just as well.
Many thanks,
Johann
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}