Adapting this arduino instructable to ESP32

Hi all,

I am trying to create a project that is similar to the one found here:

https://www.instructables.com/id/Easy-Arduino-Menus-for-Rotary-Encoders/

However, I am using an ESP32, and not an Arduino board.

To get to that far I need to get my Rotary Encoder working with his code: https://www.instructables.com/id/Improved-Arduino-Rotary-Encoder-Reading/

However, I can not compile the code and get an error on "PIND". This line:

reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values

So my question is: Do you have an idea as to how I can adapt the encoder code to work with an ESP32?

Thanks a lot in advance. :)

His complete code:

/*******Interrupt-based Rotary Encoder Sketch*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/

static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
}

void PinA(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void loop(){
  if(oldEncPos != encoderPos) {
    Serial.println(encoderPos);
    oldEncPos = encoderPos;
  }
}

Looking here, you’ll learn about port manipulation. PIND is the value or the PORT D which gathers the values of digital inputs from 0 to 7.

D0=0x01 D1=0x02 D2=0x04 D3=0x08 D4=0x10 D5=0x20 D6=0x40 D7=0x80

0xC is 1100 binary so it reads digital pins 2 and 3.
The instruction reading = PIND & 0xC; computes the AND of the values of pins 0 to 7 and the binary mask 1100. So it gets rid of D0, D1, and D4 to D7, only keeping D2 and D3. Then if D2 is HIGH (i.e. is 1) the AND will return 4, and if D3 is HIGH the AND will return 8. If both are HIGH, the AND will return 8+4 = 12 = 0xC.

So:

  • D2 HIGH, D3 HIGH : reading = 12
  • D2 LOW, D3 HIGH : reading = 8
  • D2 HIGH, D3 LOW: reading = 4
  • D2 LOW, D3 LOW: reading = 0
    This is used to increase the reading speed of input ports in Arduinos, but you’re using an ESP32, which is a Formula 1 compared to those Arduinos. So you can simply read the ports and compute the reading, using ifs for instance
reading = 0;
if (digitalRead(D2)) reading+=4;
if (digitalRead(D3)) reading+=8;

or using a similar mask
reading = (digitalRead(D2) << 2 + digitalRead(D3) << 3) & 0xC;Just try…

Hi lesept!

Thanks a lot for helping me out. I really appreciate it! I have now tried to implement both of your suggestions, but it doesn’t seem to work out for me.

static int pinA = 12; // Our first hardware interrupt pin is digital pin 2
static int pinB = 14; // Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0, PinA, RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1, PinB, RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
}

void PinA() {
  cli(); //stop interrupts happening before we read pin values
  reading = (digitalRead(PinA) << 2 + digitalRead(PinB) << 3) & 0xC;
  if (reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB() {
  cli(); //stop interrupts happening before we read pin values
  reading = (digitalRead(PinA) << 2 + digitalRead(PinB) << 3) & 0xC;
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void loop() {
  if (oldEncPos != encoderPos) {
    Serial.println(encoderPos);
    oldEncPos = encoderPos;
  }
}

When I compile (for ESP32 Thing) I get the following error message on the implementation:

invalid conversion from ‘void (*)()’ to ‘uint8_t {aka unsigned char}’ [-fpermissive]

I just can’t seem to figure this one out.

You have variables and functions with the same names.

digitalRead(PinA) --> digitalRead(pinA) 4 times

Try printing the values of reading and aFlag after the line
  reading = (digitalRead(PinA) << 2 + digitalRead(PinB) << 3) & 0xC;to see if the values are correct and if the problem is not somewhere else…

Bonjour lesept

lesept:
Try printing the values of reading and aFlag after the line

  reading = (digitalRead(PinA) << 2 + digitalRead(PinB) << 3) & 0xC;

to see if the values are correct and if the problem is not somewhere else…

C’est pinA et pinB , pas PinA ou PinB.

Cordialement,
bidouilleelec

Yes, Bidouillelec is right, I should have told you this:

  reading = (digitalRead(pinA) << 2 + digitalRead(pinB) << 3) & 0xC;

My mistake…