Go Down

Topic: Led oscillate (Read 1 time) previous topic - next topic

chutch

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?

CrossRoads

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?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

fungus


Code: [Select]
  // 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?
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Go Up