We have written a code for calculation of rpm using encoder motor. We have used two interrupts and made two interrupt service routines (ISR) for the same. The problem is in our code only one thing gets printed at a time on the serial monitor. Either left count or the right count. Both don't work together. Also, the code is not entering the void loop, which it should before entering the ISR's. Here's the code:
#include "digitalWriteFast.h"
// left motor pins
#define InAL 10 // INA motor pin
#define InBL 11 // INB motor pin
#define PWML 6 // PWM motor pin
#define encodPinAL 2 // encoder A pin
#define encodPinBL 3 // encoder B pin
//right motor pins
#define InAR 12 // INA motor pin
#define InBR 13 // INB motor pin
#define PWMR 7 // PWM motor pin
#define encodPinAR 18 // encoder A pin
#define encodPinBR 19 // encoder B pin
unsigned long start_Time = millis();
volatile int countLeft = 0;
volatile int countRight = 0;
volatile int left_prev_count = 0;
volatile int right_prev_count = 0;
volatile int left_RPM = 0;
volatile int right_RPM = 0;
int Leftoffset = 0;
int Rightoffset = 0;
float konstant = 11.11;
void setup()
{
// put your setup code here, to run once:
pinMode(InAL, OUTPUT);
pinMode(InBL, OUTPUT);
pinMode(PWML, OUTPUT);
pinMode(encodPinAL, INPUT_PULLUP);
pinMode(encodPinBL, INPUT);
digitalWrite(encodPinAL, HIGH); // turn on pullup resistor
attachInterrupt(digitalPinToInterrupt(2), ISRL, CHANGE);
pinMode(InAR, OUTPUT);
pinMode(InBR, OUTPUT);
pinMode(PWMR, OUTPUT);
pinMode(encodPinAR, INPUT_PULLUP);
pinMode(encodPinBR, INPUT);
digitalWrite(encodPinAR, HIGH); // turn on pullup resistor
attachInterrupt(digitalPinToInterrupt(18), ISRR, CHANGE);
Serial.begin(19200);
}
void ISRL()
{
int state = digitalReadFast(encodPinAL);
if (digitalReadFast(encodPinBL))
{
state ? countLeft-- : countLeft++;
}
else
{
state ? countLeft++ : countLeft--;
}
}
void ISRR()
{
int state = digitalReadFast(encodPinAR);
if (digitalReadFast(encodPinBR))
state ? countRight++ : countRight--;
else
state ? countRight-- : countRight++;}
void moveForward() {
digitalWrite(InAL, HIGH);
digitalWrite(InBL, LOW);
digitalWrite(InAR, HIGH);
digitalWrite(InBR, LOW);
}
void moveBackward() {
digitalWrite(InAL, LOW);
digitalWrite(InBL, HIGH);
digitalWrite(InAR, LOW);
digitalWrite(InBR, HIGH);
}
void stopMotor(){
digitalWrite(InAL, LOW);
digitalWrite(InBL, LOW);
digitalWrite(InAR, LOW);
digitalWrite(InBL, LOW);
}
void moveMotor(int Left, int Right)
{
analogWrite(PWML,Left);
analogWrite(PWMR,Right);
if(Left>0 && Right>0){
moveForward();
}
else if(Left<0 && Right<0){
moveBackward();
}
else if(Left==0 || Right==0)
{
stopMotor();
}
}
void loop()
{
moveMotor(255 + Leftoffset,255 + Rightoffset);
if((millis()-start_Time)>20)
{
Serial.println("Entering loop()");
start_Time = millis();
left_RPM = countLeft*konstant;
right_RPM = countRight*konstant;
Serial.println(left_RPM);
Serial.println(right_RPM);
countLeft = 0;
countRight = 0;
}
}