Sketch keeps resetting after ~5 minutes

I have a fairly simply sketch that will eventually be running a clock (yes another one XD). All the code (attached) behaves exactly as it should but after approximately 5 minutes the setup function re-run's.

I'm using a V3 Nano in a breadboard setup, no external devices at this time (although later a few 74HC595N will drive the output, and a bluetooth module will provide input). I'm connecting via USB and using the Serial Monitor to view the output, all on a Win7Pro PC.

Is this normal behavior ? Or is it indicative of a problem ?

I'm not normally a C programmer and I haven't done electronics for a few many years so it's quite possible I've FUBAR'ed.

Any suggestions ?

ClockCode.pde (6.52 KB)

byte registers[5];                       //6 bytes for the output of the 6 8bit registers

The comment is wrong.

  for (int i = 0; i < 6; i++)            //re-initialise all the registers
    { registers[i] = 0xFF; };

Although this statement looks like the comment was right but the declaration was wrong.

  int upperLimit = 58;                   //Highest valid ASCII value +1 (57 = "9")
  int input = 0;                         //Value entered
  
  inputTimeoutAt = millis() + inputTimeoutInterval;
                                         //Set new input timeout
  input = Serial.read();                 //Get this input
    
  switch (inputPtr)                      //Get max valid value for current 
  {
    case 0:                              //First digit 0-2
      upperLimit = 51;
      break;
    case 1:                              //If first digit was 2 then second is 0-3
      if (inputBuffer[0] == 2)
        { upperLimit = 52; };
      break;
    case 2:                              //Third digit 0-5
      upperLimit = 54;
      break;
  };                                     //Second and fourth 0-9 (set by declaration) 
  if (input > 47 && input < upperLimit)  //If digit in valid range
  { 
    inputBuffer[inputPtr] = input - 48;  //Turn character into number and store in

Since 48 = '0', this code would be a lot more readable if you got rid of the ASCII values and used character values.

Hiya Paul,

The comment is right if a little misleading. The arrays are zero based, therefore declaring

byte registers[5];

actually gives 6 bytes of variable.

The loop goes from 0 to 5 as it will run while i is less then 6.

Definitely agree that the use of ASCII is less readable than characters, I will probably change that on next iteration.

Gunney

The comment is right if a little misleading. The arrays are zero based, therefore declaring

byte registers[5];

actually gives 6 bytes of variable.

No, it doesn't. The value in the brackets is the number of elements, not the upper index.

Got me thinking now :~

Back to the books :frowning:

Time to eat humble pie !

Straight from the Main Site

Arrays are zero indexed, that is, referring to the array initialization above, the first element of the array is at index 0, hence

mySensVals[0] == 2, mySensVals[1] == 4, and so forth.

It also means that in an array with ten elements, index nine is the last element. Hence:

int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory address)

Thank you Paul for pointing out my error. I mis-understood this from my earlier reading and took it to be the same as Vb.NET.

Now changed the code to

byte registers[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
                                         //6 bytes for the output of the 6 8bit registers

And guess what, I think that it was this that was reseting the sketch as it has now run 12 minutes without a glitch :smiley: