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