6 Leds with fade and lots of fun

Hello everybody,

I am running this code:

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int leds[] = {2, 3, 4, 5, 6, 7};  // the pin that the LED is attached to
int brightness[] = {0, 0, 0, 0, 0, 0};  // how bright the LEDs are
int fadeAmount = 5;    // how many points to fade the LED by
int a = 0;  // zaehlvariable

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 2-7 to be an output:
  for (int x = 2; x < 8; x++){
  pinMode(x, OUTPUT);
  }
} 

// the loop routine runs over and over again forever:
void loop()  {
  
  if (a == 7){
    a = 0;
  }
  // set the brightness
  analogWrite(leds[a], brightness[a]);    

  // change the brightness for next time through the loop:
  brightness[a] = brightness[a] + fadeAmount;

  // reset the brightness at the end of the fade: 
  if (brightness[a] == 255) {
    brightness[a] = 0; 
  }
  a = a + 1;  
  // wait to see the dimming effect    
  delay(10);                            
}

and it works perfectly fine and as intended, however if I change the start values of the brightness array to for example:

brightness[] = {0, 50, 100, 150, 200, 250}

it won't work any more. All the Leds fade to as bright as possible and nothing else happens. I tried to get the values through the serial monitor but what I get is very confusing. I get the proper values with some strange numbers inbetween:

5
5
5
5
5
6
526969
10
10
10
10
10
6
526969
15
15
15
15
15
6
526969
20
20
20
20
20
6
526969

etc..

And when I change the brightness values slightly I get numbers like these:

2
165
165
155
255
155
160
2
325
325
315
415
assertion "duty <= pPwm->PWM_CH_NUM[ul_channel].PWM_CPRD" failed: file "../source/pwmc.c", line 272, function: PWMC_SetDutyCycle
Exiting with status 1.

Which produce the error above (I guess the numbers are too high but I don't get it why).

I am using an Arduino Due with the 1.5.2 IDE

Tim

You have an array of pin numbers:

int leds[] = {2, 3, 4, 5, 6, 7};  // the pin that the LED is attached to

Why aren't you using them?

  // declare pin 2-7 to be an output:
  for (int x = 2; x < 8; x++){
  pinMode(x, OUTPUT);
  }

Your arrays only contain 6 elements - numbered 0 to 5. You are looping for a equal 0 to 6, writing off the end of the array.

Does that make a difference regarding my problem? So you say I should change it to:

  // declare pin 2-7 to be an output:
  for (int x = 0; x < 6; x++){
  pinMode(leds[x], OUTPUT);
  }

Does that make much of a difference? And I don't see how that helps with my problem..

But thanks for your help :wink:

Does that make a difference regarding my problem?

Using an array of pin numbers vs. setting the pin states directly does not. Writing off the end of the array does.

TIm,

You're experimenting and that is good thing but I'm going to spoil your project just a bit and in the process you'll perhaps learn a few things for your next experiment.

The key to learning is asking questions followed by a search for the answers.

You see something you don't understand seek the answer till you do.

/*
   Fade

   This example shows how to fade multiple LED's using an array of pins to which
   LED's are attached and analogWrite() function.

   This example code is in the public domain.
*/

#define ARRAY_SIZEOF(ARRAY)         (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinsLEDS[]      = { 2, 3, 4, 5, 6, 7 };
int             brightness[]    = { 0, 0, 0, 0, 0, 0 };
int             deltaFade       = 5;
size_t          indexElement    = 0;

void loop()
{
    analogWrite(pinsLEDS[indexElement], brightness[indexElement]);

    brightness[indexElement] += deltaFade;
    brightness[indexElement] %= 256;

    ++indexElement;
    indexElement %= ARRAY_SIZEOF(pinsLEDS);

    delay(10UL);
}

void setup()
{ 
    for ( size_t i = 0; i < ARRAY_SIZEOF(pinsLEDS); i++ )
    {
        pinMode(pinsLEDS[i], OUTPUT);
    }
}

Thanks, looks really neat and crispy :smiley: I will take a closer look at it!