Unable to Display variable value in 16x2 LCD

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);
}
}

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

Move your LCD pins away from digital #1 because it is used by Serial TX.

david_prentice:

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

Move your LCD pins away from digital #1 because it is used by Serial TX.

Yes. Thank you. I have changed the pin from 1 to 2 so that it is now "LiquidCrystal lcd(2, 3, 4, 5, 6, 7); ".
And now the sequence runs but no data is shown in the LCD. Maybe it is in loop and hence it is overwriting continuously. Is there a way to change the sequence so that the value comes only once and data shows in the LCD? (i.e, it is continuously providing output and if i try a while loop, it still shows nothing. Serial Monitor also goes empty.

Please go and read the instructions, then go back and modify your post using the "More --> Modify" option you will find to the bottom right of the post, to mark up the code (but it always needs to be the complete code) as such so we can examine it conveniently and accurately. Please do not post a ".ino" file as an attachment - that would mean that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour. In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, whatever code you do post could well be garbled and is certainly anything but easy to read. Yours seems to have survived so far but I didn't look too closely.

Note: Also mark up any data - as a separate section - in the same way. This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code. You can then use the "copy for forum" function in order to paste it into a message with the "code" tags already provided.

Try and avoid unnecessary white space (blank lines). You should only use these to separate functional blocks of code.