Interrupts not working

I am writing a program that changes the speed of a DC motor with encoder based on a potentiometer value, and that can measure the speed of the motor using the readings from the encoder outputs. The program works fully on tinkercad, however once I run the code on my Arduino uno (which has been wired up exactly the same as on tinkercad), the interrupt function no longer gets called and therefore cannot read the rpm of the motor.

Here is the code:

#define enA 9
#define in1 6
#define in2 7
#define button 4
#define enca 2
#define encb 3

volatile int count = 0;
int rotDirection = 0;
int pressed = false;
int prevT = 0;
float outCount = 0;

void encoderPulse()
{
  count++;
}

void setup() {
  Serial.begin(9600);
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  pinMode(enca, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enca), encoderPulse, RISING);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
}

void loop() {
  int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

  // Read button - Debounce
  if (digitalRead(button) == true) {
    pressed = !pressed;
  }
  while (digitalRead(button) == true);
  delay(20);

  // If button is pressed - change rotation direction
  if (pressed == true  & rotDirection == 0) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    rotDirection = 1;
    delay(20);
  }
  // If button is pressed - change rotation direction
  if (pressed == false & rotDirection == 1) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    rotDirection = 0;
    delay(20);
  }
 
  int currT = millis();
  if ((currT - prevT) >= 200) {
    outCount = (count*100)/(currT - prevT);
    prevT = currT;
    count = 0;
    Serial.print(outCount);
    Serial.print(" ");
    Serial.print("RPM");
    Serial.println();
    if (rotDirection == 1) {
      Serial.print("Clockwise");
    }
    else {
      Serial.print("Anti-Clockwise");
    }
    Serial.println();
    Serial.print("Trimmer Value : ");
    Serial.print(pwmOutput);
    Serial.println();
    Serial.println();
  }  
}

And here is how the board is wired up:

Thanks for any help you can offer me!

How do you know this?

It is not possible to access the variable "count" atomically so it should only be copied in a critical section

int pwmOutput = potValue / 4;

Is more obvious and, arguably, more correct

if (pressed == false & rotDirection == 1) {

"&&" Would be more correct

It looks to me like your code is incomplete

I know that the interrupt function does not get called as I added the following line into the interrupt function :
Serial.println("test");
, but nothing is output to the serial monitor, however when I add the same line of code to the Tinkercad project, "test" is repeatedly output to the serial monitor.

Serial.println("test");

If you added that to the ISR then don't rely on it working. Serial uses interrupts. Interrupts are automatically turned off when in an ISR

If you want to test that the ISR is being triggered do something like turning on an LED from within the ISR

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