Simple LED sketch

Hey guys, newbie question here. I want to use a simple LED sketch that will utilize all 7 PWM ports on the Leonardo. I want to fad up LED 1 and when it reaches maximum brightness begin to fade up LED 2 and so on in succesion until all 7 are lit. 5 seconds after they are all lit I want them all to fade off at the same time. I tried modifying the 'fade' code thats in the examples folder but I didn't get very far. Any help with this would be appreciated

Assuming that you can fade each up in succession.
Fading back down is the same process in reverse. In your case, the only difference is instead of fading each in succession they go down all "at once". So -- do that. analogWrite that same intensity value to each PWM pin in succession, then "delay", next lower value,... till they're dark.

Thanks for the reply. Im still not sure how to fade them up one by one though.

Reading between the lines of the post, it seems that you're having trouble with the fading process in general. Try reducing the scope of your task to just brightening a single LED from 0 to full brightness. If you can get that to work, the rest of your issues might very well vanish. If you can't, you'll have something specific to post here. Right now, your question is pretty vague.

To fade up, write a for:next loop that counts up, use that value for analogWrite.

for (x = 0; x<256; x=x+1){
analogWrite (pwmPin1, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (pwmPin2, x);
delay(10); // take ~2.5 seconds for full fade up
}
etc,. for all 7 pins
delay (5000); // hold for 5 seconds
// fade down
for (x=255; x>0; x=x-1){
analogWrite (pwmPin1, x);
analogWrite (pwmPin2, x);
analogWrite (pwmPin3, x);
analogWrite (pwmPin4, x);
analogWrite (pwmPin5, x);
analogWrite (pwmPin6, x);
analogWrite (pwmPin7, x);
delay (10);
}

I wouldn't use delay, I would use blink-without-delay method so other stuff could happen during the hold at each brightness, but this should give you the idea of what to do.
The fade up could be done as for:next also (from 0 to 6, with the pins in an array), but writing them all out as 7 loops will work also.

Thanks! I'll try this out when I get home.

I tried adding that code to the fade sketch and I got a whole mess or errors. I should mention that when it comes to coding I'm very much a beginner. This is for an art project, I tend to do better at the "art" part haha.

and I got a whole mess or errors

So keep them to you self if you don't want help fixing them.
Alternative post the code you are trying to use.
Better still as advised before back off on the ambition and just try for one. It is much easier to get one going and then expand it to lots that get lost going all in one go.

when it comes to coding I'm very much a beginner

So you will be wanting to learn a lot then. :slight_smile:

Oh sorry, here is the code I tried using. I think I have to remove the void loop to make it work? Maybe its all wrong in general. Try not to laugh when you read it! haha

/*
 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 led1 = 3;
int led2 = 5;  
int led3 = 6;  
int led4 = 9;  
int led5 = 10;  
int led6 = 11;  
int led7 = 13;  // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led1, brightness);    

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

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
    
 
  }
  
for (x = 0; x<256; x=x+1){
analogWrite (led1, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (led2, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (led3, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (led4, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (led5, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (led6, x);
delay(10); // take ~2.5 seconds for full fade up
}
for (x = 0; x<256; x=x+1){
analogWrite (led7, x);
delay(10); // take ~2.5 seconds for full fade up
}

}

delay (5000); // hold for 5 seconds
// fade down
for (x=255; x>0; x=x-1){
analogWrite (led1, x);
analogWrite (led2, x);
analogWrite (led3, x);
analogWrite (led4, x);
analogWrite (led5, x);
analogWrite (led6, x);
analogWrite (led7, x);
delay (10);
}
      
 
}

Also, here are errors:

Fade.cpp: In function 'void loop()':
Fade:46: error: 'x' was not declared in this scope
Fade.cpp: At global scope:
Fade:77: error: expected constructor, destructor, or type conversion before '(' token
Fade:79: error: expected unqualified-id before 'for'
Fade:79: error: expected constructor, destructor, or type conversion before '>' token
Fade:79: error: expected constructor, destructor, or type conversion before '=' token
Fade:91: error: expected declaration before '}' token

It looks like you've decided to try to complete the whole program at once. I'll suggest that you don't try that, as it's likely to be very frustrating for you if nobody decides to write it for you. I recommend that you start with a simple goal, and, once you succeed at that one, add more functionality. With that approach, you only have to fix one or two things at a time. With this broad approach, you don't know if any of your code works, until you can make it all work.

Try to autoformat your code. It will make it much easier to follow. And, when the IDE declines to autoformat, it will give you an important clue as to one of the reasons your program won't compile. For another clue, look at the first error message.

Here's a hint - if you see lots of repeated code like this

for (x = 0; x<256; x=x+1){
analogWrite (led1, x);
delay(10); // take ~2.5 seconds for full fade up
}

You should probably wite a function to do that and then call the function, like this:

void fadeUpOneLED(int ledPin)
{
  for (int x = 0; x<256; x=x+1)
  {
    analogWrite (ledPin, x);
    delay(10); // take ~2.5 seconds for full fade up
  }
}

and in the main program call

fadeUpOneLED(thispin);

Also, properly format your code (there is an autoformat function in the IDE that makes it easy) as this will improve readability and make it easier for you (and us) to debug.

jeffd:
Hey guys, newbie question here. I want to use a simple LED sketch that will utilize all 7 PWM ports on the Leonardo. I want to fad up LED 1 and when it reaches maximum brightness begin to fade up LED 2 and so on in succesion until all 7 are lit. 5 seconds after they are all lit I want them all to fade off at the same time. I tried modifying the 'fade' code thats in the examples folder but I didn't get very far. Any help with this would be appreciated

You're lucky that I'm the type that likes writing code ;). Here is a complete code of your request. I didn't try it out because I'm simply to lazy on that part =P!

/*
Fade in, fade out for my friend jeffd @ Arduino forum
 
 //Baselsw
 */

int leds[] = {3,5,6,9,10,11,13};
int num = 7;


void init_leds(){
  for(int i = 0; i<num; i++){
    digitalWrite(leds[i],LOW);
    pinMode(leds[i], OUTPUT);     
  } 
}

void FadeOutLeds(){
  for(int i = 255;i>0;i--){

    for(int j = 0;j<num;j++){
      analogWrite(leds[j],i); 
    }
    delay(10);
  } 

}

void FadeInLed(int theLed){
  for(int i = 0;i<256;i++){
    analogWrite(theLed,i);
    delay(10);
  }
}


void setup() {                
  // initialize the digital pins as an output.
  init_leds();
}

// the loop routine runs over and over again forever:
void loop() {

  for(int i = 0; i<num;i++){
    FadeInLed(leds[i]);      
  }
  delay(5000);               // wait for 5 seconds
  FadeOutLeds();

}

baselsw, YOU DA MAN!

Thanks everyone else for the tips!

jeffd:
baselsw, YOU DA MAN!

Thanks everyone else for the tips!

Well, everything for the state of art! Don't forget to post a picture/video of your finished masterpiece :P..

Make

num = sizeof(leds)/sizeof(leds[0]);

So that you don't need to remember to change that variable - just add pins to the leds array and it will adjust.