Advertisement
cbonzo69

hueTwinkleSwitch_2

Nov 28th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. /*
  4.  * hueTwinkleSwitch
  5.  *
  6.  * Credits to:
  7.  * Mark Kriegsman----Twinkles
  8.  * Philip Gonzalez---Switch program
  9.  * Chris Bendzel-----All the rest
  10.  *
  11.  */
  12.  
  13. // Shared variables
  14. #define LED_PIN     6
  15. #define LED_TYPE    WS2812B
  16. #define COLOR_ORDER GRB
  17. #define NUM_LEDS    40
  18. #define BRIGHTNESS  50
  19. CRGB leds[NUM_LEDS];
  20.  
  21. int brtInPin = 1;                    // Analog input pin to the brightness pot
  22. int brtVal;                          // brightness value from pot pin1
  23. int brtSensorValue = BRIGHTNESS;     // value read from the brightness pot
  24.  
  25. // Hue variables
  26. int hueLow = 25;                     // low color range
  27. int hueHigh = 56;                    // high color range
  28. int hueVal;                          // hue value from pot pin0
  29. int hue;
  30. int hueInPin = 0;                    // Analog input pin to the hue potentiometer
  31. int hueSensorValue = 0;              // value read from the hue pot
  32.  
  33.  
  34. //  Twinkle variables
  35. #define DENSITY     220              // probability of twinkle
  36. int denVal;                          // density value from pot pin0
  37. int den;                             //
  38. int denInPin = 0;                    // Analog input pin to the density potentiometer
  39. int denSensorValue = 0;              // value read from the density pot
  40.  
  41. /*
  42. Five State Switch --  By: Philip Gonzalez
  43. Switch variables    */
  44. int switchPin = 8;                
  45. int switchState = 0;                 // tracks button presses
  46. boolean buttonState = LOW;           // tracks button state with a debouncer
  47.  
  48.  
  49. const CRGB lightcolor(8,7,1);
  50.  
  51. void setup() {
  52.   FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  53.   FastLED.setBrightness(BRIGHTNESS);
  54.  
  55.     Serial.begin(9600);
  56.   pinMode(switchPin, INPUT);  
  57. }
  58.  
  59. void loop()
  60. {
  61.   if (digitalRead(switchPin) == HIGH) // debouncer
  62.   {
  63.     delay(111);
  64.     buttonState = digitalRead(switchPin);
  65.   }
  66.   if (buttonState == HIGH && switchState == 0) // if the button is pressed change the switch to the first sate
  67.   {
  68.     switchState = 1;
  69.     buttonState = LOW;
  70.   }
  71.   if (buttonState == HIGH && switchState == 1) // if the button is pressed again change the switch to the second sate
  72.   {
  73.    switchState = 2;
  74.    buttonState = LOW;
  75.   }
  76.   if (buttonState == HIGH && switchState == 2) // if the button is pressed again change the switch to the third sate
  77.   {
  78.    switchState = 0;
  79.    buttonState = LOW;
  80.   }
  81.  
  82.  
  83.   if (switchState == 0)  // if the switch is in the null state, call hueLight
  84.   {
  85.     Serial.println("#*#*#*#*#");
  86.     // Call hue light \\
  87.     hueLight();
  88.   }
  89.   if (switchState == 1)  // if the switch is in the first state, call twinkles
  90.   {
  91.     Serial.println("         #*#*#*#*#");
  92.     // Call twinkles \\
  93.     softtwinkles();
  94.   }
  95.   if (switchState == 2)  // if the switch is in the second state, call something in the future
  96.   {
  97.    
  98.     // Call something down the road
  99.   }
  100.   Serial.println(switchState);
  101. }
  102.  
  103. void hueLight()
  104. {
  105.   Serial.println("                  ++++++++++");
  106.  hueSensorValue = analogRead(hueInPin);                     // read the hue pot
  107.   hueVal = map(hueSensorValue, 0, 1023, hueLow, hueHigh);   // map the hue value
  108.  
  109.   brtSensorValue = analogRead(brtInPin);                    // read the brt pot
  110.   brtVal = map(brtSensorValue, 0, 1023, 0, BRIGHTNESS);     // map the brt value
  111.  
  112.    for(int dot = 0; dot < NUM_LEDS; dot++) {
  113.    leds[dot] = CHSV( hueVal, 255, 255);                     // adjust hue
  114.    FastLED.setBrightness(brtVal);                           // adjust brightness
  115.    }
  116.    FastLED.show();
  117.  
  118. }
  119.  
  120.  
  121.  
  122. void softtwinkles()
  123. {
  124.   delay(5000);
  125.      for( int i = 0; i < NUM_LEDS; i++) {
  126.  
  127.      denSensorValue = analogRead(denInPin);                    // read the density pot
  128.      denVal = map(denSensorValue, 0, 1023, 6, DENSITY);        // map the density value
  129.  
  130.      brtSensorValue = analogRead(brtInPin);                    // read the brt pot
  131.      brtVal = map(brtSensorValue, 0, 1023, 0, BRIGHTNESS);     // map the brt value
  132.      FastLED.setBrightness(brtVal);
  133.    
  134.     if( !leds[i]) continue; // skip black pixels
  135.     if( leds[i].r & 1) { // is red odd?
  136.       leds[i] -= lightcolor; // darken if red is odd
  137.     } else {
  138.       leds[i] += lightcolor; // brighten if red is even
  139.     }
  140.   }
  141.  
  142.  
  143.   if( random8() < denVal) {
  144.     int j = random16(NUM_LEDS);
  145.     if( !leds[j] ) leds[j] = lightcolor;
  146.   }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement