Multiple LED's different brightness same time

Hello all, got started playing with the arduino and uhh. Looked on the forums and havent seen really anything on this which it seems pretty simple i'm just a noob i guess and i know my code isn't very efficent. Let me know of much needed improvements.

Setup: I have 6 LED's all needing the ability to be dimmed seperately so im using the PWM channels. I'm also using an IR sensor as a switch. Right now I have it setup as once the switch/IR sensor is triggered, the first led in the string turns on dimming from off to on Then the second, and so forth. Works great but not really what im looking for. I'm looking for when the first LED is triggered it goes half way brightness (1-127 on a 1-255 analog scale), then from there the second LED does the same as the first LED but the first LED goes to full brightness while the second one has started its cycle. Then it keeps going till the last one. Let me know what you guys come up with. My Code as of now is below. Pardon my ignorance. ;D

      int led13 = 11;                // LED connected to digital pin 11
      int led12 = 10;
      int led11 = 9;
      int led10 = 6;
      int led09 = 5;
      int led08 = 3;
      
      void setup()                    // run once, when the sketch starts
      {
            
            pinMode(led13, OUTPUT);      // sets the digital pin as output
            pinMode(led12, OUTPUT);
            pinMode(led11, OUTPUT);
            pinMode(led10, OUTPUT);
            pinMode(led09, OUTPUT);
            pinMode(led08, OUTPUT);
      }
      
      void loop()                     // run over and over again
      {
            int analogValue = analogRead(0);
            int analogValue1 = analogRead(2);
            Serial.println(analogValue);
            Serial.println(analogValue1);
            delay(10);
            if (analogValue > 500) {
                  for(int fadeValue = 0; fadeValue <= 255; fadeValue += 5){
                        analogWrite(led13, fadeValue);   
                        delay(30);                               
                  }
                  for(int fadeValue = 0; fadeValue <= 255; fadeValue += 5){      
                        analogWrite(led12, fadeValue);    
                        delay(30); 
                         }

oh ya and this code shows two analog inputs. I'm really only using 1, forgot about that. :-[

The BlinkWithoutDelay example will provide some inspiration and direction.

Basically, instead of setting an alarm and going to sleep (delay()), you keep track of when it is time to do things. Periodically, you check your watch (millis()), and see if it is time to do any of the things that might need to be done.

The things you have identified that you want to do are to change the PWM value of 6 different pins. All the pins change value at the same time (or near enough). The value of each of the pins is going to to be different. The easiest way to accomplish this is to have an array of pin values and pin numbers. On each pass though loop, see if it is time to change the pin values and write the new values to the pins. If it is, update the values, and write them to the pins.

Of course the pin value for any one pin will depend on the pin value of the preceding pin, except for the first one.

Hopefully, this will give you enough direction to get started. If you have problems getting it working, post your code and describe your problems, and we'll try yo help.

Sorry made this code up before you posted. This is a down a dirty code that works but my lord is it basic. It works just not exactly what I want lol. The amount of code is crazy for this little application. I should make a varible for the delay so i can change it easily.

      int led13 = 11;                // LED connected to digital pin 13
      int led12 = 10;
      int led11 = 9;
      int led10 = 6;
      int led09 = 5;
      int led08 = 3;
      int fade0 = 0;
      int fade1 = 10;
      int fade2 = 20;
      int fade3 = 30;
      int fade4 = 40;
      int fade5 = 50;
      int fade6 = 60;
      int fade7 = 70;
      int fade8 = 80;
      int fade9 = 90;
      int fade10 = 100;
      int fade11 = 110;
      int fade12 = 120;
      int fade13 = 130;
      int fade14 = 140;
      int fade15 = 150;
      int fade16 = 160;
      int fade17 = 170;
      int fade18 = 180;
      int fade19 = 190;
      int fade20 = 200;
      int fade21 = 210;
      int fade22 = 220;
      int fade23 = 230;
      int fade24 = 240;
      int fade25 = 250;
      int fade26 = 255;
            
      void setup()                    // run once, when the sketch starts
      {
            
            pinMode(led13, OUTPUT);      // sets the digital pin as output
            pinMode(led12, OUTPUT);
            pinMode(led11, OUTPUT);
            pinMode(led10, OUTPUT);
            pinMode(led09, OUTPUT);
            pinMode(led08, OUTPUT);
      }
      
      void loop()                     // run over and over again
      {
            int analogValue = analogRead(0);
            int analogValue1 = analogRead(2);
            Serial.println(analogValue);
            Serial.println(analogValue1);
            delay(10);
            if (analogValue > 500) {
                  analogWrite(led13, fade0);      // sets the LED on
                  delay(10);
                  analogWrite(led13, fade1);      // waits for a second
                  delay(10);
                  analogWrite(led13, fade2);
                  delay(10);
                  analogWrite(led13, fade3);
                  delay(10);
                  analogWrite(led13, fade4);
                  delay(10);
                  analogWrite(led13, fade5);
                  delay(10);
                  analogWrite(led13, fade6);
                  delay(10);
                  analogWrite(led13, fade7);
                  delay(10);
                  analogWrite(led13, fade8);
                  delay(10);
                  analogWrite(led13, fade9);
                  delay(10);
                  analogWrite(led13, fade10);
                  delay(10);
                  analogWrite(led13, fade11);
                  delay(10);
                  analogWrite(led13, fade12);
                  delay(10);
                  analogWrite(led13, fade13);
                  delay(10);
                  analogWrite(led12, fade0);
                  delay(10);
                  analogWrite(led13, fade14);
                  delay(10);
                  analogWrite(led12, fade1);
                  delay(10);
                  analogWrite(led13, fade15);
                  delay(10);
                  analogWrite(led12, fade2);
                  delay(10);
                  analogWrite(led13, fade16);
                  delay(10);
                  analogWrite(led12, fade3);
                  delay(10);
                  analogWrite(led13, fade17);
                  delay(10);
                  analogWrite(led12, fade4);
                  delay(10);
                  analogWrite(led13, fade18);
                  delay(10);
                  analogWrite(led12, fade5);
                  delay(10);
                  analogWrite(led13, fade19);
                  delay(10);
                  analogWrite(led12, fade6);
                  delay(10);
                  analogWrite(led13, fade20);
                  delay(10);
                  analogWrite(led12, fade7);
                  delay(10);
                  analogWrite(led13, fade21);
                  delay(10);
                  analogWrite(led12, fade8);
                  delay(10);
                  analogWrite(led13, fade22);
                  delay(10);
                  analogWrite(led12, fade9);
                  delay(10);
                  analogWrite(led13, fade23);
                  delay(10);
                  analogWrite(led12, fade10);
                  delay(10);
                  analogWrite(led13, fade24);
                  delay(10);
                  analogWrite(led12, fade11);
                  delay(10);
                  analogWrite(led13, fade25);
                  delay(10);
                  analogWrite(led12, fade12);
                  delay(10);
                  analogWrite(led13, fade26);
                  delay(10);
                  analogWrite(led12, fade13);
                  delay(10);

I'm looking at the blink without delay code, confusing me a lot, how would i incorporate that? I see how it works but would i still be able to use my dimmer code?

for(int fadeValue = 0; fadeValue <= 255; fadeValue += 5){
                  analogWrite(led08, fadeValue);    // sets the LED on
                  delay(30);

If im seeing this correctly. after my if/switch code, it would start counting milli's then at say 0 millis it starts my first dimming code that would take 1000ms to complete and then have the next cycle be at 500ms. Sorry im just confused on how to define it. is there a way to reference all my dimming codes then reference them by just using a timmer after the switch? Uhhh how i hate code. I'm way off.

Suppose you were dimming the LED manually, using a potentiometer. Suppose the brightness needed to change every hour. You have a watch.

You'd set the potentiometer, and write down the time you last changed the potentiometer. Every so often, you'd check your watch to see if it was time to change the potentiometer setting. If it was, you would move the potentiometer, and write down the new time.

Well, on the Arduino, the "every so often" is every pass through loop. Checking your watch is calling millis(). The other steps are pretty obvious.

The BlinkWithDelay example changes the state of the LED, from on to off or from off to on. What you want to do is change the brightness of the led.

int increment = +1;
int pwmVal = 0;
unsigned long lastChange = 0;
unsigned long interval = 100;

void loop()
{
   unsigned long now = millis();
   if(now - lastChange > interval) // See if it's time
   {
      pwmVal += increment; // Set the new value
      analogWrite(pwmPin, pwmVal); // Write the new value

      // If we are at maximum or minimum, change direction
      if(pwmVal == 255)
         increment = -1;
      if(pwmVal == 0)
         increment = +1;

      lastChange = now;
   }
}

Z3RO - to learn programming means to read other persons code :slight_smile:
Here is some I just made up - untested!!! You might find bugs :slight_smile:

const int maxLed = 5;    // the number of LEDs in the bar graph minus 1

byte ledPins[] = { 3, 5, 6, 9,10,11 };   // pin numbers of dimmable LEDs 
byte activeLed = 0;

void setup() {
  }

byte activeDim = 0;
void loop() {
 
      if (activeDim<255)
          digitalWrite(ledPins[activeLed], ++activeDim); 
          
      if (activeDim<128 && activeLed>0) 
          digitalWrite(ledPins[activeLed-1], 128+activeDim); 
 
      if (activeDim==128 && activeLed<maxLed ) {
          ++activeLed;
          activeDim=0;
         }
      delay(4);   
  }

Wow, Thanks a lot you put that in to perspective. I get what its trying to do now and whats wrong with my terribly written code. I'm going to play with it and see what I come up with. Makes a lot of since now. I'll post my code once its all said and done. Thanks a lot!

Alright so two issues im running into. My code before, when the sensor went above 500 it stopped readingAnalog and did the sequence. As of now you have to continually be breaking the beam for it brighten. My previous code acted more as a switch. Not seeing the difference between the two codes with the "if" commands. Also, how does this help me with having one light start brightning at the midway point of the brightness of the previous LED. Wouldnt you have to define something if this led PWM gets to 125 then engage the next led? or would you just use timers for each LED's cycle. first led starts at 1 and takes 1000ms to complete its full brightness and then the next one would start at 500 so its starting halfway from the previous LED's cycle. See code below...

      int led13 = 11;                // LED connected to digital pin 13
      int led12 = 10;
      int led11 = 9;
      int led10 = 6;
      int led09 = 5;
      int led08 = 3;
      int increment = +5;
      int increment1 = -5;
      int pwmVal = 0;
      long lastChange = 0;
      long interval = 100;
      
      void setup()                    // run once, when the sketch starts
      {
            
            pinMode(led13, OUTPUT);      // sets the digital pin as output
            pinMode(led12, OUTPUT);
            pinMode(led11, OUTPUT);
            pinMode(led10, OUTPUT);
            pinMode(led09, OUTPUT);
            pinMode(led08, OUTPUT);
      }
      
      void loop()                     // run over and over again
      {
            int analogValue = analogRead(0);
            int analogValue1 = analogRead(2);
            Serial.println(analogValue);
            Serial.println(analogValue1);
            delay(10);
            if (analogValue > 500){
                  long now = millis();
                  if (now - lastChange > interval)
                        pwmVal += increment;
                  analogWrite(led13, pwmVal);
                  if(pwmVal == 255)
                        delay(1000);
            }
            if (analogValue1 > 500){
                  long now = millis();
                  if(now - lastChange > interval)
                        pwmVal -= increment1;
                        analogWrite(led13, pwmVal);
                        if(pwmVal == 0)
                        delay(1000);

The whole idea behind using millis() is to allow "simultaneous" actions to occurs. Putting delay in there defeats the whole purpose.

If you only want to read the sensor again when the whole 6 LED fading cycle is complete, that is possible. Set a boolean variable to true when a reading needs to be taken (initialized to true). Take a reading only when one is needed (the variable is true). When the fading cycle starts, set that variable to false. Set it back to true when the fading cycle is complete.

However, if you want the sensor to act as a switch, and turn the entire fading cycle on and have it run to completion, forget about millis and not using delay.

Create a single for loop that sets the appropriate value for all 6 pins, write the pin values, and delay for however long you want.

I wonder whether you have already spotted the laughable bugs in my code snippet above? ;D
After corrections it works fine here!

I also added the 10 lines to check your on/off condition and switch off the leds. It is not so difficult to not only switch them off but dim them down in reverse order, the simplest way is to copy the code....

Thanks guys, Was playing around with the code with millis and got it to work but the LED's I'm using are apparently not sensitive enough to the code. It basically seems like it has 5 different levels with that code. I ran my basic code through it and it seems to work better. I have to get some different ones anyways just because of color variances. I havent tried out your code yet deSilva, I'll give it both of these another try with the new LED's

It basically seems like it has 5 different levels with that code.

Which code would that be?

I ran my basic code through it and it seems to work better.

"it"? What is "it"?

I have to get some different ones anyways just because of color variances.

There will always be color variances. You need to adjust the size of the resistor in line with each leg of each LED to even things out, or use some sort of diffuser.

The really basic code which calls a delay to each dim setting. Seems to be the most consistant.

      int led13 = 11;                // LED connected to digital pin 13
      int led12 = 10;
      int led11 = 9;
      int led10 = 6;
      int led09 = 5;
      int led08 = 3;
      int fade0 = 0;
      int fade1 = 10;
      int fade2 = 20;
      int fade3 = 30;
      int fade4 = 40;
      int fade5 = 50;
      int fade6 = 60;
      int fade7 = 70;
      int fade8 = 80;
      int fade9 = 90;
      int fade10 = 100;
      int fade11 = 110;
      int fade12 = 120;
      int fade13 = 130;
      int fade14 = 140;
      int fade15 = 150;
      int fade16 = 160;
      int fade17 = 170;
      int fade18 = 180;
      int fade19 = 190;
      int fade20 = 200;
      int fade21 = 210;
      int fade22 = 220;
      int fade23 = 230;
      int fade24 = 240;
      int fade25 = 250;
      int fade26 = 255;

      void setup()                    // run once, when the sketch starts
      {

            pinMode(led13, OUTPUT);      // sets the digital pin as output
            pinMode(led12, OUTPUT);
            pinMode(led11, OUTPUT);
            pinMode(led10, OUTPUT);
            pinMode(led09, OUTPUT);
            pinMode(led08, OUTPUT);
      }

      void loop()                     // run over and over again
      {
            int analogValue = analogRead(0);
            int analogValue1 = analogRead(2);
            Serial.println(analogValue);
            Serial.println(analogValue1);
            delay(10);
            if (analogValue > 500) {
                  analogWrite(led13, fade0);      // sets the LED on
                  delay(10);
                  analogWrite(led13, fade1);      // waits for a second
                  delay(10);
                  analogWrite(led13, fade2);
                  delay(10);
                  analogWrite(led13, fade3);
                  delay(10);
                  analogWrite(led13, fade4);
                  delay(10);
                  analogWrite(led13, fade5);
                  delay(10);
                  analogWrite(led13, fade6);
                  delay(10);
                  analogWrite(led13, fade7);
                  delay(10);
                  analogWrite(led13, fade8);
                  delay(10);
                  analogWrite(led13, fade9);
                  delay(10);
                  analogWrite(led13, fade10);
                  delay(10);
                  analogWrite(led13, fade11);
                  delay(10);
                  analogWrite(led13, fade12);
                  delay(10);
                  analogWrite(led13, fade13);
                  delay(10);
                  analogWrite(led12, fade0);
                  delay(10);
                  analogWrite(led13, fade14);
                  delay(10);
                  analogWrite(led12, fade1);
                  delay(10);
                  analogWrite(led13, fade15);
                  delay(10);
                  analogWrite(led12, fade2);
                  delay(10);
                  analogWrite(led13, fade16);
                  delay(10);
                  analogWrite(led12, fade3);
                  delay(10);
                  analogWrite(led13, fade17);
                  delay(10);
                  analogWrite(led12, fade4);
                  delay(10);
                  analogWrite(led13, fade18);
                  delay(10);
                  analogWrite(led12, fade5);
                  delay(10);
                  analogWrite(led13, fade19);
                  delay(10);
                  analogWrite(led12, fade6);
                  delay(10);
                  analogWrite(led13, fade20);
                  delay(10);
                  analogWrite(led12, fade7);
                  delay(10);
                  analogWrite(led13, fade21);
                  delay(10);
                  analogWrite(led12, fade8);
                  delay(10);
                  analogWrite(led13, fade22);
                  delay(10);
                  analogWrite(led12, fade9);
                  delay(10);
                  analogWrite(led13, fade23);
                  delay(10);
                  analogWrite(led12, fade10);
                  delay(10);
                  analogWrite(led13, fade24);
                  delay(10);
                  analogWrite(led12, fade11);
                  delay(10);
                  analogWrite(led13, fade25);
                  delay(10);
                  analogWrite(led12, fade12);
                  delay(10);
                  analogWrite(led13, fade26);
                  delay(10);
                  analogWrite(led12, fade13);
                  delay(10);

"it" being the dimming consistancy, it seemed with the timed feature it was a little laggy and not a fluid of a dimming.

By color variancies I mean I have like 2 green 4 Red LED's. I just need to find a good dimmable soft white LED.

Also another question. Say I wanted to do more then the 6 LED's say I wanted 12, with the ability to dim all seperatly using the 6 PWM ports that the Arduino has or using a breakout board of some sort or another plug and play device. What do you think would be my best option with still having the controll over all the single LED's? Would like to here what comes around. Been seeing some other projects like LED matrixing and LED cubes with seperate controlls for each but more or less on and off with groups. Let me know what you guys come up with. :wink: