Arduino Stops After Period of time.

Well i made a small circuit and program that takes readings from a LDR sensor, then lights a set of 4 BlinkM's when the LDR Detects a "Blink Of Light".

The program stops for no apparent reason, and the Onboard LED is stuck on..Im assuming the Timer area of my sketch is the Problem.

#include <Wire.h>

const int LDRinput = 0;       // LDR (Light Sensor Input Pin).
const int LEDpin = 13;	// OnBoard LED On Arduino for HeartBeat.
const int CountUpButton = 6;     // Button to control mode during bootup

int BeepEnabled = 0;   // 0=OFF 1=ON

int LDRreading;	        // Variable to Hold the Live LDRreading from the LDR Sensor(LED Blink Sensor).
int PreviousLDRreading;           // Variable to Hold The Last LDRreading from the LDR Sensor.
int count = 0;          // Variable to Hold the Current Count of Blinks detected from the LDR Sensor
int LastCount;


int ledState = LOW;

unsigned long Threshold = 1000;    // Variable to Set the Threshold for the Brightness of the LED + Ambient Light.
unsigned long TimeOut = 4000;     // Variable to Set the Time out between blink and no blink.
unsigned long DelayValue = 0;  // Variable to Set the Delay before program will continue.

unsigned long LastTimeExecuted;  // Variable (32bit size) to hold the Last time a Case (1-5) was executed. its a Timer to set the TIMEOUT if no Blink is detected.
unsigned long time = 0;	         // Variable (32bit size) to hold the Last time the LDR "sees" a Blink for the Versus Sensor.
unsigned long debounce = 50;     // Variable (32bit size) to hold a time so that Rapid Blinks, or Constant Light are ignored.

unsigned long previousLEDmillis = 0;
unsigned long StatusLEDinterval = 1000; 


int firsttime = 1;
unsigned long startTime;
unsigned long pressTime;



//Variables that hold a 0 or 1 value to make the Switch Cases Execute Once. 
int Case0Executed;
int Case1Executed;
int Case2Executed;












void setup()
{

  //Sets up hardware inputs + outputs
  pinMode(LDRinput, INPUT);
  pinMode(LEDpin, OUTPUT);

  pinMode(CountUpButton, INPUT);           // set pin to input
  digitalWrite(CountUpButton, HIGH);


  Serial.begin(9600);


  //Sends a STOP command to ALL (0x00) BlinkM LEDS
  Wire.begin();
  Wire.beginTransmission(0x00);
  Wire.send('o');
  Wire.endTransmission();
  Wire.begin();
  Wire.beginTransmission(0x00);
  Wire.send('f');
  Wire.send(128);
  Wire.endTransmission();
  Wire.beginTransmission(0x00);
  Wire.send('t');
  Wire.send(0);
  Wire.endTransmission();
  delay(50);


  //START UP TONES AND LIGHTS FOR SELF CHECKUP
  tone(10, 400, 50);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0xff);
  Wire.send(0x00);
  Wire.send(0x00);
  Wire.endTransmission();
  delay(100);
  tone(10, 800, 50);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0x00);
  Wire.send(0xff);
  Wire.send(0x00);
  Wire.endTransmission();
  delay(100);
  tone(10, 1200, 50);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0x00);
  Wire.send(0x00);
  Wire.send(0xff);
  Wire.endTransmission();
  delay(100);
  tone(10, 1600, 50);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0xff);
  Wire.send(0xff);
  Wire.send(0x00);
  Wire.endTransmission();
  delay(100);
  tone(10, 2000, 100);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0xff);
  Wire.send(0xff);
  Wire.send(0xff);
  Wire.endTransmission();
  delay(250);
  tone(10, 100, 100);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0x00);
  Wire.send(0xff);
  Wire.send(0xff);
  Wire.endTransmission();
  delay(250);
  tone(10, 2000, 500);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0x00);
  Wire.send(0x00);
  Wire.send(0xff);
  Wire.endTransmission();
  delay(50);
  Wire.beginTransmission(0x00);
  Wire.send('c');
  Wire.send(0x00);
  Wire.send(0x00);
  Wire.send(0x00);
  Wire.endTransmission();
  delay(50);
}

void loop()             //START PROGRAM
{

  Serial.println(count);





  LDRreading = analogRead(LDRinput);          //Set LIVE READING
  unsigned long currentMillis = millis();     //Set Current Time


  if (LDRreading < Threshold && PreviousLDRreading > Threshold && millis() - time > debounce) {
    count++;            // increment count
    time = millis();    // Remember when the last button press was 
    ledState = HIGH;



    //Any Code In this area will run only when the LDR detects a Blink, and coding only executes once per blink.
  }
  PreviousLDRreading = LDRreading;

  if(count >= 2){
    Wire.beginTransmission(0x00);
    Wire.send('n');
    Wire.send(0x00);
    Wire.send(0x00);
    Wire.send(0x00);
    Wire.endTransmission();
    count = 0;
  }


  switch (count){
  case 0:
    Wire.beginTransmission(0x00);
    Wire.send('c');
    Wire.send(0x00);
    Wire.send(0x00);
    Wire.send(0x00);
    Wire.endTransmission();
    Case1Executed = 0;
    Case2Executed = 0;

    if(Case0Executed == 0){
      LastTimeExecuted = currentMillis;
      Case0Executed = 1;
    }

    break;
    /*
  case 1:
     Wire.beginTransmission(0x00);
     Wire.send('c');
     Wire.send(0x00);
     Wire.send(0x00);
     Wire.send(0x00);
     Wire.endTransmission();
     Case0Executed = 0;
     if(Case1Executed == 0){
     LastTimeExecuted = currentMillis;
     Case1Executed = 1;
     }
     
     break;
     
     */


  case 1:
    if(Case2Executed == 0){
      if(BeepEnabled == 1)
      {
        tone(10, 3000, 100);
      }
      Wire.beginTransmission(0x00);
      Wire.send('n');
      Wire.send(0xff);
      Wire.send(0x00);
      Wire.send(0xff);
      Wire.endTransmission();
      delay(100);
      if(BeepEnabled == 1)
      {
        tone(10, 3000, 100);
      }
      Wire.beginTransmission(0x00);
      Wire.send('c');
      Wire.send(0x00);
      Wire.send(0x00);
      Wire.send(0x00);
      Wire.endTransmission();
      delay(100);
      if(BeepEnabled == 1)
      {
        tone(10, 3000, 100);
      }
      Wire.beginTransmission(0x00);
      Wire.send('n');
      Wire.send(0xff);
      Wire.send(0x00);
      Wire.send(0xff);
      Wire.endTransmission();
      delay(100);
      if(BeepEnabled == 1)
      {
        tone(10, 3000, 100);
      }
      Wire.beginTransmission(0x00);
      Wire.send('c');
      Wire.send(0x00);
      Wire.send(0x00);
      Wire.send(0x00);
      Wire.endTransmission();
      Case2Executed = 1;
      LastTimeExecuted = currentMillis;
      delay(DelayValue);
    }



  default:
    count = 0;
  }



  if(currentMillis - LastTimeExecuted >= TimeOut){     //If Led 
    count = 0;
  }

  LastCount = count;







  if(currentMillis - previousLEDmillis > StatusLEDinterval) {
    // save the last time you blinked the LED 
    previousLEDmillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LEDpin, ledState);
  }  








  if(digitalRead(CountUpButton) == LOW){
    count++;
  } 
}












/*

 
 |--BLINKM COMMAND SETUP-------------------------------------------|
 |                                                                 |
 |             COMMAND NAME          CHAR   ARGS      FORMAT       |
 |_________________________________________________________________|
 | Go to RGB Color Now          |   n   |   3   |  {‘n’,R,G,B}      |   
 | Fade to RGB Color            |   c   |   3   |  {‘c’,R,G,B}      |
 | Fade to HSB Color            |   h   |   3   |  {‘h’,H,S,B}      |
 | Fade to Random RGB Color     |   C   |   3   |  {‘C’,R,G,B}      |
 | Fade to Random HSB Color     |   H   |   3   |  {‘H’,H,S,B}      |
 | Play Light Script            |   p   |   3   |  {‘p’,n,r,p}      |
 | Stop Script                  |   o   |   0   |  {‘o’}            |
 | Set Fade Speed               |   f   |   1   |  {‘f’,f}          |
 | Set Time Adjust              |   t   |   1   |  {‘t’,t}          |
 | Get Current RGB Color        |   g   |   0   |  {‘g’}            |
 | Write Script Line            |   W   |   7   |  {‘W’,n,p,...}    |
 | Read Script Line             |   R   |   2   |  {‘R’,n,p}        |
 | Set Script Length & Repeats  |   L   |   3   |  {‘L’,n,l,r}      |
 | Set BlinkM Address           |   A   |   4   |  {‘A’,a...}       |
 | Get BlinkM Address           |   a   |   0   |  {‘a’}            |
 | Get BlinkM Firmware Version  |   Z   |   0   |  {‘Z’}            |
 | Set Startup Parameters       |   B   |   5   |  {‘B’,m,n,r,f,t}  |
 |                                                                 |
 |-----------------------------------------------------------------|
 
 
 
 */

You need to put some debug print statements in to see at what point it stops. It could be that you have intermittent connections on one device or some other hardware glitch.
If it always stops in the same place then it points to that part. If random then you might have a decoupling issue on your power supplies.

  Wire.begin();
  Wire.beginTransmission(0x00);
  Wire.send('o');
  Wire.endTransmission();
  Wire.begin();
  Wire.beginTransmission(0x00);
  Wire.send('f');
  Wire.send(128);
  Wire.endTransmission();
  Wire.beginTransmission(0x00);
  Wire.send('t');
  Wire.send(0);
  Wire.endTransmission();

There should only be one Wire.begin() call in the sketch.

PaulS, is there a Wire.end() also to free up the pins to do something else?

Grumpy_Mike:
You need to put some debug print statements in to see at what point it stops. It could be that you have intermittent connections on one device or some other hardware glitch.
If it always stops in the same place then it points to that part. If random then you might have a decoupling issue on your power supplies.

The arduino cannot be connected to a computer at this time, and the power supply is a wall wort 9v 1000ma hooked up about 60 feet away.

And the Led i have Coded with the Blink with out delay example, and that is what was solid when i seen that the device was locked up.

I do have a OpenLog device from Sparkfun,(SparkFun OpenLog - DEV-13712 - SparkFun Electronics) if i connected that, how would i get a data stream to a file on it?

and the power supply is a wall wort 9v 1000ma hooked up about 60 feet away.

I bet that is the problem, what sort of extra decoupling have you got?

Grumpy_Mike:

and the power supply is a wall wort 9v 1000ma hooked up about 60 feet away.

I bet that is the problem, what sort of extra decoupling have you got?

decoupling???
well the only thing is that it lasted over a month with hour to hour use, then just locked up... i assumed that since i made the onboard LED a "Heartbeat" and that was what was locked on solid it must not be alternating with this code.

if(currentMillis - previousLEDmillis > StatusLEDinterval) {
    // save the last time you blinked the LED 
    previousLEDmillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LEDpin, ledState);
  }

Grumpy_Mike:

and the power supply is a wall wort 9v 1000ma hooked up about 60 feet away.

I bet that is the problem, what sort of extra decoupling have you got?

do you think that it would last more than a month with no decoupling????

Why are you using currentMillis and not mills();

It could be on the edge and the extra activity of the LED pushed it over. It never hurts to have too much decoupling.