Arduino Uno with 500 p/r Quadrature Encoder using Interrupt

Hi! I am having a basic problem but I don't know what else to do more than this.

The quadrature encoder(500 p/r) is working at 12 VDC which I took from the Arduino's power supply itself. The inputs A and B is reduced to 5v using two BD241 transistors. Transistor inputs are given to PIN 2 and PIN 3 respectively. [ I am also using two LEDs at the base of the transistors for indication]

I am using LED seven segment display to view the count. [For that I am using shift7seg.h library]

The problem I am having is the Uno misses pulses even I'm using intruppts for this project. This happens at very low speed such as 2 rev/s.

The code is as follows.

#include <shift7seg.h>
shift7seg shift(8, 9, 10);

int pinAState = 0;    
int lastButtonState = 0;

int count =0;
void setup() {
  Serial.begin(115200);
  DDRD &= ~(1 << DDD2);     // Clear the PD2 pin
    // PD2 (PCINT0 pin) is now an input

  PORTD |= (1 << PORTD2);    // turn On the Pull-up
  // PD2 is now an input with pull-up enabled

 EICRA |= (1 << ISC01);    // set INT0 to trigger on RISING 
 EICRA |= (1 << ISC00);
 EIMSK |= (1 << INT0);     // Turns on INT0
 sei();
 
 
 DDRD |= 0b00000000;
 PORTD |= 0b11111111;

 Serial.print("EICRA =");
 Serial.println(EICRA);
}

void loop() {

 if(count>=0){
shift.load_data(count);
 } else{ shift.load_data(0);
 }
 Serial.println(count); delay(100);
}

ISR (INT0_vect)
{
  if (PIND == 0b11110111 ){
  count--;

  } else if(PIND == 0b11111111){count++;}
 
}

I have written a basic C code for ISR but I think it still takes too much cycles to process. What did I miss ?

Confused.. see if your math is better than mine.

So let’s just math up the delays.
2rpm * 500 counts per rpm= 1000 counts/min
1000 counts/min / 60secs = 16.667 counts/sec
delay(100)loop * 16.667 > 1 second of delays so it’s impossible to accurately count.

Likely would get much better results skipping the delay and simply serial printing the count based on millis()

Thank you for your answer Slumpert. I rotated it 2 revolutions per second. The encoder gives 500 pulses per revolution. Which makes 1000 pulses per second in this case.

I believe there is no effect in loop delays as I am using an Interrupt for counting. Or would it?

The interrupt has very little coding as you can see in ISR(int 0_vect). Why cant it be processed within 10 to 50 clock cycles? Why is mine this slow?

Thanks.

@slumpert - the op is using interrupts! - therefore the whole of your post is garbage!

@op

First provide a circuit diagram - why are you using 12 volts for the encoder?

second - count Must be copied and then used it must never be used directly. The copying myst be done with interrupts turned off this will stop "count" being changed by the ISR while your outputting it.

Mark

"count" must also be volatile eg

volatile int count;

Mark

Hi Mark! Thank you for your answer. I will upload the circuit diagram soon.

I am using this for a paper cutter at a press. The seven segment display is used to display a length. The encoder says it operates from 5v to 24v but it doesn't give anything at 5v.

I will do those changes and let you know.

Thank you.