Created
March 19, 2017 17:44
-
-
Save cbonzo69/c1711c0299e6f0988f9eb391c430c7bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <FastLED.h> | |
#define LED_PIN 4 | |
#define COLOR_ORDER GRB | |
#define CHIPSET WS2812B | |
#define NUM_LEDS 22 | |
#define BRIGHTNESS 255 | |
int brtInPin = 4; // Analog input pin to the brightness pot | |
int brtVal; // brightness value from pot pin1 | |
int brtSensorValue = BRIGHTNESS; // value read from the brightness pot | |
int oldVal; | |
int switchPin = 2; | |
int switchState = 0; // tracks button presses | |
boolean buttonState = LOW; // tracks button state with a debouncer | |
unsigned long interval=2000; | |
unsigned long previousMillis=0; | |
unsigned long currentMillis; | |
CRGB leds[NUM_LEDS]; | |
void setup() { | |
delay(3000); // sanity delay | |
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); | |
FastLED.setBrightness( BRIGHTNESS ); | |
pinMode(switchPin, INPUT); | |
Serial.begin(9600); | |
}// setup | |
//--------------------------------------------------------------------------- | |
void off(){ | |
FastLED.clear(); | |
Serial.print("Off "); | |
Serial.println(currentMillis); | |
}// off() | |
//---------------------------------------------------------------------------- | |
void warm(){ | |
Serial.print("Warm "); | |
Serial.println(currentMillis); | |
}// warm | |
//---------------------------------------------------------------------------- | |
void cool(){ | |
Serial.print("Cool "); | |
Serial.println(currentMillis); | |
}// cool | |
//---------------------------------------------------------------------------- | |
void loop() | |
{ | |
currentMillis = millis(); // grab current time | |
if (digitalRead(switchPin) == HIGH) // debouncer | |
{ | |
delay(125); // debounce delay | |
buttonState = digitalRead(switchPin); | |
} | |
if (buttonState == HIGH && switchState == 0) // if the button is pressed change the switch to the first state | |
{ | |
switchState = 1; | |
buttonState = LOW; | |
previousMillis = 0; | |
} | |
if (buttonState == HIGH && switchState == 1 && (unsigned long)(currentMillis - previousMillis) >= interval) // if the button is pressed again within 2 seconds change the switch to the second state | |
{ | |
switchState = 2; | |
buttonState = LOW; | |
} | |
else | |
if (buttonState == HIGH && switchState == 1 && (unsigned long)(currentMillis - previousMillis) <= interval) | |
{ | |
switchState = 0; | |
buttonState = LOW; | |
} | |
if (buttonState == HIGH && switchState == 2) // if the button is pressed again change the switch to the third state | |
{ | |
switchState = 0; | |
buttonState = LOW; | |
} | |
//-------------------------------------------------------------------------------- | |
if (switchState == 0) // if the switch is in the null state, all off | |
{ | |
// All off | |
off(); | |
} | |
else if (switchState == 1) // if the switch is in the first state, warm white | |
{ | |
warm(); | |
} | |
else if (switchState == 2) // if the switch is in the second state, cool white | |
{ | |
cool(); | |
} | |
}// loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment