Mood lamp program is reseting

Hi, my problem with my program is that it resets when it completes its first loop of the loop() function. Don’t know if its a memory problem, I checked some other posts related. Anyway I leave the code, hope you can help me out, thanks.

// Lampara LED's RGB "Mood Lamp"
// Version 1
// Cambio de colores basico.

// Variables globales
int brightness = 0;
int fadeAmount = 5;
int i = 3, c = 6, b = 9, d = 0;// Pines analogicos
int arr[3] = {2,4,5};// Pines digitales

void setup()
{
    pinMode(3, OUTPUT);// PWM
    pinMode(6, OUTPUT);// PWM
    pinMode(9, OUTPUT);// PWM
    for(int i=0; i<=2; i++)// Digital
      pinMode(arr[i], OUTPUT);
}

void fade(int x){// 1 color
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(x, fadeValue);         
    delay(30);                            
  }
  delay(1000);
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(x, fadeValue);         
    delay(30);                            
  }
}

void fade2(int x, int y){// 2 colores
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(x, fadeValue);   
    analogWrite(y, fadeValue);             
    delay(30);                            
  } 
  delay(1000);
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(x, fadeValue);   
    analogWrite(y, fadeValue);             
    delay(30);                            
  } 
}

void fade3(){// 3 colores
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(3, fadeValue);   
    analogWrite(6, fadeValue);     
    analogWrite(9, fadeValue);                 
    delay(30);                            
  } 
  delay(1000);
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(3, fadeValue);   
    analogWrite(6, fadeValue); 
    analogWrite(9, fadeValue);                 
    delay(30);                            
  } 
}

void loop()
{
     digitalWrite(arr[d], HIGH);
     fade(i);  
     delay(2000);
     fade2(i,c);
     delay(2000);
     fade3();
     delay(2000);
     //Cambiar pines automaticamente
     if(i==9)
       i=3;
     else
       i=i+3;
     if(c==9)
       c=3;
     else
       c=c+3;
     if(d==2)
     {
       d=0;
       for(int i=0; i<=2; i++)
         digitalWrite(arr[i], LOW);
     }  
     else
       d++;
}

It shouldn’t make a difference, but you might want to change the variable name in your for loop to something other than a global:

for(int i=0; i<=2; i++)
         digitalWrite(arr[i], LOW);

Also, are you sure it’s resetting? What happens if it’s just changing i or c to a value you don’t expect? You might want to test i>=9 instead of i==9, etc.

You might put a Serial.println() at the top of the loop and the bottom of the setup() just so you can tell what’s happening.

If all the code checks out, then think about the power being consumed - most LEDs aren’t a problem, but if you’re driving enough of them you might be taking the Arduino below what it needs to stay alive. (You could test this by disconnecting your LEDs and putting serial port debugging statements in.)

I tried out your sketch on my Uno, connected to the USB, and it works fine. Still going half an hour later. Probably a power-consumption issue like AndyCC said.

Do you have current-limiting resistors?

Well, i had 8 RGB leds conected to the 3,6,9 pins. I removed them and leave just one but still, after the first loop it resets. A curious fact is that, when I start a serial monitor the whole cycle is done, no flaws, but then i close it and again resets after the first loop..weird :astonished:, plus i changed the variable name on the for cycles like AndyCC said, just for protection.

I'll ask again: do you have current limiting resistors? If so, what value?

Also, how do you know it resets? You said "after the first loop". But after the loop executes it does it all again. So what are the symptoms exactly?

I must admit that when I tried it I threw in a Serial.println, just to see if it was working OK (then I plugged in the LEDs). But even without the Serial stuff it seems to work for me.

This is on a Uno. What board are you using?

Sorry I didn't answered your question earlier nick, I'm using a 150ohm resistor connected to the cathodes of the leds. Saying after the first loop, i know its resetting cause when using a serial monitor, the first loop starts with leds in blue, the second loop starts with green, and so on. Without the serial monitor, after showing all colors (fade3();), the led on the board blinks two times and the next loop starts with blue color. I'm using a Duemilanove board, recently bought. Thanks :)

mgradob: Well, i had 8 RGB leds conected to the 3,6,9 pins.

...

I'm using a 150ohm resistor connected to the cathodes of the leds ...

I presume you mean 8 resistors? Not "a" resistor.

I had a weird connection, which I assume was the root of my problem, yes, using 8 resistors, but now I'm using only one led, yet the problem persists, i think that a video will be better, so you can see whats happening so i leave a link here:

http://bananastudiosnews.blogspot.com/2011/06/mood-lamp.html

I hope this isn't too much of a problem to you guys, I really appreciate your support, thanks.

What LED is that? It looks pretty bright. Can you sketch or otherwise draw your circuit? That seems to be a lot more than 2 wires going to the LED.

Well yeah, the led has 4 pins: red - cathode - green - blue.
The only thing working on the breadboard is that led, all other stuff is from some other projects but aren’t connected or so.

Can you quote a part number? I'd like to look it up.

I don't have a part number for the leds, but I have some specifications:

BRIGHTNESS 10000mcd CURRENT = 5mA to 15mA = 3.7 Vdc Maximum Voltage 35 º OPENING

OK, well the thing that troubles me here is that the resistor is being shared between all 3 LEDs. So effectively as you ramp up the number of colours, the current increases.

See this for an alternative:

http://wiring.org.co/learning/basics/rgbled.html

Also here:

http://arduino.cc/playground/Main/RGBLEDPWM

And:

http://www.pjrc.com/teensy/tutorial2.html

Virtually every page I have found suggests 3 resistors, one per colour, not one like you have.

I suspect what is happening is that as more colours light up, the current flow goes up from something like 20/40/60 mA which is way over what the Arduino pin is designed to deliver, and it then resets.

I suspect what is happening is that as more colours light up, the current flow goes up from something like 20/40/60 mA which is way over what the Arduino pin is designed to deliver, and it then resets.

While one resistor is very defiantly wrong it will not cause a problem like this. What will happen is that as more LEDs are turned on and more current flows through the resistor so the voltage it drops increases. This then reduces the current through the other LEDs and reduces their brightness or even prevents them from coming on.

mgradob: Well, i had 8 RGB leds conected to the 3,6,9 pins.

Now that I re-read that, can you clarify this? That is 3 pins but you say you have 8 LEDs.

Even looking at this:

int i = 3, c = 6, b = 9, d = 0;// Pines analogicos
int arr[3] = {2,4,5};// Pines digitales

I count 6 LEDs not 8 (because d is just a counter, not a pin).

So have you got LEDs in parallel, or what?

Yes, the leds are in parallel, all leds are using the 3,6,9 pins. Just checked out the arduino with an external supply and it worked perfect. there were no resets,all leds light up in the correct sequence, so i think the usb port doesn't provide enough current for the circuit to work. I think i'll stick with this configuration. Thank you guys for all your support, i really appreciate it, you're the best!

all leds are using the 3,6,9 pins.

You know you can only draw 30mA from a pin without damaging your arduino? That is normally only enough for one or two LEDs at the most. Any more than that and you have to use a transistor.

mgradob: I think i'll stick with this configuration. Thank you guys for all your support, i really appreciate it, you're the best!

You are welcome. However you will be back in a week saying "my Arduino has stopped working" unless you follow Mike's suggestion.

So what transistor do you recommend? maybe a 2N4401?

The electronics experts are better able to answer than me, but I would recommend a (logic level) MOSFET (which is more of a switch) than a signal transistor, which is more of an analog device.

I did an example circuit here:

http://arduino.cc/forum/index.php?topic=61308

You probably want the top one which has less components. Also you don't need D1 (the diode) unless you are switching a motor or relay.

Afrotechmods does an interesting Youtube video which shows a very simple MOSFET circuit which is triggered by a interrupting a light beam:

http://www.youtube.com/user/Afrotechmods#p/u/29/BNfGoy5dqbg

Since you have so many LED paths you might want to look up an MOSFET "LED driver" (which would drive something like 8 LEDs in a single chip), or a MOSFET "array" chip.

Adafruit has a tutorial on driving multi-color LEDs using MOSFETs:

http://www.ladyada.net/products/rgbledstrip/