Hello all,
I am writing an Arduino program to track rotary motion and translate to linear distance.
I am taking this linear measurement and storing it on an SD card and printing it to a 16x2 LCD display.
Everything seems to be working fine, except that if I rotate the encoder semi-fast, the value doesnt update.
Im assuming this works on some type of hall effect sensor, so I can understand why this would happen, however it seems like it isnt very constant. When rotating the sensor clockwise, for example, it seems to catch at lower rotational speeds as opposed to ccw motion.
I'm not sure if there is an error in my code causing it to catch, or if I have a bad encoder, wiring setup, or loose connections.
All connections SEEM fine, sometimes the seems encoder works better if I push it harder into the breadboard (could be my imagination, though - and the trend with cw being worse then ccw still continues).
Can anyone help confirm if it is my code, something hardware related, or if this is non avoidable using a rotary encoder?
My wiring: CLK and DT are plugged into digital pins, BTN (SW) is not wired.
CODE:
#include <SD.h>
#include <LiquidCrystal_I2C.h>
#define outputA 6
#define outputB 7
#define pitchDiameter 0.56229722 //Inches
float counter = 0.0;
int aState;
int aLastState;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//Constant for sd card
const int chipSelect = 53;
void setup() {
//Initialize LCD
lcd.init();
lcd.backlight();
//Begins SD Card Output
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
//Establish pins for encoder
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
aLastState = digitalRead(outputA);
Serial.begin(9600);
lcd.setCursor(0,0);
lcd.print("Position: ");
// SD initialization
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present.");
}
else
{
Serial.println("Card initialized");
}
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState)
{
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState)
{
counter += 0.07853981633 * pitchDiameter;
}
else
{
counter -= 0.07853981633 * pitchDiameter;
}
Serial.print("Position: ");
Serial.println(counter);
//Print position to LCD
lcd.setCursor(0,0);
lcd.print("Position: ");
lcd.print(" ");
lcd.setCursor(10,0);
lcd.print(counter);
lcd.setCursor(0,1);
lcd.print("Inches");
}
aLastState = aState; // Updates the previous state of the outputA with the current state
//Add position to data string
String dataString = "";
dataString += String("Position: ");
dataString += String(counter);
//Open datafile
File dataFile = SD.open("DataLog.txt", FILE_WRITE);
//If the datafile is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
}
//If the file isn't open, pop up an error:
else
{
}
}