Mega 2560: Millis() causing the board to continuously restart

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!

Check your array indexes.

You're writing outside the bounds of the arrays.

Blackfin:
Check your array indexes.

You’re writing outside the bounds of the arrays.

Okay, so I changed the for loop from n < 8 to n < 7 so as to not go outside of my array. However, this does not solve the issue with using millis(). Unless I am mistaken and you meant something else?

Yep, something else. Don’t duplicate post!! ( Reported )

Yeah, I realize that it was a duplicate post, I reported the other one already. I would have just deleted it and posted it here but apparently, you can't delete posts that you make in that board -- and the original post did not belong there.

Look at your console output:

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

Your arrays are initialized with:

int trigTopVal[5] = {};
int trigBotVal[5] = {};

"Setting up lane" shows 6 values ranging from 0 to 5 inclusive but your arrays are only of size 5.

You may have fixed the problem with the LED array but you need to ensure all instances of array accesses are done within bounds.

jivandabeast:
Okay, so I changed the for loop from n < 8 to n < 7 so as to not go outside of my array. However, this does not solve the issue with using millis(). Unless I am mistaken and you meant something else?

You are still writing outside of array here :

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

Blackfin:
You may have fixed the problem with the LED array but you need to ensure all instances of array accesses are done within bounds.

alesam:
You may have fixed the problem with the LED array but you need to ensure all instances of array accesses are done within bounds.
You are still writing outside of array here :

Wow, I feel dumb lol. Thank you guys so much! :slight_smile: