Skip to content

Commit ac0031e

Browse files
committed
Cycle values and examples improved. New settings - see Multi-select example
1 parent 6823a3f commit ac0031e

File tree

6 files changed

+129
-12
lines changed

6 files changed

+129
-12
lines changed

examples/Esp32RotaryEncoderTestBoundaries/Esp32RotaryEncoderTestBoundaries.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ void setup()
117117
*/
118118
//rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
119119
rotaryEncoder.setAcceleration(250); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
120+
Serial.print("\nPress rotary button to change boundaries ");
120121
}
121122

122123
void loop()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "AiEsp32RotaryEncoder.h"
2+
3+
#if defined(ESP8266)
4+
#define ROTARY_ENCODER_A_PIN D6
5+
#define ROTARY_ENCODER_B_PIN D5
6+
#define ROTARY_ENCODER_BUTTON_PIN D7
7+
#else
8+
#define ROTARY_ENCODER_A_PIN 32
9+
#define ROTARY_ENCODER_B_PIN 21
10+
#define ROTARY_ENCODER_BUTTON_PIN 25
11+
#endif
12+
#define ROTARY_ENCODER_STEPS 4
13+
14+
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
15+
16+
void IRAM_ATTR readEncoderISR()
17+
{
18+
rotaryEncoder.readEncoder_ISR();
19+
}
20+
21+
void setup()
22+
{
23+
Serial.begin(115200);
24+
rotaryEncoder.begin();
25+
rotaryEncoder.setup(readEncoderISR);
26+
rotaryEncoder.setBoundaries(0, 1000, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
27+
rotaryEncoder.setAcceleration(250);
28+
}
29+
30+
void loop()
31+
{
32+
if (rotaryEncoder.encoderChanged())
33+
{
34+
Serial.println(rotaryEncoder.readEncoder());
35+
}
36+
if (rotaryEncoder.isEncoderButtonClicked())
37+
{
38+
Serial.println("button pressed");
39+
}
40+
}

examples/Esp32RotaryEncoderTheShortestExampleNoResistors/Esp32RotaryEncoderTheShortestExampleNoResistors.ino

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#include "AiEsp32RotaryEncoder.h"
22

3-
#define ROTARY_ENCODER_A_PIN 22
4-
#define ROTARY_ENCODER_B_PIN 23
3+
#if defined(ESP8266)
4+
#define ROTARY_ENCODER_A_PIN D6
5+
#define ROTARY_ENCODER_B_PIN D5
6+
#define ROTARY_ENCODER_BUTTON_PIN D7
7+
#else
8+
#define ROTARY_ENCODER_A_PIN 32
9+
#define ROTARY_ENCODER_B_PIN 21
510
#define ROTARY_ENCODER_BUTTON_PIN 25
11+
#endif
612
#define ROTARY_ENCODER_STEPS 4
713
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
814

@@ -13,9 +19,10 @@ void IRAM_ATTR readEncoderISR()
1319

1420
void setup()
1521
{
16-
pinMode(ROTARY_ENCODER_A_PIN, INPUT_PULLUP);
17-
pinMode(ROTARY_ENCODER_B_PIN, INPUT_PULLUP);
22+
// pinMode(ROTARY_ENCODER_A_PIN, INPUT_PULLUP);
23+
// pinMode(ROTARY_ENCODER_B_PIN, INPUT_PULLUP);
1824
Serial.begin(115200);
25+
rotaryEncoder.areEncoderPinsPulldownforEsp32=false;
1926
rotaryEncoder.begin();
2027
rotaryEncoder.setup(readEncoderISR);
2128
rotaryEncoder.setBoundaries(0, 1000, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)

examples/Multi-select/Multi-select.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ At the end select if you want fast delivery for extra 2$
1919
String options[] = {"Select fast food", "How many (1...10)", "Do you want fast delicery for 2$?"};
2020

2121
String foods[] = {"Hot dog", "Pizza", "Hamburger", "Cheeseburger"};
22+
//String foods[] = {" 1", " 2", " 3", " 4"};
2223
String selectedFood = "";
2324

2425
String delivery[] = {"Normal", "Fast for extra 2$"};
@@ -43,7 +44,7 @@ void setForOption(int newOption)
4344
rotaryEncoder.setEncoderValue(1);
4445
break;
4546
case 2:
46-
rotaryEncoder.setBoundaries(0, 1, true); //select delivery
47+
rotaryEncoder.setBoundaries(0, 1, false); //select delivery
4748
rotaryEncoder.setEncoderValue(0);
4849
break;
4950

@@ -111,6 +112,11 @@ void setup()
111112
rotaryEncoder.begin();
112113
rotaryEncoder.setup(readEncoderISR);
113114
rotaryEncoder.setAcceleration(0);
115+
116+
rotaryEncoder.correctionOffset=2; //try with zero or ROTARY_ENCODER_STEPS/2
117+
rotaryEncoder.isButtonPulldown = false;
118+
rotaryEncoder.areEncoderPinsPulldownforEsp32 = true;
119+
114120
setForOption(0);
115121
}
116122

src/AiEsp32RotaryEncoder.cpp

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ void IRAM_ATTR AiEsp32RotaryEncoder::readEncoder_ISR()
3737

3838
if (currentDirection != 0)
3939
{
40+
// bool ignoreCorrection = false;
41+
// if (this->encoder0Pos > this->_maxEncoderValue) ignoreCorrection = true;
42+
// if (this->encoder0Pos < this->_minEncoderValue) ignoreCorrection = true;
4043
long prevRotaryPosition = this->encoder0Pos / this->encoderSteps;
4144
this->encoder0Pos += currentDirection;
4245
long newRotaryPosition = this->encoder0Pos / this->encoderSteps;
@@ -78,11 +81,55 @@ void IRAM_ATTR AiEsp32RotaryEncoder::readEncoder_ISR()
7881
this->lastMovementDirection = currentDirection;
7982
}
8083

84+
//https://github.com/igorantolic/ai-esp32-rotary-encoder/issues/40
85+
/*
86+
when circling there is an issue since encoderSteps is tipically 4
87+
that means 4 changes for a single roary movement (step)
88+
so if maximum is 4 that means _maxEncoderValue is 4*4=16
89+
when we detact 18 we cannot go to zero since next 2 will make it wild
90+
Here we changed to 18 set not to 0 but to -2; 17 to -3...
91+
Now it seems better however that -3 divided with 4 will give -1 which is not regular -> also readEncoder() is changed to give allowed values
92+
It is not yet perfect for cycling options but it is much better than before
93+
94+
optimistic view was that most of the time encoder0Pos values will be near to N*encodersteps
95+
*/
8196
// respect limits
82-
if (this->encoder0Pos > (this->_maxEncoderValue))
83-
this->encoder0Pos = this->_circleValues ? this->_minEncoderValue : this->_maxEncoderValue;
84-
if (this->encoder0Pos < (this->_minEncoderValue))
97+
if ((this->encoder0Pos/ this->encoderSteps) > (this->_maxEncoderValue/ this->encoderSteps)){
98+
// Serial.print("circle values limit HIGH");
99+
// Serial.print(this->encoder0Pos);
100+
//this->encoder0Pos = this->_circleValues ? this->_minEncoderValue : this->_maxEncoderValue;
101+
if (_circleValues){
102+
//if (!ignoreCorrection){
103+
int delta =this->_maxEncoderValue + this->encoderSteps -this->encoder0Pos;
104+
this->encoder0Pos = this->_minEncoderValue-delta;
105+
//}
106+
} else {
107+
this->encoder0Pos = this->_maxEncoderValue;
108+
}
109+
//this->encoder0Pos = this->_circleValues ? (this->_minEncoderValue this->encoder0Pos-this->encoderSteps) : this->_maxEncoderValue;
110+
// Serial.print(" -> ");
111+
// Serial.println(this->encoder0Pos);
112+
} else if ((this->encoder0Pos/ this->encoderSteps) < (this->_minEncoderValue/ this->encoderSteps)){
113+
// Serial.print("circle values limit LOW");
114+
// Serial.print(this->encoder0Pos);
115+
//this->encoder0Pos = this->_circleValues ? this->_maxEncoderValue : this->_minEncoderValue;
85116
this->encoder0Pos = this->_circleValues ? this->_maxEncoderValue : this->_minEncoderValue;
117+
if (_circleValues){
118+
//if (!ignoreCorrection){
119+
int delta =this->_minEncoderValue +this->encoderSteps +this->encoder0Pos;
120+
this->encoder0Pos = this->_maxEncoderValue+delta;
121+
//}
122+
} else {
123+
this->encoder0Pos = this->_minEncoderValue;
124+
}
125+
126+
// Serial.print(" -> ");
127+
// Serial.println(this->encoder0Pos);
128+
}else{
129+
// Serial.print("no circle values limit ");
130+
// Serial.println(this->encoder0Pos);
131+
}
132+
//Serial.println(this->encoder0Pos);
86133
}
87134
}
88135
#if defined(ESP8266)
@@ -146,8 +193,8 @@ AiEsp32RotaryEncoder::AiEsp32RotaryEncoder(uint8_t encoder_APin, uint8_t encoder
146193
pinMode(this->encoderAPin, INPUT_PULLUP);
147194
pinMode(this->encoderBPin, INPUT_PULLUP);
148195
#else
149-
pinMode(this->encoderAPin, INPUT_PULLDOWN);
150-
pinMode(this->encoderBPin, INPUT_PULLDOWN);
196+
pinMode(this->encoderAPin, (areEncoderPinsPulldownforEsp32? INPUT_PULLDOWN:INPUT_PULLUP));
197+
pinMode(this->encoderBPin, (areEncoderPinsPulldownforEsp32? INPUT_PULLDOWN:INPUT_PULLUP));
151198
#endif
152199
}
153200

@@ -161,6 +208,11 @@ void AiEsp32RotaryEncoder::setBoundaries(long minEncoderValue, long maxEncoderVa
161208

162209
long AiEsp32RotaryEncoder::readEncoder()
163210
{
211+
//return (this->encoder0Pos / this->encoderSteps);
212+
if ((this->encoder0Pos/ this->encoderSteps) > (this->_maxEncoderValue/ this->encoderSteps))
213+
return this->_maxEncoderValue/ this->encoderSteps;
214+
if ((this->encoder0Pos/ this->encoderSteps) < (this->_minEncoderValue/ this->encoderSteps))
215+
return this->_minEncoderValue/ this->encoderSteps;
164216
return (this->encoder0Pos / this->encoderSteps);
165217
}
166218

@@ -207,7 +259,13 @@ void AiEsp32RotaryEncoder::begin()
207259
this->previous_butt_state = 0;
208260
if (this->encoderButtonPin >= 0)
209261
{
262+
263+
#if defined(ESP8266)
210264
pinMode(this->encoderButtonPin, INPUT_PULLUP);
265+
#else
266+
pinMode(this->encoderButtonPin,isButtonPulldown?INPUT_PULLDOWN, INPUT_PULLUP);
267+
#endif
268+
211269
}
212270
}
213271

@@ -226,12 +284,14 @@ ButtonState AiEsp32RotaryEncoder::readButtonState()
226284
void AiEsp32RotaryEncoder::reset(long newValue_)
227285
{
228286
newValue_ = newValue_ * this->encoderSteps;
229-
this->encoder0Pos = newValue_;
287+
this->encoder0Pos = newValue_ +this->correctionOffset;
230288
this->lastReadEncoder0Pos = this->encoder0Pos;
231289
if (this->encoder0Pos > this->_maxEncoderValue)
232290
this->encoder0Pos = this->_circleValues ? this->_minEncoderValue : this->_maxEncoderValue;
233291
if (this->encoder0Pos < this->_minEncoderValue)
234292
this->encoder0Pos = this->_circleValues ? this->_maxEncoderValue : this->_minEncoderValue;
293+
294+
this->lastReadEncoder0Pos = this->readEncoder();
235295
}
236296

237297
void AiEsp32RotaryEncoder::enable()

src/AiEsp32RotaryEncoder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class AiEsp32RotaryEncoder
5353
long _minEncoderValue = -1 << 15;
5454
long _maxEncoderValue = 1 << 15;
5555

56-
uint8_t old_AB;
56+
int8_t old_AB;
5757
long lastReadEncoder0Pos;
5858
bool previous_butt_state;
5959

@@ -71,6 +71,9 @@ class AiEsp32RotaryEncoder
7171
int encoderVccPin = AIESP32ROTARYENCODER_DEFAULT_VCC_PIN,
7272
uint8_t encoderSteps = AIESP32ROTARYENCODER_DEFAULT_STEPS);
7373
void setBoundaries(long minValue = -100, long maxValue = 100, bool circleValues = false);
74+
int correctionOffset=2;
75+
bool isButtonPulldown = false;
76+
bool areEncoderPinsPulldownforEsp32 = true;
7477
#if defined(ESP8266)
7578
ICACHE_RAM_ATTR void readEncoder_ISR();
7679
ICACHE_RAM_ATTR void readButton_ISR();

0 commit comments

Comments
 (0)