railway, speedometer, i2c, IR sensors, close.

Got it, or closer. Can I delete this post?

After weeks I am really close. The IR sensors are working and this is changing leds. It is clear I am doing more wrong, (not something, I am doing a lot wrong).
The locomotive passes an IR sensor left and then another, right.
I have spent weeks working on it and am slowly getting there. It is frustrating. I have looked at and copied various sketches from tutorials and have tried very hard.
I wonder if someone could be kind enough to have a quick look and see if it is something simple.
I know I am close, well a lot closer than I have been. Now I need to get it display onto the LCD unit.

[#include <LiquidCrystal_I2C.h>

////    MODEL RAILROAD SPEEDOMETER    /////

#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.000; //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 = 4;
int sensorRight = 5;
int leftValue;
int  rightValue;
int var = READY;
int ledReady = 10;
int ledDelay = 11; //NEW. Added this version for new LED on pin 11. CDH
int led13 = 13;

void setup() {
  //Initiate the LCD


  lcd.init();
  lcd.backlight();


  pinMode(sensorLeft, INPUT);
  pinMode(sensorRight, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ledReady, OUTPUT);
  pinMode(ledDelay, 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(2, 0); //Start at character 0 on line 0
  lcd.print("VanRaay");
  lcd.setCursor(2, 1);
  lcd.print("Railway");
  delay(2000);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("SPEEDOMETER");
  delay(1000);
  lcd.clear();

}


void loop() {

  switch (var) {

    case READY:
      lcd.setCursor(2, 0);
      lcd.print("Ready");
      delay(1000);
      var = WAITING;
      break;

    case WAITING:
      leftValue = digitalRead(sensorLeft);
      if (leftValue == 0) {
        starttime = millis();
        lcd.clear ();
        lcd.setCursor(7, 0);
        lcd.print("STARTED");
        var = S_2;
        digitalWrite(LED_BUILTIN, HIGH); //Turns on LED 13Y when sensor Left1 is tripped. 
        digitalWrite(ledReady, LOW);  //Turns off Pin 10G LED when sensor Left is tripped. 
        delay(1000);

      }

      rightValue = digitalRead(sensorRight);
      if (rightValue == 0) {
        starttime = millis();
        lcd.clear ();
        lcd.setCursor(7, 0);
        lcd.print("STARTED");
        var = S_3;
        digitalWrite(LED_BUILTIN, HIGH); //Turns on LED 13Y when sensor left is tripped. 
        digitalWrite(ledReady, LOW);  //Turns off Pin 10G LED when sensor left is tripped. 
      }
      break;

    case S_2:
      rightValue = digitalRead(sensorRight);
      if (rightValue == 0) {
        finish = millis();
        var = stoptime;
        digitalWrite(LED_BUILTIN, LOW); //Turns off LED 13Y when sensor Right is tripped. 
        digitalWrite(ledDelay, HIGH);  //Turns on Pin 11R LED when sensor Right is tripped. 
      }
      break;

    case S_3:
      leftValue = digitalRead(sensorLeft);
      if (leftValue == 0) {
        finish = millis();
        var = stoptime;
        digitalWrite(LED_BUILTIN, LOW); //Turns off LED 13Y when sensor Right is tripped. 
        digitalWrite(ledDelay, HIGH);  //Turns on Pin 11R LED when sensor Right is tripped. 
      }
      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.clear ();
      lcd.setCursor(4, 1);
      lcd.print("scaleKH");



      delay(1000);

      var = READY;
      digitalWrite(ledDelay, LOW);  //Turns off Pin 11R LED after delay is complete. 
      digitalWrite(ledReady, HIGH);  //Turns on Pin 10G LED after delay is complete. 
      digitalWrite(led13, LOW);  //Turns off Pin 13Y LED after delay is complete. 
      break;
  }
}

richardvanraay:
Can I delete this post?

What I’d suggest is to post the current, and maybe later on the final, code and schematic in separate posts in this thread for others to learn from.

Some folk mark the opening post title as [solved] or similar.

Thanks. I have itworking now . It has taken me a very long time.
I would like to know how to fix the display. How can I round the speed off to the nearest whole number?
[code#include <LiquidCrystal_I2C.h>

////    MODEL RAILROAD SPEEDOMETER    /////

#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.000; //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 = 4;
int sensorRight = 5;
int leftValue;
int  rightValue;
int var = READY;
int ledReady = 10;
int ledDelay = 11; //NEW. Added this version for new LED on pin 11. CDH
int led13 = 13;

void setup() {
  //Initiate the LCD


  lcd.init();
  lcd.backlight();


  pinMode(sensorLeft, INPUT);
  pinMode(sensorRight, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ledReady, OUTPUT);
  pinMode(ledDelay, 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(2, 0); //Start at character 0 on line 0
  lcd.print("Van Raay");
  lcd.setCursor(2, 1);
  lcd.print("Railway");
  delay(2000);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("Speedometer");
  delay(1000);
  lcd.clear();
 
  }




void loop() {

  switch (var) {

    case READY:
      lcd.setCursor(4, 0);
      lcd.print("Speed");
      delay(1000);
      var = WAITING;
      break;

    case WAITING:
      leftValue = digitalRead(sensorLeft);
      if (leftValue == 0) {
        starttime = millis();
        lcd.clear ();
        lcd.setCursor(2, 0);
        lcd.print("Begin timing");
        var = S_2;
        digitalWrite(LED_BUILTIN, HIGH); //Turns on LED 13Y when sensor Left1 is tripped. 
        digitalWrite(ledReady, LOW);  //Turns off Pin 10G LED when sensor Left is tripped. 
        delay(1000);

      }

      rightValue = digitalRead(sensorRight);
      if (rightValue == 0) {
        starttime = millis();
        lcd.clear ();
        lcd.setCursor(7, 0);
        lcd.print("STARTED");
        var = S_3;
        digitalWrite(LED_BUILTIN, HIGH); //Turns on LED 13Y when sensor left is tripped. 
        digitalWrite(ledReady, LOW);  //Turns off Pin 10G LED when sensor left is tripped. 
      }
      break;

    case S_2:
      rightValue = digitalRead(sensorRight);
      if (rightValue == 0) {
        finish = millis();
        var = stoptime;
        digitalWrite(LED_BUILTIN, LOW); //Turns off LED 13Y when sensor Right is tripped. 
        digitalWrite(ledDelay, HIGH);  //Turns on Pin 11R LED when sensor Right is tripped. 
      }
      break;

    case S_3:
      leftValue = digitalRead(sensorLeft);
      if (leftValue == 0) {
        finish = millis();
        var = stoptime;
        digitalWrite(LED_BUILTIN, LOW); //Turns off LED 13Y when sensor Right is tripped. 
        digitalWrite(ledDelay, HIGH);  //Turns on Pin 11R LED when sensor Right is tripped. 
      }
      break;

    case stoptime:
      elapsed = finish - starttime; // millis
      elapsed = elapsed / 1000; // seconds
      kilometres = distance / 10000; // kilometres
      hours = elapsed / 3600; // hours
      kh = kilometres / hours;
      scaleKH = kh * scale;

     lcd.clear();
      lcd.setCursor(4, 1);
     
      lcd.println(scaleKH);



      delay(1000);

      var = READY;
      digitalWrite(ledDelay, LOW);  //Turns off Pin 11R LED after delay is complete. 
      digitalWrite(ledReady, HIGH);  //Turns on Pin 10G LED after delay is complete. 
      digitalWrite(led13, LOW);  //Turns off Pin 13Y LED after delay is complete. 
      break;
  }
}]

Hal_Posoyan:
What I'd suggest is to post the current, and maybe later on the final, code and schematic in separate posts in this thread for others to learn from.

Some folk mark the opening post title as [solved] or similar.

Thank you.

richardvanraay:
Got it, or closer. Can I delete this post?

Why would you want to delete it?

Leave it here as your Open Source contribution - your learning may help another person.

…R