fade LEDs

Hi,

Sorry for opening a new thread. I think in my old thread my problem description was a bit confusing.

I have two LEDs and want to fade them out. But want the 2nd LED to start fading out when the first is about half way faded out.

I really don't know how to do this.

Any help please..........

Look in the tutorials for the "blink without delay" example, and "fade", instead of "blink".

I know how to fade them, but not how to start fading the 2nd one while the first one is still fading.

Maybe you can post some code you're using to fade one LED. That way we can help you fade the second one in the most similar method.

#define NUMLEDS 2
#define BUTTON 13

int val = 0;
int oldVal = 0;
int state = 0;
int led[NUMLEDS]={11,10};

void setup(){

for(int i = 0; i<NUMLEDS; i++){
pinMode(led*,OUTPUT);*

  • }*
  • pinMode(BUTTON,INPUT);*
  • Serial.begin(9600);*
    }
    void loop(){
  • val = digitalRead(BUTTON);*
  • Serial.println(val);*
  • if((val==HIGH)&&(oldVal==LOW)){*
  • state=1-state;*
  • delay(10);*
  • }*
  • oldVal=val;*
  • if(state==1){*
  • for(int i = 0; i<NUMLEDS; i++){*
    _ digitalWrite(led*,HIGH);*_

* }*
* delay(3000);*
_ for(int i = 2*255; i>=0; i–){_

* fading1(i);*
* //Serial.println(i);*
* }*
* state=0;*
* }*
* else{*
* for(int i = 0; i<6; i++){*
_ digitalWrite(led*,LOW);
}
}
}*
void fading1(int i){_

* int value1 = i-255;*
* int value2 = i;*

* analogWrite(led[0],value1);*
* Serial.println(value1);*

* if (value1 < 128){*
* analogWrite(led[1],value2);*
* }*
* if (value1<1){*
* value1=0;*
* } *

* delay(10);*
}

for (int light = 255; light <=0; --light) {
  analogWrite (LED1_PIN, light);
  if (light < 128) {
    analogWrite (LED2_PIN, light * 2);
  }
  delay (100);
}

thanks.

now the 2nd LED starts fading when the 1st LED reaches 128 but unfortunately they reach 0 at the same time.

When the 1st LED is at 0 I want the 2nd to be at 128 fading down to 0.

I've copied the essential parts of your code and used it as a base for this code that seems to do what you requested.

int led[]={5,6};

void setup() {
  Serial.begin(9600);
  pinMode(led[0], OUTPUT);
  pinMode(led[1], OUTPUT);
}

void loop() {
  for(int i=255 + 127; i>=0; i--) {
    fading1(i);
  }
}

void fading1(int i) {
  int value1 = min(255, max(0, i));
  int value2 = min(255, max(0, i - 127));
  
  analogWrite(led[0], value1);
  Serial.println(value1);
  
  analogWrite(led[1], value2);
  Serial.println(value2);
  
  delay(10);
}

P.S. if you put your code between [ code ] and [ /code ] it's easier read

hey thank you so much. that did it. min() and max() just wonderful.

Just be aware when using min/max that it might seem reversed in some cases.

This one is straight forward: int c = max(a, b); // c becomes the greater of a and b

But in this case, it's a bit odd: int c = min(255, a); // c becomes a, with a maximum of 255 int d = max(0, b); // d becomes b, with a minimum of 0