Loading...
Pages: [1]   Go Down
Author Topic: Led oscillate  (Read 216 times)
0 Members and 1 Guest are viewing this topic.
42°21′N 71°03′W
Offline Offline
Newbie
*
Karma: 0
Posts: 5
I am here to learn and give back
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am learning
i am trying to figure out how to make 2 LEDs fade but out of phase

here is my fail attempt


/*
 *Double fade
 *
 *
 *This sketch is an experement to make two leds fade on and off
 *in opposite time to eachother.
 */

int led1 = 6; // the pin that the LED is attached to
int led2 = 5;
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);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 9:
  analogWrite(led1, brightness); 
  delay(15);
  analogWrite(led2, 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 ;
  }     
  // wait for 30 milliseconds to see the dimming effect   
  delay(30);                           
}


furthermore, i would like to learn from some lessons, but i cant seem to find anything that goes too deep. any advice?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 247
Posts: 16535
Available for Design & Build services
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are using the same 'brightness' for both PWM signals.
Try brightness, brightness1
One goes 0 to 255, the other goes 255 to 0, then they change directions.

You could also try brightness and (255 - brightness):
0,      5,    10,   15,  20 ... 240, 245, 250, 255
255, 250, 245, 240, 235..  15, 10,   5,     0


You've also got some pin9 comments in, left over from something else?
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

Valencia, Spain
Offline Offline
Edison Member
*
Karma: 65
Posts: 2256
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  // set the brightness of pin 9:
  analogWrite(led1, brightness); 
  delay(15);
  analogWrite(led2, brightness); 

You're setting both LEDs to the same brightness.

furthermore, i would like to learn from some lessons, but i cant seem to find anything that goes too deep. any advice?

Lessons for what?
Logged

Pages: [1]   Go Up
Print
 
Jump to: