RGB–Color Control

In this project, I made a circuit with three digital inputs and three digital outputs. There are three switches with different colours and three different colours’ LED lights. Red and green switches together can control the blue LED lights,  red and blue switches together control the green LED lights, and the blue and green switches control the red LED lights. If you push the three buttons at the same time, all lights will be on.

https://youtu.be/7pJ_lve0vEw

At the beginning, I had several problems with this project. The code didn’t work for the lights and switches. Both code and circuit had problems. The resistances connected to the digital output should be large. You can find the former code here.

void setup() {
  // configure the input and output pins:
  pinMode(2, OUTPUT);  // the LED blue
  pinMode(3, OUTPUT);  // the LED green
  pinMode(4, OUTPUT);  // the LED red
  pinMode(7, INPUT);   // the switch blue (pushbutton)
  pinMode(6, INPUT);   // the switch green (pushbutton)
  pinMode(5, INPUT);   // the switch red (pushbutton)

}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(5) == HIGH)  &&  digitalRead(6) == HIGH) } {
    digitalWrite(2,HIGH);
    delay(200);
  } else {
    digitalWrite(2, LOW);
    delay(200);
  }

  void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(5) == HIGH)  && digitalRead(7) == HIGH) } {
    digitalWrite(3,HIGH);
    delay(200);
  } else {
    digitalWrite(3, LOW);
    delay(200);
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(6) == HIGH)  && digitalRead(7) == HIGH) } {
    digitalWrite(4,HIGH);
    delay(200);
  } else {
    digitalWrite(4, LOW);
    delay(200);
  }

The problem is that I should not use the “void loop” for several times. In addition, I don’t need to use else in each function.

With the help of Scott and James, the right one is here.

void setup() {
  // configure the input and output pins:
  pinMode(2, OUTPUT);  // the LED blue
  pinMode(3, OUTPUT);  // the LED green
  pinMode(4, OUTPUT);  // the LED red
  pinMode(7, INPUT);   // the switch blue (pushbutton)
  pinMode(6, INPUT);   // the switch green (pushbutton)
  pinMode(5, INPUT);   // the switch red (pushbutton)

}

void loop() {

  // put your main code here, to run repeatedly:
  if (digitalRead(5) == HIGH  && digitalRead(6) == HIGH  && digitalRead(7) == HIGH) {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(2, HIGH);
    //  delay(200);
  } else  if (digitalRead(5) == HIGH  &&  digitalRead(6) == HIGH)  {
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
  } else  if (digitalRead(5) == HIGH  && digitalRead(7) == HIGH)  {
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
  } else  if (digitalRead(6) == HIGH  && digitalRead(7) == HIGH)  {
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
  } else {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
}







Finally, it works!!!

 

 

Leave a Reply