Arduino Sketch stops running after a minute

I recently created a board of LEDs which fade in and out. My sketch works for a short amount of time, but for some reason, the sketch stops running and I have to unplug and plug the power source back in order to restart it. Here is the sketch:

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int RED = 11; 
int BLUE = 10;// the pin that the LED is attached to
int led3 = 9;
int led4 = 6;

int toggle = 0;
int DELAY = 30;

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

int count = 0;
// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  if(toggle == 0)
  {
     // set the brightness of pin 9:
     analogWrite(RED, brightness);    
     analogWrite(BLUE, 0);
     analogWrite(led3, 0);
     analogWrite(led4, 0);
     // change the brightness for next time through the loop:
     brightness = brightness + fadeAmount;

     // reverse the direction of the fading at the ends of the fade: 
     if (brightness < 1 || brightness > 254) {
       fadeAmount = -fadeAmount ; 
       count++;
     }     
     if(count == 2)
     {
      toggle = 1; 
      count = 0;
     }
     // wait for 30 milliseconds to see the dimming effect    
     delay(DELAY); 
  }
  else if(toggle == 1)
  {
     // set the brightness of pin 9:
     analogWrite(RED, 0);    
     analogWrite(BLUE, brightness);
     analogWrite(led3, 0);
     analogWrite(led4, 0);
     // change the brightness for next time through the loop:
     brightness = brightness + fadeAmount;

     // reverse the direction of the fading at the ends of the fade: 
     if (brightness < 1 || brightness > 254) {
       fadeAmount = -fadeAmount ; 
       count++;
     }     
     if(count == 2)
     {
      toggle = 2; 
      count = 0;
     }  
     // wait for 30 milliseconds to see the dimming effect    
     delay(DELAY);
  }    
  else if(toggle == 2)
  {
     // set the brightness of pin 9:
     analogWrite(RED, 0);    
     analogWrite(BLUE, 0);
     analogWrite(led3, brightness);
     analogWrite(led4, 0);
     // change the brightness for next time through the loop:
     brightness = brightness + fadeAmount;

     // reverse the direction of the fading at the ends of the fade: 
     if (brightness < 1 || brightness > 254) {
       fadeAmount = -fadeAmount ; 
       count++;
     }     
     if(count == 2)
     {
      toggle = 3; 
      count = 0;
     }  
     // wait for 30 milliseconds to see the dimming effect    
     delay(DELAY);
  }  
    else if(toggle == 3)
  {
     // set the brightness of pin 9:
     analogWrite(RED, 0);    
     analogWrite(BLUE, 0);
     analogWrite(led3, 0);
     analogWrite(led4, brightness);
     // change the brightness for next time through the loop:
     brightness = brightness + fadeAmount;

     // reverse the direction of the fading at the ends of the fade: 
     if (brightness < 1 || brightness > 254) {
       fadeAmount = -fadeAmount ; 
       count++;
     }     
     if(count == 2)
     {
      toggle = 0; 
      count = 0;
     }  
     // wait for 30 milliseconds to see the dimming effect    
     delay(DELAY);
  }
}

I know that the battery is not running out of power. I am using an Arduino Pro Mini 3.3v 8MHz.

What do you mean "stops running"?

Bye the way I'll give even money on the battery Are you using a PP3?

Mark

My lights fade in and out in a sequence; after a little bit of time, the light cuts out at any point in the cycle and no longer turns on. I am using an external battery pack hooked up via USB. As far as I know, the light continues if it is plugged into the computer via a usb cable.

As far as I know, the light continues if it is plugged into the computer via a usb cable.

If it’s yours you know …

You have power issues. I win!

Mark

Strange, my power supply says it outputs at 5V and I'm feeding the power through RAW, and I know it's not out of charge. I'm not really sure why it cuts out.

Your using int for your brightness variable, suggest using byte.

EDIT: Try printing out your count and brightness variables ... this will show if their values get out of range when the problem occurs.

I have a second battery pack, I'm thinking something may be whacked with this one. If it works, I'll update the post.

Charsmud: I recently created a board of LEDs which fade in and out. My sketch works for a short amount of time, but for some reason, the sketch stops running

I tested your sketch on my Uno, and after 10 minutes it is still running. What is "a short amount of time" in your book? 10 seconds? 2 hours?

... after a little bit of time, the light cuts out at any point in the cycle ...

That doesn't clarify. Please specify in real units, eg. "33 seconds".


You could probably usefully rewrite a bit to use a function to do the dimming. A lot of that code looks copy-and-pasted. If there is a bug, you copy-pasted it four times.

A variance of time around 1 to 3 minutes.

EDIT: I've switched out the power supply and it seems to be working fine now. Seems that the power supply I was using had something wrong with it. When I attempted to charge it, it made a high-pitched buzzing noise, which may have been the cause of the problem.

It's still working 4.5 hours later. :)

Hi, Have you got a DMM to measure the battery voltage as it fails. What is the battery that makes a buzzing noise, can you post a picture please?

Thanks.. Tom... :)