First we declare all the variables that we're gonna use from arduino:
const int green = 13; //Green LED Strip
const int blue = 12; //Blue LED Strip
const int BLUEledPin = 11; //Blue LED Pin
const int REDledPin = 10; //Red LED Pin
const int GREENledPin = 9; //Green LED Pin
const int red = 8; //Red LED Strip
const int buttonPinRed = 7; // the number of the pushbutton pin
const int buttonPinBlue = 6; // the number of the pushbutton pin
const int buttonPinGreen = 5; // the number of the pushbutton pin
int buttonStateRed = 0; // variable for reading the pushbutton status
int buttonStateBlue = 0; // variable for reading the pushbutton status
int buttonStateGreen = 0; // variable for reading the pushbutton status
int color=0; //Initial State of Color
Then we assign what variables goes as input and output:
void setup() {
// initialize the LED pin as an output:
pinMode(BLUEledPin, OUTPUT);
pinMode(REDledPin, OUTPUT);
pinMode(GREENledPin, OUTPUT);
// initialize the LED pin as an output:
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinRed, INPUT);
digitalWrite(buttonPinRed, HIGH); //Activate internal pull up for switch
pinMode(buttonPinBlue, INPUT);
digitalWrite(buttonPinBlue, HIGH); //Activate internal pull up for switch
pinMode(buttonPinGreen, INPUT);
digitalWrite(buttonPinGreen, HIGH); //Activate internal pull up for switch
}
Then we use void loop as our main function were we code according to what we want to do, for example the if statements goes with the switches, for example if you press the red and blue switch, you get a purple color on the LED Strip.
void loop() {
// read the state of the pushbutton value:
buttonStateRed = digitalRead(buttonPinRed);
buttonStateBlue = digitalRead(buttonPinBlue);
buttonStateGreen = digitalRead(buttonPinGreen);
if (buttonStateRed == LOW && buttonStateBlue == HIGH && buttonStateGreen == HIGH) {
SetColor(255, 0, 0);
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
}
if (buttonStateRed == LOW && buttonStateBlue == LOW && buttonStateGreen == HIGH) {
SetColor(255, 0, 255);
digitalWrite(red, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(green, LOW);
}
if (buttonStateRed == LOW && buttonStateBlue == LOW && buttonStateGreen == LOW) {
SetColor(255, 255, 255);
digitalWrite(red, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(green, HIGH);
}
if (buttonStateRed == HIGH && buttonStateBlue == LOW && buttonStateGreen == LOW) {
SetColor(0, 255, 255);
digitalWrite(blue, HIGH);
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
if (buttonStateRed == HIGH && buttonStateBlue == HIGH && buttonStateGreen == LOW) {
SetColor(0, 255, 0);
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
}
if (buttonStateRed == HIGH && buttonStateBlue == HIGH && buttonStateGreen == HIGH) {
SetColor(0, 0, 0);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
}
if (buttonStateRed == HIGH && buttonStateBlue == LOW && buttonStateGreen == HIGH) {
SetColor(0, 0, 255);
digitalWrite(blue, HIGH);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
}
if (buttonStateRed == LOW && buttonStateBlue == HIGH && buttonStateGreen == LOW) {
SetColor(255, 255, 0);
digitalWrite(green, HIGH);
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
}
}
Now lastly we code the function for our LED Strip.
void SetColor(char R,char G,char B)
{
analogWrite(REDledPin,R);
analogWrite(GREENledPin,G);
analogWrite(BLUEledPin,B);
}
Here are the combinations for the colors:
- • Red Switch: On, Blue Switch: On, Green Switch: On - White
- • Red Switch: On, Blue Switch: On, Green Switch: Off - Purple
- • Red Switch: On, Blue Switch: Off, Green Switch: Off - Red
- • Red Switch: Off, Blue Switch: On, Green Switch: On - Cyan
- • Red Switch: Off, Blue Switch: Off, Green Switch: On - Green
- • Red Switch: Off, Blue Switch: On, Green Switch: Off - Blue
- • Red Switch: On, Blue Switch: Off, Green Switch: On - Yellow
- • Red Switch: On, Blue Switch: Off, Green Switch: On - LED Strip Turns Off