Guest User

Wave

a guest
Dec 2nd, 2014
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 2
  4. #define NUM_LEDS 255 //max # of leds for this code
  5. CRGB leds[NUM_LEDS];
  6. byte temp[NUM_LEDS];
  7.  
  8. byte wave;
  9. int wave_scale = 20;
  10. unsigned long currentMillis,previousMillis = 0;
  11.  
  12. void setup(){  
  13.   //be sure to add your leds
  14.   FastLED.addLeds< NEOPIXEL, LED_PIN >(leds, NUM_LEDS);
  15.   setup_hue_array();
  16. }
  17.  
  18. void loop(){
  19.   wave_fx();
  20. }
  21.  
  22. void wave_fx(){
  23.   //define the middle of the strip
  24.   static byte middle = NUM_LEDS/2;
  25.  
  26.   //check the millis() timer
  27.   currentMillis = millis();
  28.  
  29.   //if enough time has passed, run the program
  30.   if(currentMillis - previousMillis >= 10) {
  31.    
  32.     for (byte i = 0; i < middle; i++){
  33.       //this is the function that creates the wave effect
  34.       //it may take time to fully understand what is happening here
  35.       wave = sin8((millis() / wave_scale) * i);
  36.      
  37.       //populate the leds[x] with the temp[x] array hue's
  38.       leds[i] = CHSV(temp[i], 255, wave);
  39.     }
  40.     //now mirror the data from leds #0 thru #middle
  41.     //to leds #middle thru #end of string
  42.     for(byte i = middle; i < NUM_LEDS; i++){
  43.       leds[i] = leds[NUM_LEDS - i];
  44.     }
  45.     //timestamp the animation and show the leds[] data
  46.     previousMillis = currentMillis;
  47.     LEDS.show();
  48.   }
  49. }
  50.  
  51. void setup_hue_array(){
  52.   static byte x, hue = 0;
  53.  
  54.   //only assign data for half the led strip
  55.   for(byte i = 0; i < ((NUM_LEDS/2)+ 1); i++){
  56.    
  57.     //for every 5 leds, increase the hue by 20
  58.     if(x++ == 5){
  59.       hue += 20;
  60.       x = 0;  
  61.     }
  62.    
  63.     //assign the hue for later use in the temp[] array
  64.     temp[i] = hue;
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment