Confused about time to speed formulas

So I have worked out how long it takes for my model train to pass two laser tripwires that are spaced 20 cm apart. But how do I turn that into meters per second?

This code tells me that if it takes 3 seconds to travel 20 centimeters, that my trains speed is nearly 21000 km/h. Something tells me that that isn't correct. Where have I gone wrong?

#include "U8glib.h"

U8GLIB_ST7920_128X64_4X u8g(10);

uint8_t draw_state = 0;

const int laserPin1 = 2;
const int laserPin2 = 3;
const int laserPin3 = 4;
const int laserPin4 = 5;

const int ldrPin1 = A0;
const int ldrPin2 = A1;
const int ldrPin3 = A2;
const int ldrPin4 = A3;

unsigned long startcount1 = 0;
unsigned long endcount1 = 0;

byte scale = 87.1;

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 22, "Hello World!");
}

void setup() {
  Serial.begin(9600);
  pinMode(laserPin1, OUTPUT);
  pinMode(laserPin2, OUTPUT);
  pinMode(laserPin3, OUTPUT);
  pinMode(laserPin4, OUTPUT);
  digitalWrite(laserPin1, HIGH);
  digitalWrite(laserPin2, HIGH);
  digitalWrite(laserPin3, HIGH);
  digitalWrite(laserPin4, HIGH);
  pinMode(ldrPin1, INPUT);
  pinMode(ldrPin2, INPUT);
  pinMode(ldrPin3, INPUT);
  pinMode(ldrPin4, INPUT);
}

void loop() {
  int ldrStatus1 = analogRead(ldrPin1);
  if (ldrStatus1 <= 400) {
    startcount1 = millis();
    Serial.println(startcount1);
    endcount1 = 0;
  } else {
  }
  int ldrStatus2 = analogRead(ldrPin2);
  if (ldrStatus2 <= 400) {
    endcount1 = millis();
    Serial.println(endcount1);
    unsigned long time1 = (endcount1 - startcount1);
    Serial.println(time1);
    float speedms1 = (scale * 200000) / time1);
    Serial.println(speedms1);
    float speedkph1 = (3.6 * speedms1);
    Serial.println(speedkph1);
    u8g.firstPage();
    do {
      draw();
    } while ( u8g.nextPage() );
  } else {
  }
  delay(100);
}

20cm in 3s = 6.7cm/s
6.7cm/s = 0.067m/s
0.067m/s = 0.24Km/h

That's nice, but every time it goes through it will be a different speed. There needs to be a formula that will work for every speed.

What does this number represent?

What does this number represent?

What does this number represent?

87.1 represents the scale train I am using. (HO scale)
200000 is the distance between the two tripwires (20 cm)
3.6 is supposedly the number used to change m/s to kph. I got that off the internet.

Nevermind, I was calculating for microseconds, and using milliseconds. So I figured that one out myself...
:rofl:

How long did you think about it before you posted on the forum?

1 Like

About 15 minutes....
Don't mind me, I'm dense, tired, and have a cold, so I'm not the sharpest I've ever been today.

this may be helpful

    S = 87
    D = 20      # cm
    t =  3      # sec

    cmPsec  = D/t
    ScmPsec = S * cmPsec
    SmPhr   = ScmPsec * (3600 / 100)
    Sfps    = ScmPsec * (.01 * 39.4 / 12)
    Smph    = Sfps    * 3600 / 5280

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.