Resetting after 30 seconds without serial connected

I have an arduino nano 3. I think I have a bug in my code but have no idea how to find it.

Basically, if I power up the arduino it will operate my code seemingly properly. Everything works and I know a loop is happening because a dimming circuit is functioning. After about 30 seconds (I counted it several times and it was the same every time) the arduino resets. The dimming circuit stops, all outputs flop to their pullup and millis goes back to zero for about a second then it starts working again.

Their weird part is if I connect serial monitor to it, it won't do it. It runs flawlessly for as long as I have worked with it. This leads me to believe there is an issue with serial code?

This leads me to believe there is an issue with serial code?

What serial code? I don't see any serial code. Or, any other code, for that matter. Care to remedy that?

You are absolutely right and I apologize… I wasn’t thinking.

//define pins
#define LDRInput 1
#define ignitionInput 2
#define pcrunningInput 3
#define powerFETOutput 4
#define pcswitchOutput 5
#define PWMOutput 6
#define roofInput 7
#define dashInput 8
#define steeringInput 0  //analog

//define global variables
boolean ignitionOn = false;          //Arduino's known ignition state.  used to compare current state and new ignition input pin
boolean ignitionInputState = false;  //state of ignition input pin (HIGH or LOW)
boolean roofSwitch = false;
boolean dashLights = false;
boolean blAuto = true;              //keeps track of manual vs auto backlight from computer.  Defaults to auto.
float blValue = 1;                  //if backlight isn't auto then this holds the manual value from PC in percentage.  1 = 100%
int swrest = 235;                   // rest resistance of steering wheel controls 
int steeringValue = 0;               //holds recent steering wheel control button value if not at rest value
int steeringSent = 0;                //holds most recently sent sw value.  This keeps from sending the same value repeatedly from one press
int LDRValue = 0;
int PWM = 0;


void setup() {
  pinMode(ignitionInput, INPUT);
  pinMode(pcrunningInput, INPUT);
  pinMode(powerFETOutput, OUTPUT);
  pinMode(pcswitchOutput, OUTPUT);
  pinMode(PWMOutput, OUTPUT);
  pinMode(roofInput, INPUT);
  pinMode(dashInput, INPUT);
  digitalWrite(pcswitchOutput, LOW); //set to low by default.  A pulse is to feed 5v into opto which shorts laptop power switch to ground.
  digitalWrite(powerFETOutput, LOW); //set to low to keep stuff off by default.
  Serial.begin(9600);
  hold();  //kick right into hold to avoid powering stuff up
}

void loop() {
//START STARTUP/SHUTDOWN CONTROLLER CODE----------------------------------------------------------------------------
ignitionInputState = digitalRead(ignitionInput);
if (ignitionInputState != ignitionOn)        //if known state differs from input state, do something
{
  ignitionOn = ignitionInputState;
  if (ignitionOn == true) //ignition was turned on, initiate powerup
  {
    if (digitalRead(pcrunningInput) == false) //check if computer is off
    { 
      digitalWrite(powerFETOutput, HIGH); //turn on accessory power
      delay(1000);//adjust faster if possible, slower if accessory not up fast enough
      digitalWrite(pcswitchOutput, HIGH); //pulse header .5 second
      delay(500);
      digitalWrite(pcswitchOutput, LOW);
    }
    else digitalWrite(powerFETOutput, HIGH);
  }
  else  //ignition was turned off, initiate shutdown
  {
    if (digitalRead(pcrunningInput) == true) //check if computer is on
    {
       digitalWrite(pcswitchOutput, HIGH);  //pulse header .5 second
       delay(500);
       digitalWrite(pcswitchOutput, LOW);
       int shutdownTimer = 0;
       do
       {
          delay(1000);    //wait a second and check if pc is still running
          if (digitalRead(pcrunningInput) == false)
          {
            break;  //pc down, move on with program
          }
          else if (shutdownTimer==20)  //pc not down, check if we have waited 20 seconds
          {
             digitalWrite(pcswitchOutput, HIGH); //pulse header 5 seconds to kill pc
             delay(5000);
             digitalWrite(pcswitchOutput, LOW);
             break;
          }
          shutdownTimer+=1;
       } while (shutdownTimer <21);
    }
    digitalWrite(powerFETOutput, LOW); //kill power
    analogWrite(PWMOutput, 0); //stop PWM
    PWM = 0;
    hold ();
  }
}
//END STARTUP/SHUTDOWN CONTROLLER CODE-----------------------------------------------------------------------------

//START STEERING WHEEL CONTROL CODE -----------------------------------------------------------------------------
int steeringValue = analogRead(steeringInput);
if (steeringValue != swrest) & (steeringValue != steeringSent)
{
  //code to send value to PC
  Serial.print("SW");  //prefix for steering wheel
  Serial.println(steeringValue); //send value.
  steeringSent = steeringValue
}
if (steeringValue == swrest)
{
  steeringSent = 0;
}
steeringValue = 0;
//END STEERING WHEEL CONTROL CODE --------------------------------------------------------------------------

//START BACKLIGHT CONTROL CODE ---------------------------------------------------------------------------
if (blAuto == false)
{
  analogWrite(PWMOutput, (255*blValue));  //set pwm equal to blvalue percentage of 255
}
else
{
  LDRValue = analogRead(LDRInput);
  PWM = map(LDRValue,1,1023,255,1); //map analog value to PWM and place in variable for debug info
  analogWrite(PWMOutput, PWM);
}
  
//END BACKLIGHT CONTROL CODE ---------------------------------------------------------------------------

//BEGIN ROOFSWITCH CODE ---------------------------------------------------------------------------
boolean rooftemp = digitalRead(roofInput);
if (roofSwitch != rooftemp)
{
  roofSwitch = rooftemp;  //roof switch changed, send serial to computer
  Serial.print("RS");
  Serial.println(roofSwitch,DEC);
  delay(50); //debounce
}
//END ROOFSWITCH CODE ------------------------------------------------------------------------------

//START DASHLIGHT CODE ----------------------------------------------------------------------------
boolean dashtemp = digitalRead(dashInput);
if (dashLights != dashtemp)
{
 dashLights = dashtemp;
 Serial.print("DL");
 Serial.println(dashLights,DEC); 
 delay(50); //debounce
}
//END DASHLIGHT CODE ----------------------------------------------------------------------------

//START SERIAL RECEIVE CODE -----------------------------------------------------------------------
if (Serial.available() > 0 ) //do we have serial data in buffer
{
  int serialbyte = Serial.read();
  switch (serialbyte)
  {
    case 49:                    //ASCII 49 - 58 = 1-9&":" DEC are 10 - 100% brightness, ASCII 0 or DEC48 = auto brightness
      blAuto = false;
      blValue = .1;
      break;
    case 50:
      blAuto = false;
      blValue = .2;
      break;
    case 51:
      blAuto = false;
      blValue = .3;
      break;
    case 52:
      blAuto = false;
      blValue = .4;
      break;
    case 53:
      blAuto = false;
      blValue = .5;
      break;
    case 54:
      blAuto = false;
      blValue = .6;
      break;
    case 55:
      blAuto = false;
      blValue = .7;
      break;
    case 56:
      blAuto = false;
      blValue = .8;
      break;
    case 57:
      blAuto = false;
      blValue = .9;
      break;
    case 58:
      blAuto = false;
      blValue = 1;
      break;
    case 48:
      blAuto = true;  //set back to auto
      blValue = 1;  //reset back to full bright
      break;
    case 72:
      Serial.println("HB"); //sends heartbeat response
      break;
    case 82:  //uppercase R ASCII
      //code here to send status
      Serial.print("RS");
      Serial.println(roofSwitch,DEC);
      break;
    case 83: //debug/all status info  "S"
      printdebug();
    //default:
  }
}
//END SERIAL RECEIVE CODE -------------------------------------------------------------------------
}

void hold ()   //function that holds arduino in "hold" pattern listening only for ignition on
{
  while(digitalRead(ignitionInput) == LOW)
  {
    if (Serial.available() > 0 ) //do we have serial data in buffer
    {
      int serialbyte = Serial.read();
      if (serialbyte == 83) printdebug();
    }
  }
}

void printdebug ()
{
  Serial.print("DB");
      Serial.print(millis());
      Serial.print("!");
      Serial.print(digitalRead(ignitionInput));
      Serial.print("!");
      Serial.print(digitalRead(powerFETOutput));
      Serial.print("!");
      Serial.print(digitalRead(pcrunningInput));
      Serial.print("!");
      Serial.print(digitalRead(roofInput));
      Serial.print("!");
      Serial.print(analogRead(steeringInput));
      Serial.print("!");
      Serial.print(digitalRead(dashInput));
      Serial.print("!");
      Serial.print(analogRead(LDRInput));
      Serial.print("!");
      if (blAuto == true)
      {
        Serial.print("Auto");
        Serial.print("!");
        Serial.println(PWM);
      }
      else
      {
        Serial.print(blValue);
        Serial.print("!");
        Serial.println(blValue*255);
      }
//     Serial.print(" Roof:");
//     Serial.print(digitalRead(roofInput));
//     Serial.print(" LDR:");
//     Serial.print(analogRead(LDRInput));
   }
      if (serialbyte == 83) printdebug();

Now, why would I want to go look up what 83 corresponds to?

      if (serialbyte == 'S') printdebug();

Means exactly the same thing to the compiler, but it means a whole lot more to me.

Some comments in the code would be useful. In hold(), for instance, there is no clue what that is doing. Waiting for some switch to be pressed is obvious, but why?

There's no clue what is actually connected to the Arduino. pcrunningInput? Means nothing to me. pcswitchOutput? Ditto.

The implication is that there will be something connected to these pins, and that there might be a PC involved, but it certainly isn't clear what or how.

ignitionInputState = digitalRead(ignitionInput);
if (ignitionInputState != ignitionOn)        //if known state differs from input state, do something
{
  ignitionOn = ignitionInputState;
  if (ignitionOn == true) //ignition was turned on, initiate powerup
  {
    if (digitalRead(pcrunningInput) == false) //check if computer is off
    {

digitalRead() does not return true or false. It returns HIGH or LOW. Get with the program, please. If HIGH and LOW don't mean anything to you, #define some more meaningful names, like PRESSED and RELEASED and assign them values of HIGH or LOW as appropriate.

I like names that have a degree of sameness to them, like currState and prevState. The look related to me. ignitionOn for last time around and ignitionInputState for this time around don't really do anything for me.

Please use Tools + Auto format to correct the indentation of the code. I read up to the point where hold() is called in loop(), and the closing braces retreat to column 1, looking like the end of loop(). This is followed by a while bunch of code. This makes me wonder if the code even compiles, at which point I quit reading.

When I initially started this thread my mindset wasn't digging into my code specifically but rather in general was there gotchas like over filling a variable or something that would cause this behavior. Your initial post pointed out that it might be difficult without the code which I hadn't considered prior.

I'm an amateur trying to learn. I have read all of the learning and reference articles, there is just a TON to remember. I have written this code for my own use and not to mass produce or hand to other people so the comments and variables work for me. I will attempt to help them make more sense for you to understand.

I couldn't figure out how to get it to understand 'S' and read a post someplace on using ascii... I will update it.

I thought I was using true and false for boolean variable comparison. Although there may be exceptions(will scrutinize the code later) a quick read through showed low and high for digital read not comparing to boolean. Not that it is best practice... but it does work so is there a reason NOT to do it?

There are comments in hold... it waits for ignition to turn on. I assume you didn't read hold as it is further down.

I can explain everything in the code, I just figured a 3 page post would get ignored.

The arduino is located in a vehicle and is connected to the vehicle in many ways and a pc via USB and a couple of wires.

These connections have opto couplers and other basic electrical circuitry to protect the arduino... I can enumerate those if necessary. It is connected to: a steering wheel control resistor network on analog pin 0 an LDR on analog pin 1 the vehicles ignition on digital pin 2 the computer's 5v usb pin on digital pin 3 a mosfet to turn on accessory power for several items on digital pin 4 the computers power button on digital pin 5 a mosfet to PWM an LED backlight on a screen on digital pin 6 a switch in the roof for when the top is latched on digital pin 7 the vehicles 12v dash light dimmer wire on digital pin 8

When power is applied to the arduino I figured it made sense to dump it to a "hold pattern" to simply listen for the vehicles ignition to be on as opposed to write "exceptions" into each portion of the code to skip the portion if the ignition is off.

When the ignition is turned on, it should power up the mosfet to turn on usb devices and laptop power, delay shortly and then power the laptop on by pulsing the power header to ground for a short period. (there is checking to see if it is already running)

At this point it will monitor the roof switch, dashlights and steering wheel controls for activity. If activity occurs, it will send serial data to the computer which will use the data in a vb.net application to do something.

It also monitors the LDR and adjusts the PWM of the backlight of the screen.

Lastly it listens for serial data from the laptop. Mostly for requests for status or changes to backlight settings.

When the ignition is turned off, the arduino will pulse the laptop power header to shut it down, start looking at the computers running input to see if the computer is still running. Once it sees the computer is down it shuts down the mosfet and dumps to hold again waiting for the ignition to turn on. If the computer fails to shutdown within a time interval, it holds the power header longer to kill it and then shuts everything down.

And…sorry about the formatting. I didn’t know about the auto button. Hopefully this is better.

//define pins
#define steeringInput 0
#define LDRInput 1
#define ignitionInput 2
#define pcrunningInput 3
#define powerFETOutput 4
#define pcswitchOutput 5
#define PWMOutput 6
#define roofInput 7
#define dashInput 8

//define global variables
boolean ignitionOn = false;          //Arduino's known ignition state.  used to compare current state and new ignition input pin
boolean ignitionInputState = false;  //state of ignition input pin (HIGH or LOW)
boolean roofSwitch = false;
boolean dashLights = false;
boolean blAuto = true;              //keeps track of manual vs auto backlight from computer.  Defaults to auto.
float blValue = 1;                  //if backlight isn't auto then this holds the manual value from PC in percentage.  1 = 100%
int swrest = 235;                   // rest resistance of steering wheel controls (may not need if rest=0) current 235 value is filler and not real
int steeringValue = 0;               //holds recent steering wheel control button value if not at rest value
int steeringSent = 0;                //holds most recently sent sw value.  This keeps from sending the same value repeatedly from one press
int LDRValue = 0;
int PWM = 0;


void setup() {
  pinMode(ignitionInput, INPUT);
  pinMode(pcrunningInput, INPUT);
  pinMode(powerFETOutput, OUTPUT);
  pinMode(pcswitchOutput, OUTPUT);
  pinMode(PWMOutput, OUTPUT);
  pinMode(roofInput, INPUT);
  pinMode(dashInput, INPUT);
  digitalWrite(pcswitchOutput, LOW); //set to low by default.  A pulse is to feed 5v into opto which shorts laptop power switch to ground.
  digitalWrite(powerFETOutput, LOW); //set to low to keep stuff off by default.
  Serial.begin(9600);
  hold();  //kick right into hold to avoid powering stuff up
}

void loop() {
  //START STARTUP/SHUTDOWN CONTROLLER CODE----------------------------------------------------------------------------
  ignitionInputState = digitalRead(ignitionInput);
  if (ignitionInputState != ignitionOn)        //if known state differs from input state, do something
  {
    ignitionOn = ignitionInputState;
    if (ignitionOn == true) //ignition was turned on, initiate powerup
    {
      if (digitalRead(pcrunningInput) == false) //check if computer is off
      { 
        digitalWrite(powerFETOutput, HIGH); //turn on accessory power
        delay(1000);//adjust faster if possible, slower if accessory not up fast enough
        digitalWrite(pcswitchOutput, HIGH); //pulse header .5 second
        delay(500);
        digitalWrite(pcswitchOutput, LOW);
      }
      else digitalWrite(powerFETOutput, HIGH);
    }
    else  //ignition was turned off, initiate shutdown
    {
      if (digitalRead(pcrunningInput) == true) //check if computer is on
      {
        digitalWrite(pcswitchOutput, HIGH);  //pulse header .5 second
        delay(500);
        digitalWrite(pcswitchOutput, LOW);
        int shutdownTimer = 0;
        do
        {
          delay(1000);    //wait a second and check if pc is still running
          if (digitalRead(pcrunningInput) == false)
          {
            break;  //pc down, move on with program
          }
          else if (shutdownTimer==20)  //pc not down, check if we have waited 20 seconds
          {
            digitalWrite(pcswitchOutput, HIGH); //pulse header 5 seconds to kill pc
            delay(5000);
            digitalWrite(pcswitchOutput, LOW);
            break;
          }
          shutdownTimer+=1;
        } 
        while (shutdownTimer <21);
      }
      digitalWrite(powerFETOutput, LOW); //kill power
      analogWrite(PWMOutput, 0); //stop PWM
      PWM = 0;
      hold ();
    }
  }
  //END STARTUP/SHUTDOWN CONTROLLER CODE-----------------------------------------------------------------------------

  //START STEERING WHEEL CONTROL CODE -----------------------------------------------------------------------------
  int steeringValue = analogRead(steeringInput);
  if (steeringValue != swrest & steeringValue != steeringSent)
  {
    //code to send value to PC
    Serial.print("SW");  //prefix for steering wheel
    Serial.println(steeringValue); //send value.
    steeringSent = steeringValue;
  }
  if (steeringValue == swrest)
  {
    steeringSent = 0;
  }
  steeringValue = 0;
  //END STEERING WHEEL CONTROL CODE --------------------------------------------------------------------------

  //START BACKLIGHT CONTROL CODE ---------------------------------------------------------------------------
  if (blAuto == false)
  {
    analogWrite(PWMOutput, (255*blValue));  //set pwm equal to blvalue percentage of 255
  }
  else
  {
    LDRValue = analogRead(LDRInput);
    PWM = map(LDRValue,1,1023,255,1); //map analog value to PWM and place in variable for debug info
    analogWrite(PWMOutput, PWM);
  }

  //END BACKLIGHT CONTROL CODE ---------------------------------------------------------------------------

  //BEGIN ROOFSWITCH CODE ---------------------------------------------------------------------------
  boolean rooftemp = digitalRead(roofInput);
  if (roofSwitch != rooftemp)
  {
    roofSwitch = rooftemp;  //roof switch changed, send serial to computer
    Serial.print("RS");
    Serial.println(roofSwitch,DEC);
    delay(50); //debounce
  }
  //END ROOFSWITCH CODE ------------------------------------------------------------------------------

  //START DASHLIGHT CODE ----------------------------------------------------------------------------
  boolean dashtemp = digitalRead(dashInput);
  if (dashLights != dashtemp)
  {
    dashLights = dashtemp;
    Serial.print("DL");
    Serial.println(dashLights,DEC); 
    delay(50); //debounce
  }
  //END DASHLIGHT CODE ----------------------------------------------------------------------------

  //START SERIAL RECEIVE CODE -----------------------------------------------------------------------
  if (Serial.available() > 0 ) //do we have serial data in buffer
  {
    int serialbyte = Serial.read();
    switch (serialbyte)
    {
    case 49:                    //ASCII 49 - 58 = 1-9&":" DEC are 10 - 100% brightness, ASCII 0 or DEC48 = auto brightness
      blAuto = false;
      blValue = .1;
      break;
    case 50:
      blAuto = false;
      blValue = .2;
      break;
    case 51:
      blAuto = false;
      blValue = .3;
      break;
    case 52:
      blAuto = false;
      blValue = .4;
      break;
    case 53:
      blAuto = false;
      blValue = .5;
      break;
    case 54:
      blAuto = false;
      blValue = .6;
      break;
    case 55:
      blAuto = false;
      blValue = .7;
      break;
    case 56:
      blAuto = false;
      blValue = .8;
      break;
    case 57:
      blAuto = false;
      blValue = .9;
      break;
    case 58:
      blAuto = false;
      blValue = 1;
      break;
    case 48:
      blAuto = true;  //set back to auto
      blValue = 1;  //reset back to full bright
      break;
    case 72:
      Serial.println("HB"); //sends heartbeat response
      break;
    case 82:  //uppercase R ASCII
      //code here to send status
      Serial.print("RS");
      Serial.println(roofSwitch,DEC);
      break;
    case 83: //debug/all status info  "S"
      printdebug();
      //default:
      //no match, do this code.  Maybe a "say what?" from arduino to computer?
    }
  }
  //END SERIAL RECEIVE CODE -------------------------------------------------------------------------
}

void hold ()   //function that holds arduino in "hold" pattern listening only for ignition on
{
  while(digitalRead(ignitionInput) == LOW)
  {
    if (Serial.available() > 0 ) //do we have serial data in buffer
    {
      int serialbyte = Serial.read();
      if (serialbyte == 83) printdebug();
    }
  }
}

void printdebug ()
{
  Serial.print("DB");
  Serial.print(millis());
  Serial.print("!");
  Serial.print(digitalRead(ignitionInput));
  Serial.print("!");
  Serial.print(digitalRead(powerFETOutput));
  Serial.print("!");
  Serial.print(digitalRead(pcrunningInput));
  Serial.print("!");
  Serial.print(digitalRead(roofInput));
  Serial.print("!");
  Serial.print(analogRead(steeringInput));
  Serial.print("!");
  Serial.print(digitalRead(dashInput));
  Serial.print("!");
  Serial.print(analogRead(LDRInput));
  Serial.print("!");
  if (blAuto == true)
  {
    Serial.print("Auto");
    Serial.print("!");
    Serial.println(PWM);
  }
  else
  {
    Serial.print(blValue);
    Serial.print("!");
    Serial.println(blValue*255);
  }
  //     Serial.print(" Roof:");
  //     Serial.print(digitalRead(roofInput));
  //     Serial.print(" LDR:");
  //     Serial.print(analogRead(LDRInput));
}

Well, this version compiles, so you've fixed the couple of errors that the last one had.

Yeah, I had updated the steering wheel section this morning without testing a compile before pasting it. I have only ever programmed in vb before this so the syntax still gets me.

Not that it is best practice... but it does work so is there a reason NOT to do it?

Readability. When looking at code that uses the output of digitalRead() in a comparison,

if(digitalRead() == HIGH)

makes more sense to me than

if(digitalRead() == true)

The HIGH value implies that digitalRead() returned something that we can use. The true value implies that the digitalRead() was successful.

  boolean rooftemp = digitalRead(roofInput);

But, digitalRead() does not return a boolean.

Yes, the result is exactly the same, but one implies things that are true and one implies things that are not. To me, at least.

I have written this code for my own use and not to mass produce or hand to other people so the comments and variables work for me.

That's fair. But, might I suggest that if you put that code away for a month, it will make less sense than you think when you come back to it. If you try to write code as though lots of people needed to read, understand, and approve the code, you will strive to make the code self-documenting and crystal clear.

As a beginner, though, the code is well organized (except that I like to see the functions in the order that they are used. hold() is called before a lot of other stuff, yet it is near the bottom of the code).

I certainly wasn't criticizing your thought processes or code. I simply wanted to point out some things that I thought would make it better.

(I hate having my code reviewed, too. It's never as perfect as I think it is.)

These connections have opto couplers and other basic...

This is stuff that was not at all obvious from reading the code. Might I suggest a big comment block at the top that contains this stuff (or at the bottom if you don't want to scroll past it every time).

When power is applied to the arduino I figured it made sense to dump it to a "hold pattern" to simply listen for the vehicles ignition to be on as opposed to write "exceptions" into each portion of the code to skip the portion if the ignition is off.

I'm not sure that exception handling is quite right here. A read and an if test on each pass through loop() are pretty easy.

if(digitalRead(ignitionOn) == LOW) // Ignition switch is on..
{
}
else // Ignition switch is off
{
}

Now, having all the details on what the system is supposed to do,

Basically, if I power up the arduino it will operate my code seemingly properly. Everything works and I know a loop is happening because a dimming circuit is functioning. After about 30 seconds (I counted it several times and it was the same every time) the arduino resets. The dimming circuit stops, all outputs flop to their pullup and millis goes back to zero for about a second then it starts working again.

does this mean that if you power up the Arduino, and there is (or is not) ignitionInput supplied, that the Arduino resets after 30 seconds? Or, is the problem that the Arduino resets when there is ignitionInput but no response from the PC?

I'm trying to get a handle on the portion of the program that might be causing the issue. The fact that you see millis() go back to 0 implies that the PC is connected and working.

Thanks. I agree on readability. All of what I just put in there is heavily documented in my google docs. I won't be loosing it. I understand about single location for information but there are tons of moving parts to this project and having all the details for each piece documented in each other piece would be out of control difficult to maintain not to mention size. I have master documents that refer to and hold the most current code dumps.

On the boolean part. I totally agree on the digital read. But I'm unsure about:

 ignitionInputState = digitalRead(ignitionInput);
  if (ignitionInputState != ignitionOn)

In this case I do a digitalRead and put it into a boolean. What does the boolean hold? It is capable of 0 and 1, false and true. I'm guessing the compiler compiles it all down to the same thing... 0, false, low and 1, true, high.

Barring possible mistakes I think I use low and high for regular reads.

I didn't mean it as exceptions like "try catch" but rather instead of having "if then" starting every section, pushing it to a hold pattern made sense. The hold pattern was technically an after thought. Originally I was thinking of putting it into a lower power mode but that seemed complicated so I just didn't.

Let me clearify on the issue.

If you apply power to the arduino and it is running in the loop function but the computer which is plugged in via USB is NOT connected via serial (serial monitor, vb.net application etc.) the arduino will reboot about every 30 seconds.

To read millis obviously serial needs to be connected. During the reboot it isn't and I don't know millis specifically. To test I basically let it happen for 5 or 6 times in a row while counting seconds myself. Then I launched serial monitor and sent the 'S' command which gets the debug info. This is how I got the millis and noted that they were way lower (although not zero) then they would be if it had been up for the duration of my testing. Once I do this, it stops rebooting. It is almost like a keep alive for it.

Obviously this makes troubleshooting difficult as I cannot write any form of debug serial as the issue goes away as soon as serial is established.

Hopefully this clearifies it a bit.

Know it’s an old thread but when google on the problem this is the one that shows up for me.

I had the same problem with the reset every 30 seconds on arduino nano v3.0 and v3.1 and it drove me nuts as I couldn’t debug it more than it only happened when the arduino was connected to the computer (in my case windows 8). If I powered using a cell phone charger and USB it worked like a charm. Also if I used the serial monitor.

This thread has more details about the problem.

Hi

I’ve been working on an Arduino project and found that the board would reset itself after being connected by USB after 30 seconds.Using a 12v battery was fine and it didn’t reboot. It turns out that if the Arduino receives serial data on the USB it thinks it is a new compiled sketch and goes through its bootloader procedure.

The culprit in my case was Sony Ericsson 'companion' software trying to identify if the USB device was a mobile phone. Disabling this cured the problem. The moral of the story is don’t run any software which uses the USB whilst you are doing Arduino development.