Interrupts Not Working?

I have an arduiono uno, and am attempting to calculate the speed of a DC motor with encoder using interrupts, however my code which works in tinkercad does not work on my arduino.
Here is the the code :

int enA = 9;
int in1 = 6;
int in2 = 7;
int enca = 2;
int encb = 3;

volatile int count = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enca, INPUT_PULLUP);
  pinMode(encb, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enca), encoderPulse, RISING);
   // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

void loop() {
  analogWrite(enA, 100);
  count = 0;

void encoderPulse()

On my arduino it just outputs count as 0 every time. Any help would be greatly appreciated as me getting this to work would allow me to continue with my project. Thanks alot.

void loop() {
  analogWrite(enA, 100);
  count = 0;

What happens if you don't keep setting count to 0 ?

Count still outputs as 0

suggest simply reading and printing the values of the encoder pins to verify their functionality without using an interrupt

Better do the interrupt correctly from the start. In this case the interrupt routine uses as little as code as possible and the code handling the interrupt, located in loop(), can be expanded without much impact to the ISR itself.

Can you use a wire, one end attached to ground, and actually touch it to the enca pin to make it transition from high to low and back to high? The default pin mode setting is coded with input pullup and the interrupt pin is set to trigger on a low to high pulse, so to make an interrupt the pin must go from high to low and from low to high.

and try to code withOUT using delay().

It is important to have the least amount of code in an ISR as possible.

Have you jumpered pin 9 to pin 2?
When I run your code with a jumper in place I see counts of 98 which is about right for the pwm frequency and the 200 ms sample period.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.