Hello everyone. My church is doing a Grand Prix(basically a pinewood derby), and want me to make a timer. So far I have built the timer system, which uses one photoresistor at the start, and four at the bottom. I also have lasers shining at the sensors, so that it's very easy to tell when they're tripped. The whole thing is ready except for the code, which is giving me some difficulties. I want each cars time to get recorded from when the first sensor is tripped. This has proved a challenge, and I have not figured out a solution yet. I've got it to show the winners' time, but nothing else. Here's the code. Thanks in advance for anyone who helps me!!!!
/* For track setup, first connect all sensors in the right order. Next, test the value of the photoresistor output. Now change all the <=? to about 50-100 lower than their normal values.
You also need to change the delay on the start timer depending on how fast most of the cars are. After each run you have to reset the arduino.*/
const int sensorPin1 = A2;
const int sensorPin2 = A3;
const int sensorPin3 = A4;
const int sensorPin4 = A5;
const int sensorPin = A0;
const int TIMEOUT = 5000; // milliseconds
#define THRESHOLD 100
unsigned long start, finished, elapsed;
void setup() {
Serial.begin(9600);
Serial.print("Ready...");
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(sensorPin3, INPUT);
pinMode(sensorPin4, INPUT);
}
void displayResult()
{
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
ms=over%1000;
Serial.print("Time: ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.print("ms");
Serial.println();
}
void displaystatus5()
{
start=millis();
Serial.println();
Serial.println("...Started...");
Serial.println();
}
void loop() {
int status1 = analogRead(sensorPin1);
int status2 = analogRead(sensorPin2);
int status3 = analogRead(sensorPin3);
int status4 = analogRead(sensorPin4);
int status5 = analogRead(sensorPin);
if (status5 <=THRESHOLD) {
displaystatus5();
}
else if (status1 <=THRESHOLD) {
Serial.println("Lane #1");
finished=millis();
displayResult();
delay(TIMEOUT);
}
else if (status2 <=THRESHOLD) {
Serial.println("Lane #2");
finished=millis();
displayResult();
delay(TIMEOUT);
}
else if (status3 <=THRESHOLD) {
Serial.println("Lane #3");
finished=millis();
displayResult();
delay(TIMEOUT);
}
else if (status4 <=THRESHOLD) {
Serial.println("Lane #4");
finished=millis();
displayResult();
delay(TIMEOUT);
}
}
Race_Track_Place_Stopwatch.ino (1.98 KB)