Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- /*
- * hueTwinkleSwitch
- *
- * Credits to:
- * Mark Kriegsman----Twinkles
- * Philip Gonzalez---Switch program
- * Chris Bendzel-----All the rest
- *
- */
- // Shared variables
- #define LED_PIN 6
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- #define NUM_LEDS 40
- #define BRIGHTNESS 50
- CRGB leds[NUM_LEDS];
- int brtInPin = 1; // Analog input pin to the brightness pot
- int brtVal; // brightness value from pot pin1
- int brtSensorValue = BRIGHTNESS; // value read from the brightness pot
- // Hue variables
- int hueLow = 25; // low color range
- int hueHigh = 56; // high color range
- int hueVal; // hue value from pot pin0
- int hue;
- int hueInPin = 0; // Analog input pin to the hue potentiometer
- int hueSensorValue = 0; // value read from the hue pot
- // Twinkle variables
- #define DENSITY 220 // probability of twinkle
- int denVal; // density value from pot pin0
- int den; //
- int denInPin = 0; // Analog input pin to the density potentiometer
- int denSensorValue = 0; // value read from the density pot
- /*
- Five State Switch -- By: Philip Gonzalez
- Switch variables */
- int switchPin = 8;
- int switchState = 0; // tracks button presses
- boolean buttonState = LOW; // tracks button state with a debouncer
- const CRGB lightcolor(8,7,1);
- void setup() {
- FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- Serial.begin(9600);
- pinMode(switchPin, INPUT);
- }
- void loop()
- {
- if (digitalRead(switchPin) == HIGH) // debouncer
- {
- delay(111);
- buttonState = digitalRead(switchPin);
- }
- if (buttonState == HIGH && switchState == 0) // if the button is pressed change the switch to the first sate
- {
- switchState = 1;
- buttonState = LOW;
- }
- if (buttonState == HIGH && switchState == 1) // if the button is pressed again change the switch to the second sate
- {
- switchState = 2;
- buttonState = LOW;
- }
- if (buttonState == HIGH && switchState == 2) // if the button is pressed again change the switch to the third sate
- {
- switchState = 0;
- buttonState = LOW;
- }
- if (switchState == 0) // if the switch is in the null state, call hueLight
- {
- Serial.println("#*#*#*#*#");
- // Call hue light \\
- hueLight();
- }
- if (switchState == 1) // if the switch is in the first state, call twinkles
- {
- Serial.println(" #*#*#*#*#");
- // Call twinkles \\
- softtwinkles();
- }
- if (switchState == 2) // if the switch is in the second state, call something in the future
- {
- // Call something down the road
- }
- Serial.println(switchState);
- }
- void hueLight()
- {
- Serial.println(" ++++++++++");
- hueSensorValue = analogRead(hueInPin); // read the hue pot
- hueVal = map(hueSensorValue, 0, 1023, hueLow, hueHigh); // map the hue value
- brtSensorValue = analogRead(brtInPin); // read the brt pot
- brtVal = map(brtSensorValue, 0, 1023, 0, BRIGHTNESS); // map the brt value
- for(int dot = 0; dot < NUM_LEDS; dot++) {
- leds[dot] = CHSV( hueVal, 255, 255); // adjust hue
- FastLED.setBrightness(brtVal); // adjust brightness
- }
- FastLED.show();
- }
- void softtwinkles()
- {
- delay(5000);
- for( int i = 0; i < NUM_LEDS; i++) {
- denSensorValue = analogRead(denInPin); // read the density pot
- denVal = map(denSensorValue, 0, 1023, 6, DENSITY); // map the density value
- brtSensorValue = analogRead(brtInPin); // read the brt pot
- brtVal = map(brtSensorValue, 0, 1023, 0, BRIGHTNESS); // map the brt value
- FastLED.setBrightness(brtVal);
- if( !leds[i]) continue; // skip black pixels
- if( leds[i].r & 1) { // is red odd?
- leds[i] -= lightcolor; // darken if red is odd
- } else {
- leds[i] += lightcolor; // brighten if red is even
- }
- }
- if( random8() < denVal) {
- int j = random16(NUM_LEDS);
- if( !leds[j] ) leds[j] = lightcolor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement