1

I have two UV leds, one Vf 3.3V @ 150 mA and the second one Vf 5V @ 150 mA. I need a circuit to make them fade in and out alternatively, i.e., as one goes dimmer the other goes brighter and vice versa. I tried an Arduino PWM transistor approach with the Attiny85 board and the code below and it works, but I need the ledPin2 led to work with at a lower maxbrightness. How can I modify the code so that I can set the maxbrightness for ledPin2 to be around 25, while the maxbrightness for ledPin1 remains 255 and yet both take the same time to fade in and out?

int ledPin1 = 0;       
int ledPin2 = 4;         
int minbrightness = 0;     
int maxbrightness = 255;    
int fadeAmount = 1;   
int tim=4;
    
void setup()  { 
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
} 
    
void loop()  {
  analogWrite(ledPin1, minbrightness);
  analogWrite(ledPin2, maxbrightness);
  minbrightness = minbrightness + fadeAmount;
  maxbrightness = maxbrightness - fadeAmount;
  if (minbrightness == 0 || minbrightness == 255 ){
    fadeAmount = -fadeAmount ;
  }
  delay(tim);
}
    
2
  • Why not make the change in hardware? Much simpler and no need to change any code. If you really don't want to, why not simply try putting maxbrightness/10 into the analog write for the second LED? You might have to change the variable to a float to do it in the approximately similar time (otherwise you'll only really change a value for the brightness of the second one approximately every 5 counts).
    – Nick S.
    Nov 11, 2023 at 1:42
  • I'd like a code solution so it's easier to use it in another configurations. Also, if I just divide maxbrightness by 10, I am afraid the leds might get out of synch after some cycles.
    – Rodrigo
    Nov 11, 2023 at 16:28

1 Answer 1

0

I don't think you have much choice than to come up with a sort of a counter and instead of having a single variable for the brightness of all the LEDs, you'll have to maintain a separate brightness variable for each channel which will have its own unique dimming profile. You'll then have to empirically determine how many counts it'll take for you to decrease/increase the PWM duty cycle on each separate channel (since perceived brightness isn't linear).

Something like this? The brightness of 32 and a modulo 8 was chosen to be a factor of 256, so it's more or less synced.

int ledPin1 = 0;       
int ledPin2 = 4;            
int brightnessLP1 = 0;  //begin with the LED off
int brightnessLP2 = 32; //begin with the LED on
int fadeAmount = 1;   
int tim=4;
    
void setup()  { 
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);
} 
    
void loop()  {
  //counter value accommodates the modulo below
  for(int brightnessCounter = 1; brightnessCounter < 257; brightnessCounter++) { 
     
     analogWrite(ledPin1, brightnessLP1);
     analogWrite(ledPin2, brightnessLP2);
     brightnessLP1 = brightnessLP1 + fadeAmount; 
     if(brightnessCounter  % 8) {
       brightnessLP2 = brightnessLP2 - fadeAmount;
     }
     delay(tim);
  }
  fadeAmount = -fadeAmount;
}

I'm sure you could make it cleaner or link the brightness values to the counter, but I'll leave that to you.

2
  • Thank you. I can work from there.
    – Rodrigo
    Nov 11, 2023 at 21:38
  • Of course, you're welcome!
    – Nick S.
    Nov 11, 2023 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.