Pages: [1]   Go Down
Author Topic: Simple LED sketch  (Read 1168 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Faraday Member
**
Karma: 66
Posts: 2578
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA
Offline Offline
Sr. Member
****
Karma: 17
Posts: 391
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To fade up, write a for:next loop that counts up, use that value for analogWrite.
Code:
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.
« Last Edit: August 23, 2012, 10:31:20 am by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34581
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
when it comes to coding I'm very much a beginner
So you will be wanting to learn a lot then.  smiley
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 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:

Code:
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
Logged

USA
Offline Offline
Sr. Member
****
Karma: 17
Posts: 391
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 23, 2012, 06:09:07 pm by tmd3 » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a hint - if you see lots of repeated code like this
Code:
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:
Code:
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
Code:
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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Sweden
Offline Offline
Sr. Member
****
Karma: 13
Posts: 257
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink. Here is a complete code of your request. I didn't try it out because I'm simply to lazy on that part =P!

Code:
/*
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();

}

« Last Edit: August 23, 2012, 06:46:28 pm by baselsw » Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

baselsw, YOU DA MAN!

Thanks everyone else for the tips!
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 13
Posts: 257
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz..
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Make

Code:
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.
« Last Edit: August 24, 2012, 12:12:28 am by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: