Hey all,
So, I’m currently trying to create an Arduino controlled Hot Wheels track, with photoresistors doing the heavy lifting for when a car passes the start and finish points. However, I also want to calculate the time that a car took to travel down the track (this will allow me to later calculate the speed of the car) so I am trying to use the millis() function. The problem is that it causes the program to continuously restart.
Here is my code (with the offending code commented out):
/*
* Created by Jivan RamjiSingh
*
* Code for a semi-full scale 6 lane track setup
* 12 Photoresistors, 2 per lane
* 7 LEDs, 1 per lane + 1 "ready"
*
*/
const char lightPin[] = {3, 4, 5, 6, 7, 8, 2};
const char topPho[] = {A0, A1, A2, A3, A4, A5};
const char botPho[] = {A6, A7, A8, A9, A10, A11};
int topVal[5] = {};
int botVal[5] = {};
int trigTopVal[5] = {};
int trigBotVal[5] = {};
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long elapsedTime = 0;
boolean topTrig = true;
boolean botTrig = true;
void setup() {
Serial.begin(9600);
Serial.println("Initializing");
Serial.println();
Serial.print("Configuring LED pin outputs: ");
for (int n = 0; n < 8; n += 1) {
Serial.print(n);
Serial.print(" ");
pinMode(lightPin[n], OUTPUT);
digitalWrite(lightPin[n], LOW);
}
Serial.println();
Serial.println();
Serial.print("Setting up lane: ");
for (int i = 0; i < 6; i += 1) {
Serial.print(i);
Serial.print(" ");
topVal[i] = analogRead(topPho[i]);
botVal[i] = analogRead(botPho[i]);
trigTopVal[i] = (0.75 * topVal[i]);
trigBotVal[i] = (0.75 * botVal[i]);
}
Serial.println();
digitalWrite(lightPin[6], HIGH);
}
void loop() {
int botValue = analogRead(A6);
int topValue = analogRead(A0);
if ((botValue <= trigBotVal[0]) && (botTrig)) {
digitalWrite(3, HIGH);
botTrig = false;
//endTime = millis();
//elapsedTime = (endTime - startTime);
//elapsedTime = (elapsedTime / 1000);
//Serial.println(elapsedTime);
Serial.println("Triggered bottom photoresistor");
}
if ((topValue <= trigTopVal[0]) && (topTrig)) {
digitalWrite(4, HIGH);
topTrig = false;
//startTime = millis();
Serial.println("Triggered top photoresistor");
}
}
This code outputs the following on serial:
Initializing
Configuring LED pin outputs: 0 1 2 3 4 5 6 7
Setting up lane: 0 1 2 3 4 5
Triggered top photoresistor
Triggered bottom photoresistor
However, if I remove the comments, then I get this output (repeating forever):
Initializing
Configuring LED pInitializing
Configuring LED pInitializing
Configuring LED p⸮Initializing
Configuring LED pin ⸮Initializing
Configuring LED pInitializing
Configuring LED p⸮Initializing
Configuring LED p⸮Initializing
Configuring LED p⸮Initializing
Configuring LED pInitializing
Configuring LED p⸮Initializing
Configuring LED pInitializing
Configuring LED pInitializing
Configuring LED pInitializing
Configuring LED pin ⸮Initializing
Configuring LED pInitializing
Configuring LED pInitializing
Thank you!