Hello,
I am trying to use a rotary encoder to measure the rotation of a wheel to determine if it is rotating or not. I also want to keep track of the time it takes for the wheel to stop rotating, in my case "pulses" switches from increasing to decreasing or vice a versa.
Interrupts are being used to detect when both the A and B signals are changing which triggers the "pulses" to increment. The code that I have works well for slow speed rotations, unsure of the RPM because I am just spinning the encoder wheel by hand, but as I spin the wheel faster the timer begins to lose time. The time and timer portion works on it's own as a stopwatch as well as the encoder portion works on it's own. It is when I try to add them together that I am running into issues. Any help with this would be greatly appreciated.
The encoder I am using is a Koyo TRD-N60-RZWD 60 pulse/rev quadrature encoder. The board is an Arduino MEGA 2560 R3, and the LCD is an Adafruit 16x2 RGB LCD.
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define WHITE 0x7
int rotation = 1;
int count = 0;
float dia = 6;
unsigned long start, finish, elapsed;
bool stopFlag = false;
volatile long pulses, A_SIG=0, B_SIG=1;
int power = 53;
int led1 = 31;
int led2 = 33;
int val = 0;
int prev;
int A = 4;
int B = 5;
void setup(){
Serial.begin(115200); //sets comm rate
lcd.begin(16,2);
lcd.print("Welcome");
delay(1500);
lcd.clear();
lcd.print("Select");
delay(1000);
lcd.clear();
lcd.print("Rotation");
delay(1000);
lcd.clear();
lcd.print("Direction");
lcd.setCursor(0,1);
//lcd.print(rotation);
lcd.setBacklight(WHITE);
pinMode(power, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
attachInterrupt(A, A_RISE, RISING);
attachInterrupt(B, B_RISE, RISING);
}//setup
uint8_t i=0;
void loop(){
digitalWrite(power, HIGH);
Serial.println(count);
while(count < 1){
uint8_t buttons = lcd.readButtons();
if(buttons & BUTTON_SELECT){
++ count;
delay(1000);
//Serial.println(count);
}
if(buttons & BUTTON_UP){
delay(1000);
++ rotation;
if ((rotation % 2) == 0){
lcd.setCursor(0,1);
lcd.print(" ");
delay(10);
lcd.setCursor(0,1);
lcd.print("Clockwise");
//Serial.print(rotation);
}
else{
lcd.setCursor(0,1);
lcd.print("CounterClockwise");
//Serial.print(rotation);
}
}
}
while(count == 1){
lcd.clear();
lcd.print("Pulley Diameter");
lcd.setCursor(0,1);
lcd.print(dia/1.00,2);
lcd.print(" inches");
//delay(3000);
count = 2;
}
while(count < 3){
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_SELECT){
++ count;
delay(1000);
Serial.println(count);
}
if(buttons & BUTTON_UP){
delay(1000);
dia = dia + 0.25;
lcd.setCursor(0,1);
lcd.print(dia/1.00,2);
}
else if(buttons & BUTTON_DOWN){
delay(1000);
dia = dia - 0.25;
lcd.setCursor(0,1);
lcd.print(dia/1.00,2);
}
}
while(count == 3){
lcd.clear();
lcd.print("Up To Start");
count = 4;
//Serial.print(rotation);
}
while(count > 3){
uint8_t buttons = lcd.readButtons();
int time = millis();
prev = val;
val = pulses;
if(buttons & BUTTON_UP){
start = millis();
lcd.clear();
lcd.print("Elapsed Time");
stopFlag = false;
}
if(stopFlag == false){
if(start > 0){
elapsed = millis() - start;
lcd.setCursor(0,1);
lcd.print(elapsed/1000.00,4);
}
}
if((rotation % 2) == 0){
if (val < prev){
finish = millis();
elapsed = finish - start;
stopFlag = true;
Serial.print(elapsed/1000.4);
}
}
else{
if(val > prev){
finish = millis();
elapsed = finish - start;
stopFlag = true;
}
}
}
}
void A_RISE(){
detachInterrupt(A);
A_SIG=1;
if(B_SIG==0)
pulses++;//moving forward
if(B_SIG==1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(A, A_FALL, FALLING);
}
void A_FALL(){
detachInterrupt(A);
A_SIG=0;
if(B_SIG==1)
pulses++;//moving forward
if(B_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(A, A_RISE, RISING);
}
void B_RISE(){
detachInterrupt(B);
B_SIG=1;
if(A_SIG==1)
pulses++;//moving forward
if(A_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(B, B_FALL, FALLING);
}
void B_FALL(){
detachInterrupt(B);
B_SIG=0;
if(A_SIG==0)
pulses++;//moving forward
if(A_SIG==1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(B, B_RISE, RISING);
}
