gfvalvo is such a modest guy ;-))
I modified your code to use the NewEncoder-library which can be downloaded from here
This library is much more reliable than the encoder.h
use the download-button and install in the Arduino-IDE with Sketch - include library Add-zip-library
So here is your code modified to use NewEncoder
two changes:
you need to call the begin-function in setup
newencoder delivers its value without .read
you can define min/max limits
//#include <Encoder.h>
//Encoder myEnc(13, 12);
#include "NewEncoder.h"
NewEncoder myEnc(12, 13, -10, 10, 0, FULL_PULSE);
int bt_up = false;
int bt_down = false;
long oldPosition = -999;
const int BUTTON_PIN = 15; //Encoder pushbutton pin
const int SHORT_PRESS_TIME = 850;
const int LONG_PRESS_TIME = 850;
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
//#include
void PrintFileNameDateTime()
{
Serial.println("Code running comes from file ");
Serial.println(__FILE__);
Serial.print(" compiled ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.println(__TIME__);
}
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod )
{
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
PrintFileNameDateTime();
if (!myEnc.begin()) {
Serial.println(F("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting."));
while (1) {
}
}
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 500);
if ( TimePeriodIsOver(MyTestTimer, 50) ) {
//delay(100);
//long newPosition = myEnc.read() / 4;
long newPosition = myEnc;
if (newPosition > oldPosition) {
oldPosition = newPosition;
bt_up = true;
}
if (newPosition < oldPosition) {
oldPosition = newPosition;
bt_down = true;
}
Serial.println(newPosition);
//Serial.println(myEnc);
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW) { // button is pressed
pressedTime = millis();
isPressing = true;
isLongDetected = false;
} else if (lastState == LOW && currentState == HIGH) { // button is released
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
}
if (isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if ( pressDuration > LONG_PRESS_TIME ) {
isLongDetected = true;
}
}
// save the the last state
lastState = currentState;
}
}
I added my standard utils PrintFileNameDateTime, IfTimePeriodIsoVer and HearBeatBlink
but you can boild down your code to this version
/* SingleEncoder.cpp
Created on: Jul 25, 2018
Author: GFV
*/
#include "Arduino.h"
#include "NewEncoder.h"
// 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.adafruit.com/product/377
// NewEncoder encoder(IO_Pin_A, IO_Pin_B, MinValue, MaxValue, InitialValue, HALF_PULSE); //HALF_PULSE FULL_PULSE
NewEncoder encoder(12, 13, -10, 10, 0, FULL_PULSE);
int16_t prevEncoderValue;
void setup() {
int16_t value;
Serial.begin(115200);
//delay(2000);
Serial.println(F("Starting"));
if (!encoder.begin()) {
Serial.println(F("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting."));
while (1) {
}
} else {
value = encoder;
Serial.print(F("Encoder Successfully Started at value = "));
Serial.println(value);
}
}
void loop() {
int16_t currentValue;
bool up, down;
up = encoder.upClick();
down = encoder.downClick();
if (up || down) {
currentValue = encoder;
if (currentValue != prevEncoderValue) {
Serial.print(F("Encoder: "));
Serial.println(currentValue);
prevEncoderValue = currentValue;
} else if (up) {
Serial.println(F("Encoder at upper limit."));
} else {
Serial.println(F("Encoder at lower limit."));
}
}
}
best regards Stefan
Edit: I tested it with a KY40-encoder