Rotary Encoder - error while compiling 'Pio' does not name a type

Hi

Refer to this topic :
[SOLVED] Rotary Encoder slow update

There is a code:

const uint8_t pinA = 4;
const uint8_t pinB = 5;

int counter = 0;

Pio* portA = g_APinDescription[pinA].pPort;
Pio* portB = g_APinDescription[pinB].pPort;
const uint32_t maskA = g_APinDescription[pinA].ulPin;
const uint32_t maskB = g_APinDescription[pinB].ulPin;

boolean chanA;
boolean chanB;

int8_t oldstate = 3;
int enc = 0;
int val = 0;
int old = 0;

const uint8_t detents = 3;

const int8_t enctable[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};

// half step:
// const int8_t enctable[] = {0, 0, -1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, -1, 0, 0};

void setup() {
  Serial.begin(115200);
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  attachInterrupt(pinA, isr, CHANGE);
  attachInterrupt(pinB, isr, CHANGE);
}

void loop() {
 update();
}

void isr() {
  chanA = portA -> PIO_PDSR & maskA; // instead of digitalRead(pinA);
  chanB = portB -> PIO_PDSR & maskB; // instead of digitalRead(pinB);
  int8_t state = chanA | (chanB << 1);
  if (oldstate != state) {
    enc += enctable[state | (oldstate << 2)];
    if (state == detents)
      val = enc >> 2;
    oldstate = state;
  }
}

void update() {
  if (val > old) {
    counter++;
    Serial.println(val);
    old = val;
  }
  else if (val < old) {
    counter--;
    Serial.println(val);
    old = val;
  }
}

I am getting an error
encoder_with_byte_move:6:1: error: 'Pio' does not name a type
Pio
portA = g_APinDescription[pinA].pPort;*

Can you help what to do with Pio* or which library to include ?
Thanks

Hi,
What model Arduino are you using?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Arduino uno 3 )

The code you quoted is for a Due - a different processor family.

Thanks
Can it be used for arduino mega?

No, it can't.
The Mega 's processor is the same family as the Uno.

What is it what you want to do it in the end?

As a first hint: there is a library called NewEncoder which can be donwloaded from GitHub.
This library works for most processors like Arcuino Uno / Mega / ESP / Teensy etc.

the GiPo has some example-codes that show how to use it.
This library offers the comfort of setting up a minimum and a maximum-value and has use-functions like upclick / downclick

So please give a detailed description of what you want to do in the end.

best regards Stefan

I like the code above with right shift operator, very interesting solution.
Ill check the code on GitHub, thanks )

In may case solenoid valve is time to time operating unexpectedly, I believe the issue is my buggy code.

volatile unsigned int temp; //This variable will increase or decrease depending on the rotation of encoder

int counter = 1040;
volatile int candleLenght = 0; // Lenght -> signal  for cutting candle
int RELAY1 = 7; // relay for cutting solenoid


int y; // var for operate solenoid on/off
volatile int switchPin8,switchPin9;
void setup() {
  Serial.begin (115200);
  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, HIGH);  // initiate solenoid OFF 
  pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 
  pinMode(3, INPUT_PULLUP); // internal pullup input pin 3
  //Setting up interrupt A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2.
  attachInterrupt(0, ai0, RISING);
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3.
  attachInterrupt(1, ai1, RISING);
  }
   
  void loop() {
   
  // Send the value of counter
  if( counter != temp ){
  candleLenghtButton();
  temp = counter;
  }
  
  }


  /***********************************
   ******* CANDLE L BUTTONS ********
   ***********************************/
   void candleLenghtButton() { // to be adjusted via analogue value later
    candleLenght = 1040;     
  }
   


   
   
  /***********************************
   ******* ANGLE ENCODER *************
   ***********************************/
  void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
  counter++;
  Serial.println(counter);
        if (counter % candleLenght == 0 ) { // activate cutting solenoid @ candleLenght
         y = !y; 
         cutCandle(y);//cut candle function

        }
  }else{
  //counter--; //we do not want count anticlockwise
  }
  }
   
  void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
  //counter--; //we do not want count anticlockwise
  }else{
  counter++;
   if (counter % candleLenght == 0 ) { // activate cutting solenoid @ candleLenght
         y = !y; 
         cutCandle(y);//cut candle function

        }
  }
  }


  /***********************************
   ******* CUT CANDLE SOLENOID *******
   ***********************************/
  int cutCandle(volatile unsigned int x){
     if (x==1) {
      digitalWrite(RELAY1, HIGH);  //solenoid OFF
   delay(1);
      Serial.println("activate cutting solenoid OFF");
      
      Serial.println(x+10);
     
      
     }
     if (x==0) {
      digitalWrite(RELAY1, LOW);  //solenoid ON
        delay(1);
      Serial.println("activate cutting solenoid ON");
       
      Serial.println(x);
      
     }

  }

if I reset counter and insert this code 2 times, it works a bit better, but still double open/close some times appear in 1 second.

cutCandle(y);//cut candle function
counter = 0;

so if you have a real question you might get support.