Advertisement
3rdWard_Arduino

Class2_PWM multiple time management

Feb 1st, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. /*
  2. Fade RGB LEDs
  3.  This example shows how to fade an LED on pin 3,5,6
  4.  using the analogWrite() function.
  5.  This example code is in the public domain.
  6.  */
  7. int brightnessBlue = 0; // how bright the BLUE LED is
  8. int fadeAmountBlue = 1; // how many points to fade the BLUE LED by
  9. long incrementTimeBlue = 10;
  10. long desiredTimeBlue = incrementTimeBlue;
  11.  
  12. int brightnessGreen = 85; // how bright the GREEN LED is
  13. int fadeAmountGreen = 2; // how many points to fade the BLUE LED by
  14. long incrementTimeGreen = 10;
  15. long desiredTimeGreen = incrementTimeGreen;
  16.  
  17. int brightnessRed = 170; // how bright the RED LED is
  18. int fadeAmountRed = 3; // how many points to fade the BLUE LED by
  19. long incrementTimeRed = 10;
  20. long desiredTimeRed = incrementTimeRed;
  21.  
  22. void setup() {
  23.   // declare pin 9 to be an output:
  24.   pinMode(3, OUTPUT);
  25.   pinMode(5, OUTPUT);
  26.   pinMode(6, OUTPUT);
  27.   Serial.begin(9600);
  28. }
  29.  
  30. void loop() {
  31.   // set the brightness of pin 3, 5, and 6:
  32.   analogWrite(3, brightnessBlue);
  33.   analogWrite(5, brightnessGreen);
  34.   analogWrite(6, brightnessRed);
  35.   // BLUE LED LOGIC BEGIN
  36.   // change the brightness for next time through the loop:
  37.   if( millis() >= desiredTimeBlue){
  38.     brightnessBlue += fadeAmountBlue;
  39.     desiredTimeBlue += incrementTimeBlue;
  40.   }
  41.  
  42.   // reverse the direction of the fading at the ends of the fade:
  43.   if (brightnessBlue <= 0 || brightnessBlue >= 255) {
  44.     fadeAmountBlue *= -1 ;
  45.     brightnessBlue += fadeAmountBlue;
  46.   }
  47.   // BLUE LED LOGIC END
  48.  
  49.   // GREEN LED LOGIC BEGIN
  50.   // change the brightness for next time through the loop:
  51.   if( millis() >= desiredTimeGreen){
  52.     brightnessGreen += fadeAmountGreen;
  53.     desiredTimeGreen += incrementTimeGreen;
  54.   }
  55.  
  56.   // reverse the direction of the fading at the ends of the fade:
  57.   if (brightnessGreen <= 0 || brightnessGreen >= 255) {
  58.     fadeAmountGreen *= -1 ;
  59.     brightnessGreen += fadeAmountGreen;
  60.   }
  61.   // GREEN LED LOGIC END
  62.  
  63.   // RED LED LOGIC BEGIN
  64.   // change the brightness for next time through the loop:
  65.   if( millis() >= desiredTimeRed){
  66.     brightnessRed += fadeAmountRed;
  67.     desiredTimeRed += incrementTimeRed;
  68.   }
  69.  
  70.   // reverse the direction of the fading at the ends of the fade:
  71.   if (brightnessRed <= 0 || brightnessRed >= 255) {
  72.     fadeAmountRed *= -1 ;
  73.     brightnessRed += fadeAmountRed;
  74.   }
  75.   // RED LED LOGIC END
  76.   // wait for 30 milliseconds to see the dimming effect
  77.   //delay(30);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement