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