blinking and fading LED lights at same time

hello guys, i'am new to arduino UNO family as i 'am still in beginners level i tried to blink 2 LED and fade another LED at single programming. i succeeded in blinking the LED's but same as usual fade doesn't appear to be perfect. It doesn't fade in given delay() seconds please help me.

programme:

int pin1 = 7;
int pin2 = 8;
int pin3 = 5;
int brightness = 0;
int fadeAmount = 5;

void setup()
{
pinMode (pin1,OUTPUT);
pinMode (pin2,OUTPUT);
pinMode (pin3,OUTPUT);
}

void loop()

{

brightness = brightness + fadeAmount;

if (brightness<=0 || brightness >=200)

{
fadeAmount = -fadeAmount;
}

analogWrite(pin3,brightness);
delay(300);

digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
delay(500);
digitalWrite(pin1,LOW);
digitalWrite(pin2,HIGH);
delay(500);

}

At the moment you are fading LED3 by one "fadeAmount" then waiting for 300ms plus the time taken for the other LEDs to blink on then off. Only then does it loop back again and fade by one more "fadeAmount". So fading up and down takes a long time.

Have a look at the Blink without Delay example sketch and apply that method to your blinking and fading.

Steve

Please read How to use the forum and please edit your post accordingly (especially the part about code-tags ;))

And it's the same problem as seen a million times here and it's the delay(). A delay() does exactly that, it stops EVERYTHING dead in it's tracks and just waits. So the fading part will have to wait for the delay()'s in the blinking part. So as I said many times before, chapter two of every Arduino howto should be called "NEVER use delay() again!". See Blink without delay for that.

And if you want easy fading and even have a nicer fading because of gamma correction, see the FadeLed-library in my footer. But even then, you need to call update() as often as possible so again, NO delay()'s in the rest of the code.

Welcome to this forum, It’s a little tricky at first to use, but you will soon work it out.

As for LED’s. normally to control the brightness of LED’s, you turn it on and off rapidly, changing the duration of the off period to reduce the brightness. If the on and off periods are equal, then the LED will be ½ as bright, cycling though the loop quickly! Examples below.

// LED 1 On @ ½ max brightness.
  digitalWrite(pin1,HIGH);
  delay(8);
  digitalWrite(pin1,LOW);
  delay(8); 

// LED 1 On @ 3/4 max brightness.
  digitalWrite(pin1,HIGH);
  delay(6);
  digitalWrite(pin1,LOW);
  delay(8); 

// LED 1 On @ 1/4 max brightness.
  digitalWrite(pin1,HIGH);
  delay(2);
  digitalWrite(pin1,LOW);
  delay(8); 

// LED 1 On @ 100% max brightness.
  digitalWrite(pin1,HIGH);
  delay(8);
  digitalWrite(pin1,LOW);
  delay(0);

Yeahhhhhh, just don't do that until you really need software PWM. ::slight_smile: And even then delay() is terrible. A standard Arduino Uno has 6 PWM pins that just do that but in hardware.

Pretty sure I just read an identical question a couple days ago. Must've been another, less-procrastinatey classmate. Try using the search.

So here is an example, not quite want you want but on the way. Move LED 3 to Output #9. (digital)
This code should reduce the brightness to below 1/2 way than back up again over 500 ms.. try playing with the values of "Brightness and "FadeAmont".

int pin1 = 7;
int pin2 = 8;
int pin3 = 9;//Move this LED from analog.
int brightness = 25;
int fadeAmount = 1;

void setup()
{
  pinMode (pin1, OUTPUT);
  pinMode (pin2, OUTPUT);
  pinMode (pin3, OUTPUT);
}

void loop(){
  digitalWrite(pin1, HIGH);
  digitalWrite(pin2, LOW);

  while (brightness > 0) {// Fade Out
    digitalWrite(pin3, HIGH);
    delay(brightness);
    digitalWrite(pin3, LOW);
    delay(fadeAmount);
    brightness -= fadeAmount;
  }

  digitalWrite(pin1, LOW);
  digitalWrite(pin2, HIGH);
  brightness = 0;
  
  while (brightness < 26) {// Fade In
    digitalWrite(pin3, HIGH);
    delay(brightness);
    digitalWrite(pin3, LOW);
    delay(fadeAmount);
    brightness += fadeAmount;
  }
}

Again, don't use that... Not if you want to pass your assignment anyway...

guys i'am thankful for u all for response. special thanks to chrisbee for providing me coding.

@ragul1230, but please don't use that.... It will help you nothing and will get you a 1 / F-....

@septillion as u said the code is not perfectly matching to fade the LED at uniform...just same as it just blinks off the LED lights.

That's because it's also blocking code and doing something in software which you can do with a peripheral of the Arduino namely a PWM pin.

Just use Blink without delay for the blinking of the led. Once you have that, you can do more stuff in the loop() like blinking.

For that, two options:

  1. Do it yourself. For the timing, also use millis() instead of delay().

  2. Be lazy and grab a library like FadeLed.

i got the point how to do thanks for ur help guys

Apparently he did not...

And he's the subborn type. I added the code-tags myself

ragul1230:
As u said i tried mills() function bu again and again it doesn't work on my UNO BOARD.

int pin1 = 9; 

int pin2 = 10;
int pin3 = 11;
int pin4 = 7;      
int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
int fadeAmount1 = 5;
int fadeAmount2 = 5;
int fadeAmount3 = 5;

void setup()
{
 
 pinMode(pin1,OUTPUT);
 pinMode(pin2,OUTPUT);
 pinMode(pin3,OUTPUT);
 pinMode(pin4,OUTPUT);
}

void loop() {

analogWrite(pin1, brightness1);
 analogWrite(pin2, brightness2);
 analogWrite(pin3, brightness3);
 digitalWrite(pin4,HIGH);
 delay(200);
 digitalWrite(pin4,LOW);
 delay(200);

brightness1 = brightness1 + fadeAmount1;
 brightness2 = brightness2 + fadeAmount2;
 brightness3 = brightness3 + fadeAmount3;

if (brightness1 <= 0 || brightness1 >= 255)
 if (brightness2 <= 0 || brightness2 >= 200)
 if (brightness3 <= 0 || brightness3 >= 100)
   
 
 {
   fadeAmount1 = -fadeAmount1;
   fadeAmount2 = -fadeAmount2;
   fadeAmount3 = -fadeAmount3;
   
 }

int millis(30);
}

@ragul1230, that's simple not how millis() works. Millis() doesn't take an argument, it returns the time (in ms) since the Arduino turned on. You should have known if you read the Blink without delay as I suggested you. But then again, you should know about code-tags as well if you read How to use the forum as I told you... ::slight_smile: