Pages: [1]   Go Down
Author Topic: Arduino Stops After Period of time.  (Read 762 times)
0 Members and 1 Guest are viewing this topic.
Pa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino Beginner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.





Code:

#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}  |
 |                                                                 |
 |-----------------------------------------------------------------|
 
 
 
 */


Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino Beginner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,(http://www.sparkfun.com/products/9530) if i connected that, how would i get a data stream to a file on it?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Pa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino Beginner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

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

Pa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino Beginner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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????
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Pages: [1]   Go Up
Jump to: