When I print it to the lcd at first it reads correctly but if i move the length measure quickly it messes up so when i go back to 0 I am actually reading x amount of pulses and a length of about .03inches instead of 0inches and 0 pulses.
If you do not use the lcd, but use Serial.print() with a high baud rate like Serial.begin(115200). Do you see the same problem?
The lcd printing is slow, especially lcd.clear(), and may cause you to miss some pulses.
Write the unchanging parts of your display in setup. Do not use lcd.clear() in loop, but rather print blank spaces to clear old data before printing new values. There is no need to calculate and print at every pulse count change. Perhaps update the display every second. Work totally in raw pulse counts as much as possible and reduce the float math.
when i go back to 0 I am actually reading x amount of pulses and a length of about .03inches instead of 0inches and 0 pulses.
How long is the travel out and back? This .03" looks like you are missing two pulses. EDIT: Math error. .03 is approximately 20 pulses. pi*1.9685/4096 = .0015" per pulse.
What are you trying to measure? What accuracy is required? What linear speed is required?
OP's code
//Encoder library
#include <Encoder.h>
#include <Keypad.h> // OneWireKeypad Library
//Encoder Definition
Encoder encoder(2, 3); // Interrupts Pins
//LCD library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LCD Definition
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Variables
float diameter = 1.9685; // Shaft or Wheel Diameter on Encoder (in)
int pulse = 4096; // Encoder Pulse
float L1 = 0;
float L2 = 0;
float length = 0; // Length Measure
long NewPulse; // Encoder Position
const float pi = 3.141592;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
'4','5','6','\b',
'1','2','3','\a',
'7','8','9','\e',
'*','0','\t','\f'
};
byte colPins[COLS] = {39, 38, 37, 36}; //connect to the row pinouts of the keypad
byte rowPins[ROWS] = {35, 34, 33, 32}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
lcd.begin(); // LCD Starting
//Serial.begin(9600); // SeriPort
lcd.backlight();
lcd.print("length Measure"); // LCD First Message
}
void loop()
{
long OldPulse = encoder.read();
if (OldPulse != NewPulse)
{
NewPulse = OldPulse;
float radius = diameter * pi; // diameter Calculation
float calc = radius / pulse; // Divided by the calculated diameter pulse count.
length = OldPulse * calc; // Multiplication of 1 pulse length calculated by the obtained pulse.
L1 = (OldPulse+1) * calc;
L2 = (OldPulse-1) * calc;
//Serial.print(OldPulse); // Pulse Count
//Serial.print(" -- ");
//Serial.print(length); // Length measure in inches.
//Serial.println(" in");
lcd.clear(); // LCD Clear
lcd.setCursor(0, 0); // Set Cursor
lcd.print("Pulse"); // LCD Print
lcd.setCursor(0, 1);
lcd.print(OldPulse);
lcd.setCursor(7, 0);
lcd.print("length");
lcd.setCursor(7, 1);
lcd.print(length,4) + lcd.print("in");
}
}