Hi, I have programed Nanos to tell me the speed of my locomotives. A LCD shows the last speed and the latest reading.
It displays to one tenth of a second.
In the New speed if there is no character to display, for example it is only 2 km/h, it reads 2 and then three parallel lines in the text box. When it moves up to Last this disappears. I cannot see any difference in my sketch.
I wonder how I can get rid of the parallel lines.
#include <LiquidCrystal_I2C.h>
//// Model Railway Speedometer /////
////Northern2////
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int WAITING = 1;
const int S_2 = 2;
const int S_3 = 3;
const int READY = 4;
const int stoptime = 5;
//// VARIABLES ////
float distance = 1.5 ; //USER SETTING: Change number to measured distance between sensors in metres.
float scale = 87.1; // USER SETTING: THIS IS FOR HO SCALE. Change as needed for other scales.
float starttime, finish, elapsed, kilometres, hours, kh, scaleKH;
int sensorLeft = 5;
int sensorRight = 4;
int leftValue;
int rightValue;
int var = READY;
void setup() {
//Initiate the LCD
lcd.init();
lcd.backlight();
pinMode(sensorLeft, INPUT);
pinMode(sensorRight, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // LCD backlight on
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Van Raay ");
lcd.setCursor(4, 1);
lcd.print(" Railway ");
delay(1000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Northern");
lcd.setCursor(4, 1);
lcd.print("Track");
delay(900);
lcd.clear();
}
void loop() {
switch (var) {
case READY:
lcd.setCursor(4, 0);
lcd.print("Ready");
delay(1000);
var = WAITING;
break;
case WAITING:
leftValue = digitalRead(sensorLeft);
if (leftValue == 0) {
starttime = millis();
lcd.clear ();
lcd.setCursor(0, 0);
lcd.print("Last");
lcd.setCursor(5, 0);
lcd.print(scaleKH, 1);
lcd.setCursor(10,0);
lcd.print(" km/h");
lcd.setCursor(3,1);
lcd.print("Timing");
var = S_2;
delay(3000);// changed time, different to North Track
}
break;
case S_2:
rightValue = digitalRead(sensorRight);
if (rightValue == 0) {
finish = millis();
var = stoptime;
}
break;
case stoptime:
elapsed = finish - starttime; // millis
elapsed = elapsed / 1000; // seconds
kilometres = distance / 1000; // kilometres
hours = elapsed / 3600; // hours
kh = kilometres / hours;
scaleKH = kh * scale;
{
lcd.setCursor(1, 1);
lcd.print("New");
lcd.setCursor(5,1);
lcd.println(scaleKH, 1);
lcd.setCursor(10,1);
lcd.print(" km/h");
delay (500);
lcd.setCursor(1,1);
lcd.print("New ");
delay(500);
lcd.setCursor(1, 1);
lcd.print("New");
lcd.setCursor(5,1);
lcd.println(scaleKH, 1);
lcd.setCursor(10,1);
lcd.print(" km/h");
//// flash new speed
for (int i=0; i<8; i++) {
lcd.setCursor(1,1);
lcd.print("New ");
delay(300);
lcd.setCursor(1, 1);
lcd.print("New");
lcd.setCursor(5,1);
lcd.println(scaleKH, 1);
lcd.setCursor(10,1);
lcd.print(" km/h");
delay (500);
}
}
var = WAITING;
break;
}
}