Go Down

Topic: Optical encoder; direction determining problem (Read 129 times) previous topic - next topic

Stronghold_

Hello everyone,

I have 2 Optical encoders in a row, and I can read all the pulses passed by these encoders. (Basically, wheel rotating, and I should read the pulses ) I use interrupt for both encoders.

The question is that is it possible to determine the direction of the wheel in the interrupt itself?
If yes, I will be appreciated if you suggest how;

I can not do it in the loop because there is a lot of delays in it.

This is my code;
Code: [Select]


int pin = 2;
int pin2 = 3;
int dovrSayi = 0;
int counter = 0;
int dovrSayi2 = 0;
int counter2 = 0;

unsigned long vaxt = 0 ;
unsigned long vaxt2 = 0;

int signals = 15; // pulses that needed for one rotation
int bir = 0 ;
unsigned long currentTime = 0;
void setup() {

  // Set encoder pins as inputs
  pinMode (pin, INPUT);
  pinMode(pin2, INPUT);

  Serial.begin (9600);

  attachInterrupt(digitalPinToInterrupt(pin), updateCounter, RISING);
  attachInterrupt(digitalPinToInterrupt(pin2), updateCounter2, RISING);

}

void loop() {

  Serial.print ("counter : ");
  Serial.println(counter);


  Serial.print ("dovr sayi : ");
  Serial.println(dovrSayi);


   if ((vaxt2 > vaxt) ) {                                       
    Serial.println(vaxt2 - vaxt);
  }
  if (!(vaxt2 > vaxt) ) {
    Serial.println(vaxt - vaxt2);
  }

  delay(300);

  if (counter >= signals) {

    dovrSayi ++;
    counter = 0 ;

  }
  if (counter2 >= signals) {

    dovrSayi2 ++;
    counter2 = 0 ;

  }


// Serial Read and other stuf also added;

}

void updateCounter()
{
  counter ++;
vaxt = millis();
}

void updateCounter2()
{
  counter2++;
  vaxt2 = millis();
}

Paul_KD7HB

All depends on HOW you have the encoders positioned!!!!! When one is seeing the LED light through a hole or slot, the other MUST be off. Do you have them mounted this way?

Paul

jremington

#2
Aug 21, 2019, 01:54 am Last Edit: Aug 21, 2019, 01:56 am by jremington
This introduction should answer your questions: http://www.creative-robotics.com/quadrature-intro

Stronghold_

Thank you very much  !!  

The link really helped me to solve the problem...

Go Up