Arduino UNO suddenly stop working

Hello everyone!

I am using arduino Uno and would like to obtain data from an incremental encoder (angle,
velocity etc.,). I start with count and direction, it worked for a few seconds and suddenly stop after. I read about the problem with interrupt service routine and milllis() and do not quite get it, so I update without the millis() but it still does not work.

Here are my code:

volatile unsigned int enc_count = 0;
volatile unsigned int enc_mod = 0;
float angle = 0; // angle count in degree mode
int PPR = 2000; // Pulse per Revolution = 4 x CPR (Count per Revolution), 4 x 500 = 2000
//int timedelay = 100; // give delay for 100ms
//unsigned long changeTime = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), isrA, RISING); // using interrupt function on chA
  attachInterrupt(digitalPinToInterrupt(3), isrB, RISING); // using interrupt function on chB
  //changeTime = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
    enc_mod = enc_count % PPR;
    //angle = (float)enc_mod * 360.0 / PPR; // Converting to degree
    Serial.print("enc_mod = ");
    Serial.print(enc_mod);
    Serial.print(" enc_count = ");
    Serial.println(enc_count);
    //Serial.print(" angle = ");
    //Serial.println(angle);
    delay(100);
}

void isrA() { // interrupt service routine on chA
   if (digitalRead(3) == LOW){
      enc_count++; // CW
    }else {
     enc_count--; //CCW
 }
} 
  
void isrB() { // interrupt service routine on chA
    if (digitalRead(2) == LOW){
      enc_count--; // CCW
    }else{
     enc_count++; //CW
 }
}

I saw plenty of tutorials and used similiar scripts but they worked just fine. Can anybody explain?
Thank you

what do you mean by "stop working"?

An arduino crash or wrong data?

side note: you can't access in the loop the volatile variables without a critical section as they might change whilst you are using them.

I would use the encoder library, it's robust.

it suddenly freezes and stops giving value (serial monitor also freeezes). So, I need to get rid of the volatile variable? If I used the encoder library, do I have to change my arduino with teensy board?
Thank you for explanation

no volatile is mandatory. Usually in the loop you suspend the interrupts, make a copy of the volatile variable and then re-enable the interrupts and then proceed to print or calculate what you want using the copy that does not risk to be modified by an interrupt occurring at the wrong time.

the encoder library library works on your Uno without any issue.

the freeze is weird. anything else connected to your Arduino? stable power? how do you power the motor? ➜ post a circuit

I see. So, I might check my code if there are any blocking codes in between.
It is really weird, since I also tried to use other examples where they succeeded but not mine.
My arrangement is pretty straightforward, because it has a standard wiring as followed: The encoder has 2 channel and I wired them into Pin 2 and 3 Arduino Uno with a jumper wire. VCC to 5V pin and ground. I connect my Uno to my laptop and start as normally. I also checked it might be due my laptop charged, but I don't think that is the case.
Thanks for your quick response

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