I am trying to get sensor input into my program and display it in a 16x2 LCD Display. I have two proximity sensors where the time difference between the two inputs is calculated and used in a formula. That value is applied to a variable and I want that value to be displayed in the LCD. It works fine with the Serial Monitor, but the values look Gibberish in the LCD.
Please check attachment for reference.
Please find my code below and help me out with this issue.
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
int limitSwitch = 13;
int limitSwitch2 = 12;
int state1 = LOW;
int state2 = LOW;
float centimeter = 0.050;
float timeRequired = 0.000;
float velocity = 0.000;
float durationFloat = 0.000;
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
byte timerRunning;
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
pinMode(limitSwitch,INPUT);
pinMode(limitSwitch2,INPUT);
}
void loop()
{
int val1 = digitalRead(limitSwitch);
int val2 = digitalRead(limitSwitch2);
lcd.clear();
if( val1 != state1 || val2 != state2 )
{
state1 = val1;
state2 = val2;
if( state1 == 0 && timerRunning == 0 )
startTime = millis();
timerRunning = 1;
if( state2 == 0 && timerRunning == 1)
endTime = millis();
timerRunning = 0;
duration = endTime - startTime;
durationFloat = (float) duration;
timeRequired = durationFloat / 1000;
velocity = centimeter / timeRequired;
lcd.setCursor(0, 0);
lcd.print("Speed: ");
lcd.print(velocity);
lcd.setCursor(0, 1);
Serial.print("Speed in m/s = ");
Serial.println(velocity,7);
// lcd.print("Speed: "); lcd.print(velocity);
delay(1000);
}
}

