DC/AC Motor encoder pulse testing [Solved]

Hello,
I am working on a project to test DC/AC motor encoders. I want to facilitate the diag of the motor by reading the channel A and B I count the number of pulses if I have a difference more than 2 pulses. I display an error. My system is working well but I don't understand when I got to 34,000 pulses or so it goes into negative value and loops. I don't understand why it doesn't increment to infinity.
Do you have any idea why?
Thanks for your help!

#define Encoder_output_A 2 
#define Encoder_output_B 3 

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 


#define OLED_RESET     -1 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
int Count_pulses = 0;
int Count_pulses1 = 0;
int gap = 2;
int gap1 = -2;

void setup() {

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
}


pinMode(Encoder_output_A,INPUT); 
pinMode(Encoder_output_B,INPUT); 
attachInterrupt(digitalPinToInterrupt(Encoder_output_A),DC_Motor_Encoder,  RISING    );
attachInterrupt(digitalPinToInterrupt(Encoder_output_B),DC_Motor_Encoder1, RISING     );
}
 
void loop() {
  display.clearDisplay();


 display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("CHA:"));

 
 display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(50,0);             // Start at top-left corner
  display.println(Count_pulses);
  
 display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,15);             // Start at top-left corner
  display.println(F("CHB:"));

 
 display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(50,15);             // Start at top-left corner
  display.print(Count_pulses1);

  
int fault = (Count_pulses - Count_pulses1) ;

 display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(00,50);             // Start at top-left corner
  display.println(fault);
  display.display();


if ( fault >  gap ){ 

  
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,35);             // Start at top-left corner
  display.println(F("Fault A "));
  display.display();

  delay(1000);
  
  }

  if ( fault <  gap1 ){ 

  
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,35);             // Start at top-left corner
  display.println(F("Fault B "));
  display.display();

  delay(1000);
  }

  
  

}

void DC_Motor_Encoder(){
  int b = digitalRead(Encoder_output_B);
    Count_pulses++;

}
void DC_Motor_Encoder1(){
  int a = digitalRead(Encoder_output_A);
    Count_pulses1++;

}

Video of this mistak

uint32_t Count_pulses = 0;
uint32_t Count_pulses1 = 0;

too strong thank you it works perfectly! and then I understood why!

it can be noted that the problem is solved in such and such a post

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