You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
maybe you can update your code like this. I copied from RotaryEncoder::_button_ISR() and changed to micros()
void ARDUINO_ISR_ATTR RotaryEncoder::_encoder_ISR()
{
/** * Almost all of this came from a blog post by Garry on GarrysBlog.com: * https://garrysblog.com/2021/03/20/reliably-debouncing-rotary-encoders-with-arduino-and-esp32/ * * Read more about how this works here: * https://www.best-microcontroller-projects.com/rotary-encoder.html*/staticuint8_t _previousAB = 3;
staticint8_t _encoderPosition = 0;
staticunsignedlong _lastInterruptTime = 0;
staticlong _stepValue;
bool valueChanged = false;
// Simple software de-bounceif( ( micros() - _lastInterruptTime ) < 100 )
return;
_previousAB <<= 2; // Remember previous stateif( digitalRead( encoderPinA ) ) _previousAB |= 0x02; // Add current state of pin Aif( digitalRead( encoderPinB ) ) _previousAB |= 0x01; // Add current state of pin B
_encoderPosition += encoderStates[( _previousAB & 0x0f )];
/** * Based on how fast the encoder is being turned, we can apply an acceleration factor*/unsignedlong speed = micros() - _lastInterruptTime;
if( speed > 40000 ) // Greater than 40 milliseconds
_stepValue = this->stepValue; // Increase/decrease by 1 x stepValueelseif( speed > 20000 ) // Greater than 20 milliseconds
_stepValue = ( this->stepValue <= 9 ) ? // Increase/decrease by 3 x stepValuethis->stepValue : ( this->stepValue * 3 ) // But only if stepValue > 9
;
else// Faster than 20 milliseconds
_stepValue = ( this->stepValue <= 100 ) ? // Increase/decrease by 10 x stepValuethis->stepValue : ( this->stepValue * 10 ) // But only if stepValue > 100
;
/** * Update counter if encoder has rotated a full detent * For the following comments, we'll assume it's 4 steps per detent * The tripping point is `STEPS - 1` (so, 3 in this example)*/if( _encoderPosition > encoderTripPoint ) // Four steps forward
{
this->currentValue += _stepValue;
valueChanged = true;
}
elseif( _encoderPosition < -encoderTripPoint ) // Four steps backwards
{
this->currentValue -= _stepValue;
valueChanged = true;
}
if( valueChanged )
{
encoderChangedFlag = true;
// Reset our "step counter"
_encoderPosition = 0;
// Remember current time so we can calculate speed
_lastInterruptTime = micros();
}
}
The text was updated successfully, but these errors were encountered:
--- ESP32RotaryEncoder.cpp 2025-01-23 08:49:43+++ ESP32RotaryEncoder-changed.cpp 2025-01-23 08:56:51@@ -328,11 +328,14 @@
static int8_t _encoderPosition = 0;
static unsigned long _lastInterruptTime = 0;
static long _stepValue;
-
bool valueChanged = false;
- _previousAB <<=2; // Remember previous state+ // Simple software de-bounce+ if( ( micros() - _lastInterruptTime ) < 100 )+ return;+ _previousAB <<= 2; // Remember previous state+
if( digitalRead( encoderPinA ) ) _previousAB |= 0x02; // Add current state of pin A
if( digitalRead( encoderPinB ) ) _previousAB |= 0x01; // Add current state of pin B
\ No newline at end of file
@@ -343,12 +346,12 @@
* Based on how fast the encoder is being turned, we can apply an acceleration factor
*/
- unsigned long speed = millis() - _lastInterruptTime;+ unsigned long speed = micros() - _lastInterruptTime;- if( speed > 40 ) // Greater than 40 milliseconds+ if( speed > 40000 ) // Greater than 40 milliseconds
_stepValue = this->stepValue; // Increase/decrease by 1 x stepValue
- else if( speed > 20 ) // Greater than 20 milliseconds+ else if( speed > 20000 ) // Greater than 20 milliseconds
_stepValue = ( this->stepValue <= 9 ) ? // Increase/decrease by 3 x stepValue
this->stepValue : ( this->stepValue * 3 ) // But only if stepValue > 9
;
\ No newline at end of file
@@ -384,6 +387,6 @@
_encoderPosition = 0;
// Remember current time so we can calculate speed
- _lastInterruptTime = millis();+ _lastInterruptTime = micros();
}
}
\ No newline at end of file
...appears to be reasonable enough. I'll commit this change after I've tested.
Hi, i had a problem with bouncy china knob's
maybe you can update your code like this. I copied from RotaryEncoder::_button_ISR() and changed to micros()
The text was updated successfully, but these errors were encountered: