encoder.h with interrupt example T3.2

Status
Not open for further replies.

Mehow

Member
Hi everyone,

Here's an age old question about this encoder.h library: what is the proper way to use this library with interrupts with a quadrature encoder pot/knob..

There are clear examples of how to use it by polling.... but the library has a lot of provision for interrupts and how to implement them on the T3.2 evades me..

Thank you in advance.


20210526_214811.jpg
Here's what I've put together so far:

Code:
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
Encoder Enc(15, 16);


void setup() 
{
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  attachInterrupt(15, endoce, CHANGE);
  attachInterrupt(16, endoce, CHANGE);
  Serial.println("Encoder Test:");
 }

volatile long Pos; volatile int count;

void loop() 
{

}

void endoce()
{ 
  noInterrupts();

  Pos = Enc.read();
  if (Pos != count)
  {
    Serial.print("Pixel# = ");
    Serial.println(Pos);
    count = Pos;
  }
    
  interrupts();  
}
 
Why are you disabling interrupts?

isn't that what you are supposed to do with interrupt routines?

Code:
void interruptName()
{
Disable interrupt
--do stuff
Enable interrupt
}

Normally I would use these commands but I'm just getting started with Teensy but I don't know if those Arduino libraries will translate correctly.

cli()
sei()
 
isn't that what you are supposed to do with interrupt routines?

Not inside them, that's to create a critical section for communicating with the ISR from the
rest of the program.

You shouldn't be doing serial I/O inside an ISR, that could deadlock the processor.

Which library are you using? Normally it will hide this stuff from you so you only have to call
read() method.

You know the Teensy comes with an encoder library? As far as I'm aware you've have got it's examples available on
the drop-down menu if you look.
 
Yes, thank you MarkT for the helpful suggestions about the ISR and refraining from using Serial.print within it.

Just like the code indicates I'm using encoder.h library..

What I'm really looking for is what is the proper way to setup an ISR with a rotary encoder using encoder.h library.... all of the provided examples have you polling the counter inside the loop().... and I would love to get away from that method and have it managed inside the ISR.

encoder.h was written with significant provision for ISR handling, but I need some help with that setup..

I know there are these commands:

Enc.read()
Enc.readAndReset()
Enc.write()
 
AFAICT by default the library uses interrupts (if you give it pins that support interrupts). If you look
at the NoInterrupts example you'll see it explicitly prevents interrupts being used.
 
I apologize, I don't know what "AFAICT" means...

Yes, it does look like most of the bit handling is done by way of interrupts inside the library which is great...

This is my latest iteration of implementing this library inside an interrupt... It's not even close to finished, I'm just establishing functionality one step at a time..

Code:
#include <Arduino.h>
#include <Encoder.h>
#define ENCODER_USE_INTERRUPTS
#define FALSE 0
#define TRUE 1

void endoce(void); 

Encoder myEnc(15, 16);

void setup() {
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  attachInterrupt(15, endoce, CHANGE);
  attachInterrupt(16, endoce, CHANGE);
  Serial.begin(57600);
  Serial.println("Encoder Test:");

}

volatile long oldPosition  = -999;
volatile bool positionFlag = FALSE;

void loop() 
{
  while(positionFlag == TRUE)
  {
    Serial.println(oldPosition);     //output result to serial monitor in loop()
    positionFlag = FALSE;
  }
}

void endoce()                           //read the status of encoder pins and update "oldPosition"   
{ 
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) 
  {
    oldPosition = newPosition;
    positionFlag = TRUE;
  }
}
 
Status
Not open for further replies.
Back
Top