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