// https://forum.arduino.cc/t/controlling-a-rgb-led-strip/1468927/
// Defife Pin Numbers for hte White, Red, Green, and Blue LEDs
const byte whtLED = 9;
const byte blueLED = 3;
const byte grnLED = 5;
const byte redLED = 6;
// Define Color Buttons
const byte whtUpButton = 10;
const byte redUpButton = 7;
const byte grnUpButton = 4;
const byte blueUpButton = 2;
const byte whtDnButton = 11;
const byte redDnButton = 12;
const byte grnDnButton = 13;
const byte blueDnButton = 8;
int i = 0; //Used to count up and down
boolean whtUpVal = 0; //Store the val of the White Up button
boolean redUpVal = 0; //Store the value of the Red Up button
boolean grnUpVal = 0; //Store the value of the Green Up button
boolean blueUpVal = 0; //Store the value of the Blue Up button
boolean whtDnVal = 0; //Store the val of the White Down button
boolean redDnVal = 0; //Store the value of the Red Down button
boolean grnDnVal = 0; //Store the value of the Green Down button
boolean blueDnVal = 0; //Store the value of the Blue Down button
boolean old_whtUpVal = 0;
boolean old_redUpVal = 0;
boolean old_grnUpVal = 0;
boolean old_blueUpVal = 0;
boolean old_whtDnVal = 0;
boolean old_redDnVal = 0;
boolean old_grnDnVal = 0;
boolean old_blueDnVal = 0;
boolean whtUpState = 0; //0 = LED off while 1 = LED on
boolean redUpState = 0;
boolean grnUpState = 0;
boolean blueUpState = 0;
boolean whtDnState = 0; //0 = LED off while 1 = LED on
boolean redDnState = 0;
boolean grnDnState = 0;
boolean blueDnState = 0;
byte whtUpBrightness = 128;
byte redUpBrightness = 128;
byte grnUpBrightness = 128;
byte blueUpBrightness = 128;
byte whtDnBrightness = 128;
byte redDnBrightness = 128;
byte grnDnBrightness = 128;
byte blueDnBrightness = 128;
unsigned long whtUpStartTime = 0; //when did we start pressing the button
unsigned long redUpStartTime = 0;
unsigned long grnUpStartTime = 0;
unsigned long blueUpStartTime = 0;
unsigned long whtDnStartTime = 0; //when did we start pressing the button
unsigned long redDnStartTime = 0;
unsigned long grnDnStartTime = 0;
unsigned long blueDnStartTime = 0;
void setup() {
//Set the LED Pins to OUTPUT
pinMode(whtLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(grnLED, OUTPUT);
pinMode(blueLED, OUTPUT);
// Define the Buttons as inputs
pinMode(whtUpButton, INPUT);
pinMode(redUpButton, INPUT);
pinMode(grnUpButton, INPUT);
pinMode(blueUpButton, INPUT);
pinMode(whtDnButton, INPUT);
pinMode(redDnButton, INPUT);
pinMode(grnDnButton, INPUT);
pinMode(blueDnButton, INPUT);
Serial.begin(9600);
}
void loop() {
whtUpVal = digitalRead(whtUpButton); //Read the White Up button
//Serial.println(whtVal);
redUpVal = digitalRead(redUpButton); //Read the Red Up button
grnUpVal = digitalRead(grnUpButton); //Read the Green Up button
blueUpVal = digitalRead(blueUpButton); //Read the Blue Up button
//Serial.println(blueVal);
whtDnVal = digitalRead(whtDnButton); //Read the White Down button
//Serial.println(whtVal);
redDnVal = digitalRead(redDnButton); //Read the Red Down button
grnDnVal = digitalRead(grnDnButton); //Read the Green Down button
blueDnVal = digitalRead(blueDnButton); //Read the Blue Down button
//Check for a transition on each button
if ((whtUpVal == HIGH) && (old_whtUpVal == LOW)) {
whtUpState = 1 - whtUpState; //Change the state from off to on or vise-a-versa
whtUpStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
if ((whtDnVal == HIGH) && (old_whtDnVal == LOW)) {
whtDnState = 1 - whtDnState; //Change the state from off to on or vise-a-versa
whtDnStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
//Check whether the button is being held down
if ((whtUpVal == HIGH) && (old_whtUpVal == HIGH)) {
// If button held for more than 500 ms
if (whtUpState == 1 && (millis() - whtUpStartTime) > 500) {
whtUpBrightness++; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (whtUpBrightness > 255) { // 255 is the maximum brightness
whtUpBrightness = 0; //go to 0
}
}
}
if ((whtDnVal == HIGH) && (old_whtDnVal == HIGH)) {
// If button held for more than 500 ms
if (whtDnState == 1 && (millis() - whtDnStartTime) > 500) {
whtDnBrightness--; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (whtDnBrightness < 0) { // 255 is the maximum brightness
whtDnBrightness = 255; //go to 0
}
}
}
if (whtUpState && whtDnState == 1) {
whtUpState = 0;
whtDnState = 0;
}
old_whtUpVal = whtUpVal;
old_whtDnVal = whtDnVal;
if (whtUpState == 1) {
whtDnState = 0;
analogWrite(whtLED, whtUpBrightness);
delay(50);
} else {
analogWrite(whtLED, 0);
}
if (whtDnState == 1) {
whtUpState = 0;
analogWrite(whtLED, whtDnBrightness);
delay(50);
} else {
analogWrite(whtLED, 0);
}
if ((redUpVal == HIGH) && (old_redUpVal == LOW)) {
redUpState = 1 - redUpState; //Change the state from off to on or vise-a-versa
redUpStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
if ((redDnVal == HIGH) && (old_redDnVal == LOW)) {
redDnState = 1 - redDnState; //Change the state from off to on or vise-a-versa
redDnStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
//Check whether the button is being held down
if ((redUpVal == HIGH) && (old_redUpVal == HIGH)) {
// If button held for more than 500 ms
if (redUpState == 1 && (millis() - redUpStartTime) > 500) {
redUpBrightness++; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (redUpBrightness > 255) { // 255 is the maximum brightness
redUpBrightness = 0; //go to 0
}
}
}
//Check whether the button is being held down
if ((redDnVal == HIGH) && (old_redDnVal == HIGH)) {
// If button held for more than 500 ms
if (redDnState == 1 && (millis() - redDnStartTime) > 500) {
redDnBrightness--; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (redDnBrightness < 0) { // 255 is the maximum brightness
redDnBrightness = 255; //go to 0
}
}
}
if (redUpState && redDnState == 1) {
redUpState = 0;
redDnState = 0;
}
old_redUpVal = redUpVal;
old_redDnVal = redDnVal;
if (redUpState == 1) {
redDnState = 0;
analogWrite(redLED, redUpBrightness);
delay(100);
} else {
analogWrite(redLED, 0);
}
if (redDnState == 1) {
redUpState = 0;
analogWrite(redLED, redDnBrightness);
delay(100);
} else {
analogWrite(redLED, 0);
}
if ((grnUpVal == HIGH) && (old_grnUpVal == LOW)) {
grnUpState = 1 - grnUpState; //Change the state from off to on or vise-a-versa
grnUpStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
if ((grnDnVal == HIGH) && (old_grnDnVal == LOW)) {
grnDnState = 1 - grnDnState; //Change the state from off to on or vise-a-versa
grnDnStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
//Check whether the button is being held down
if ((grnUpVal == HIGH) && (old_grnUpVal == HIGH)) {
// If button held for more than 500 ms
if (grnUpState == 1 && (millis() - grnUpStartTime) > 500) {
grnUpBrightness++; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (grnUpBrightness > 255) { // 255 is the maximum brightness
grnUpBrightness = 0; //go to 0
}
}
}
if ((grnDnVal == HIGH) && (old_grnDnVal == HIGH)) {
// If button held for more than 500 ms
if (grnDnState == 1 && (millis() - grnDnStartTime) > 500) {
grnDnBrightness--; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (grnDnBrightness < 0) { // 255 is the maximum brightness
grnDnBrightness = 255; //go to 0
}
}
}
if (grnUpState && grnDnState == 1) {
grnUpState = 0;
grnDnState = 0;
}
old_grnUpVal = grnUpVal;
old_grnDnVal = grnDnVal;
if (grnUpState == 1) {
analogWrite(grnLED, grnUpBrightness);
delay(100);
} else {
analogWrite(grnLED, 0);
}
if (grnDnState == 1) {
analogWrite(grnLED, grnDnBrightness);
delay(100);
} else {
analogWrite(grnLED, 0);
}
if ((blueUpVal == HIGH) && (old_blueUpVal == LOW)) {
blueUpState = 1 - blueUpState; //Change the state from off to on or vise-a-versa
blueUpStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
if ((blueDnVal == HIGH) && (old_blueDnVal == LOW)) {
blueDnState = 1 - blueDnState; //Change the state from off to on or vise-a-versa
blueDnStartTime = millis(); //This is the Arduino clock. Returns the number of milliseconds that have passed
delay(10);
}
if ((blueUpVal == HIGH) && (old_blueUpVal == HIGH)) {
// If button held for more than 500 ms
if (blueUpState == 1 && (millis() - blueUpStartTime) > 500) {
blueUpBrightness++; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (blueUpBrightness > 255) { // 255 is the maximum brightness
blueUpBrightness = 0; //go to 0
}
}
}
if ((blueDnVal == HIGH) && (old_blueDnVal == HIGH)) {
// If button held for more than 500 ms
if (blueDnState == 1 && (millis() - blueDnStartTime) > 500) {
blueDnBrightness--; // Increment White Brightness by 1
delay(10); //Delay to avoid going up too fast
if (blueDnBrightness < 0) { // 255 is the maximum brightness
blueDnBrightness = 0; //go to 0
}
}
}
if (blueUpState && blueDnState == 1) {
blueUpState = 0;
blueDnState = 0;
}
old_blueUpVal = blueUpVal;
old_blueDnVal = blueDnVal;
if (blueUpState == 1) {
analogWrite(blueLED, blueUpBrightness);
delay(100);
} else {
analogWrite(blueLED, 0);
}
if (blueDnState == 1) {
analogWrite(blueLED, blueDnBrightness);
delay(100);
} else {
analogWrite(blueLED, 0);
}
}