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;
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();
}