ULN2003 LED Strip Controller (2)

Hi community! Today's project is gonna be about the ULN2003! This IC is gonna control a LED Strip with Arduino! So let's not waste any time and dive in!

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:

  1. • Red Switch: On, Blue Switch: On, Green Switch: On - White
  2. • Red Switch: On, Blue Switch: On, Green Switch: Off - Purple
  3. • Red Switch: On, Blue Switch: Off, Green Switch: Off - Red
  4. • Red Switch: Off, Blue Switch: On, Green Switch: On - Cyan
  5. • Red Switch: Off, Blue Switch: Off, Green Switch: On - Green
  6. • Red Switch: Off, Blue Switch: On, Green Switch: Off - Blue
  7. • Red Switch: On, Blue Switch: Off, Green Switch: On - Yellow
  8. • Red Switch: On, Blue Switch: Off, Green Switch: On - LED Strip Turns Off