Created
January 13, 2013 05:54
-
-
Save edsammy/4522570 to your computer and use it in GitHub Desktop.
Arduino RGB lamp
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
/* Click and Press+Hold RGB lamp | |
By Eddie Samuels | |
To keep input interfaces simple, I want to use a single button to: | |
1) cycle trough colors | |
2) press and hold to enter mood lamp mode | |
*/ | |
int buttonPin = 2; // analog input pin to use as a digital input | |
int redLed = 9; // digital output pin red led | |
int greenLed = 11; // digital output pin for green led | |
int blueLed = 10; | |
int debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button | |
int holdTime = 2000; // ms hold period: how long to wait for press+hold event | |
// Button variables | |
int buttonVal = 0; // value read from button | |
int buttonLast = 0; // buffered value of the button's previous state | |
long btnDnTime; // time the button was pressed down | |
long btnUpTime; // time the button was released | |
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered | |
// LED variables | |
int ledVal = 0; // state of LED 1 | |
//================================================= | |
void color(int colorVal){ | |
if (colorVal == 1){ | |
digitalWrite(redLed, HIGH); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, LOW); | |
} | |
if (colorVal == 2){ | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, HIGH); | |
digitalWrite(blueLed, LOW); | |
} | |
if (colorVal == 3){ | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, HIGH); | |
} | |
if (colorVal == 4){ | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, HIGH); | |
digitalWrite(blueLed, HIGH); | |
} | |
if (colorVal == 5){ | |
digitalWrite(redLed, HIGH); | |
digitalWrite(greenLed, HIGH); | |
digitalWrite(blueLed, LOW); | |
} | |
if (colorVal == 6){ | |
digitalWrite(redLed, HIGH); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, HIGH); | |
} | |
if (colorVal == 7){ | |
digitalWrite(redLed, HIGH); | |
digitalWrite(greenLed, HIGH); | |
digitalWrite(blueLed, HIGH); | |
} | |
if (colorVal == 8){ | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, LOW); | |
ledVal = 0; | |
} | |
} | |
//Fade Up | |
void fadeUp(int led){ | |
int fadeValue; | |
for(fadeValue = 0 ; fadeValue <= 255; fadeValue++) { | |
analogWrite(led, fadeValue); | |
delay(35); | |
} | |
} | |
//Fade Down | |
void fadeDown(int led){ | |
int fadeValue; | |
for(fadeValue = 255 ; fadeValue >= 0; fadeValue--) { | |
analogWrite(led, fadeValue); | |
delay(35); | |
} | |
} | |
void setup(){ | |
// Set button input pin | |
pinMode(buttonPin, INPUT); | |
digitalWrite(buttonPin, HIGH ); | |
// Set LED output pins | |
pinMode(redLed, OUTPUT); | |
pinMode(greenLed, OUTPUT); | |
pinMode(blueLed, OUTPUT); | |
} | |
//================================================= | |
void loop(){ | |
// Read the state of the button | |
buttonVal = digitalRead(buttonPin); | |
// Test for button pressed and store the down time | |
if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounce)){ | |
btnDnTime = millis(); | |
} | |
// Test for button release and store the up time | |
if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounce)){ | |
if (ignoreUp == false) event1(); | |
else ignoreUp = false; | |
btnUpTime = millis(); | |
} | |
// Test for button held down for longer than the hold time | |
if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime)){ | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, LOW); | |
fadeUp(blueLed); | |
event2(); | |
ignoreUp = true; | |
btnDnTime = millis(); | |
} | |
buttonLast = buttonVal; | |
} | |
//================================================= | |
// Events to trigger by click and press+hold | |
void event1() | |
{ | |
ledVal++; | |
color(ledVal); | |
} | |
void event2(){ | |
fadeUp(greenLed); | |
fadeDown(blueLed); | |
fadeUp(redLed); | |
fadeDown(greenLed); | |
fadeUp(blueLed); | |
fadeDown(redLed); | |
event2(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment