Arduino led`s going crazy

Hi, guys. I am new to arduino and i have some questions. So thanks for understanding and help. I started my arduino controlled Christmas lights, and everything is going nice until i wrote a code. Actually, part of my code is not running as I imagined, it`s the blinking. Here is example

analogWrite(blue, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

analogWrite(white, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

analogWrite(green, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

When i run code on arduino, led`s are going crazy. When this part is done, everything is going OK.

What have I done wrong ? Please help. Thank you.

What you've done wrong is fail to post the complete sketch so there's no way to know what's going on.

What have I done wrong ?

You have not showed use all your code.
To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

nenadicp: What have I done wrong ?

You've only posted a tiny piece of the code. Not nearly enough to tell what your problem is. Post the whole thing. Use code tags when you do so we can read it without our browsers mangling it.

You should probably also give us a good description of what

led`s are going crazy

means exactly. Are they sitting in the corner crying?

Sorry, guys, here is the code.

int red = 11; int blue = 10; int white = 9; int green = 6; int brightness = 0; int fadeAmount = 5;

void setup() { pinMode(red, OUTPUT); pinMode(blue, OUTPUT); pinMode(white, OUTPUT); pinMode(green, OUTPUT); }

void loop() { digitalWrite(red, HIGH); delay(1000); digitalWrite(blue, HIGH); delay(1000); digitalWrite(white, HIGH); delay(1000); digitalWrite(green, HIGH); delay(1000);

digitalWrite(red, LOW); delay(1000); digitalWrite(blue, LOW); delay(1000); digitalWrite(white, LOW); delay(1000); digitalWrite(green, LOW); delay(1000);

analogWrite(red, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

analogWrite(blue, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

analogWrite(white, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

analogWrite(green, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount; } delay(200);

digitalWrite(red, HIGH); digitalWrite(blue, LOW); digitalWrite(white, HIGH); digitalWrite(green, LOW); delay(500);

digitalWrite(red, LOW); digitalWrite(blue, HIGH); digitalWrite(white, LOW); digitalWrite(green, HIGH); delay(500);

digitalWrite(red, HIGH); digitalWrite(blue, HIGH); digitalWrite(white, LOW); digitalWrite(green, LOW); delay(500);

digitalWrite(red, HIGH); digitalWrite(blue, LOW); digitalWrite(white, LOW); digitalWrite(green, HIGH); delay(500);

digitalWrite(red, LOW); digitalWrite(blue, HIGH); digitalWrite(white, HIGH); digitalWrite(green, LOW); delay(500);

digitalWrite(red, LOW); digitalWrite(blue, HIGH); digitalWrite(white, LOW); digitalWrite(green, HIGH); delay(500);

digitalWrite(red, LOW); digitalWrite(blue, LOW); digitalWrite(white, HIGH); digitalWrite(green, HIGH); delay(500);

digitalWrite(red, HIGH); delay(300); digitalWrite(red, LOW); delay(200); digitalWrite(blue, HIGH); delay(300); digitalWrite(blue, LOW); delay(200); digitalWrite(white, HIGH); delay(300); digitalWrite(white, LOW); delay(200); digitalWrite(green, HIGH); delay(300); digitalWrite(green, LOW); delay(500);

digitalWrite(red, HIGH); delay(300); digitalWrite(green, HIGH); delay(300); digitalWrite(white, HIGH); delay(300); digitalWrite(blue, HIGH); delay(500); digitalWrite(red, LOW); delay(300); digitalWrite(green, LOW); delay(300); digitalWrite(white, LOW); delay(300); digitalWrite(blue, LOW); delay(500);

digitalWrite(red, HIGH); delay(300); digitalWrite(green, LOW); delay(300); digitalWrite(white, HIGH); delay(300); digitalWrite(blue, LOW); delay(500);

digitalWrite(red, LOW); delay(300); digitalWrite(green, HIGH); delay(300); digitalWrite(white, LOW); delay(300); digitalWrite(blue, HIGH); delay(500);

digitalWrite(red, HIGH); delay(300); digitalWrite(green, HIGH); delay(300); digitalWrite(white, HIGH); delay(300); digitalWrite(blue, HIGH);

analogWrite(green, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255){ fadeAmount = -fadeAmount; } delay(200);

analogWrite(red, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255){ fadeAmount = -fadeAmount; } delay(200);

analogWrite(blue, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255){ fadeAmount = -fadeAmount; } delay(200);

analogWrite(white, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255){ fadeAmount = -fadeAmount; } delay(200);

digitalWrite(red, HIGH); delay(300); digitalWrite(white, HIGH); delay(200); digitalWrite(red, LOW); delay(300); digitalWrite(white, LOW); delay(200); digitalWrite(blue, HIGH); delay(300); digitalWrite(green, HIGH); delay(200); digitalWrite(blue, LOW); delay(300); digitalWrite(white, LOW); delay(200);

digitalWrite(red, HIGH); delay(100); digitalWrite(white, HIGH); delay(50); digitalWrite(red, LOW); delay(100); digitalWrite(white, LOW); delay(50); digitalWrite(blue, HIGH); delay(100); digitalWrite(green, HIGH); delay(50); digitalWrite(blue, LOW); delay(100); digitalWrite(white, LOW); delay(50);

digitalWrite(red, HIGH); delay(400); digitalWrite(white, HIGH); delay(400); digitalWrite(blue, HIGH); delay(400); digitalWrite(green, HIGH); delay(400); digitalWrite(red, LOW); delay(400); digitalWrite(white, LOW); delay(400); digitalWrite(blue, LOW); delay(400); digitalWrite(green, LOW); delay(400);

digitalWrite(green, HIGH); delay(400); digitalWrite(blue, HIGH); delay(400); digitalWrite(white, HIGH); delay(400); digitalWrite(red, HIGH); delay(400); digitalWrite(green, LOW); delay(400); digitalWrite(blue, LOW); delay(400); digitalWrite(white, LOW); delay(400); digitalWrite(red, LOW); delay(400);

analogWrite(red, brightness); analogWrite(blue, brightness); analogWrite(white, brightness); analogWrite(green, brightness);

brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255){ fadeAmount = -fadeAmount; } delay(200);

if (brightness == 255 || brightness == 0){ fadeAmount = -fadeAmount; } delay(200); }

You've got that test for brightness being at its min or max at the end twice, so if it is at min or max it gets reversed twice.

Can you answer my other question? What does "leds going crazy" mean? Are they chasing you with a knife? If so that's not the code, that's demonic possession. Should be addressed on a different forum.

Led`s are doing fast blink at that part of code. What i want them to do is fade, and move on.

OK, I assume this is where you want them to fade:

  analogWrite(green, brightness);
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255){
    fadeAmount = -fadeAmount;
  }
  delay(200);

But that doesn’t create a fade effect. It just writes one PWM value, brightness, to the pins and then adds to brightness and moves on. You want it to repeat that part for all the PWM values and then move on. You need a for loop.

for(int i = 0; i <=255; i++){
     analogWrite(ledpin, i);
     delay(10);
}

The for loop says start with this variable i at 0 and keep repeating what’s between the brackets adding one to i every time until as long as i is less than or equal to 255.

What you have seems to be hacked out of a sketch we’re all familiar with, but all it does is fade a led and it relies on the fact that loop is repeating to keep repeating the analogWrite with different values. That’s not going to work here because you have more stuff in your loop that you don’t want to repeat along with it. So you need a loop within the loop, that’s what the for loop is.

  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount;
  }

That’s pretty risky code - it relies on fadeAmount dividing evenly into 255 to guarantee
catching each endcase.

You should be using an inequality:

  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }

But even better is to separate the tests so that the correct sign of fadeAmount is guaranteed
in the face of repeating the test immediately:

  brightness = brightness + fadeAmount;
  if (brightness <= 0)
    fadeAmount = 5 ;
  else if (brightness >= 255)
    fadeAmount = -5 ;

or even:

  brightness = brightness + fadeAmount;
  if (brightness <= 0)
    fadeAmount = abs (fadeAmount) ;
  else if (brightness >= 255)
    fadeAmount = -abs (fadeAmount) ;

Except that if you read the rest of the thread you'll see he needs to throw all of that away anyway.