Advertisement
Guest User

Untitled

a guest
Sep 5th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. int brightness = 0;
  2. //these two variables are used to store our brightness level during manipulations
  3. int brightnessRev = 255;
  4. int fadeUp = 5;
  5. //the values by which each 'step' of the fade is incremented.
  6. int fadeDown = 5;
  7.  
  8. int myPins[] = {3, 5, 6, 9, 10, 11};
  9. /*here we are storing our output pins in an array, that allows us to
  10. access the 'next' pin without hard-coding in the program itself
  11. if we wanted to change the pin assignments, we could do it once,
  12. right here, and everything else in the code would stay the same.*/
  13.  
  14. void setup() {
  15.   //our setup is very simple, we just need to configure each pin as an output
  16.   pinMode(3, OUTPUT);
  17.   pinMode(5, OUTPUT);
  18.   pinMode(6, OUTPUT);
  19.   pinMode(9, OUTPUT);
  20.   pinMode(10, OUTPUT);
  21.   pinMode(11, OUTPUT);
  22. }
  23.  
  24. void loop() {
  25.   /*this program basically just increments through each LED, slowly fading it up,
  26.   then moving on to the next and doing the same, until all LEDs are on, then
  27.   starting from the first LED, fades them down. Then the process is repeated in
  28.   reverse, creating a sort of bouncing effect. */
  29.  
  30.   for(int i=0; i < 6; i++) {
  31.     brightness = 0;
  32.     while(brightness < 255){
  33.       analogWrite(myPins[i], brightness);
  34.       brightness += fadeUp;
  35.       delay(1);
  36.     }
  37.   }
  38.   //basically the goal here is to iterate a 'fade up' routine
  39.   //on each output pin, which is what this for loop does
  40.  
  41.   for(int i=0; i <6; i++) {
  42.   brightnessRev = 255;
  43.     while(brightnessRev >= 0){
  44.       analogWrite(myPins[i], brightnessRev);
  45.       brightnessRev -= fadeDown;
  46.       delay(1);
  47.     }
  48.   }
  49.   //this for loop does the same thing,
  50.   //but fading the LEDs down.
  51.   delay(125);
  52.   //this delay gives us a slight pause before
  53.   //starting the reverse course
  54.   for(int i=5; i >= 0; i--) {
  55.     brightness = 0;
  56.     while(brightness < 255){
  57.       analogWrite(myPins[i], brightness);
  58.       brightness += fadeUp;
  59.       delay(1);
  60.     }
  61.   }
  62.   for(int i=5; i >= 0; i--) {
  63.   brightnessRev = 255;
  64.     while(brightnessRev >= 0){
  65.       analogWrite(myPins[i], brightnessRev);
  66.       brightnessRev -= fadeDown;
  67.       delay(1);
  68.     }
  69.   }
  70.   delay(125);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement