4 buttons 1 rgb LED

I have been trying to build a circuit with 4 buttons and 1 rgb LED, where every color from the LED goes on while two seperate buttons are pressed. I have build this, sometimes it works perfectly, but something the light stays on or goes on with only 1 button etc.

Can anyone see where the problem is? Or should i be doing it totally different? I’m new to this and no idea what i am doing.

void setup() {
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,INPUT);
}

void loop() {
if (digitalRead(5) ==HIGH)
if (digitalRead(6) ==HIGH){
digitalWrite(9,HIGH);
}

else{digitalWrite(9,LOW);

}

if (digitalRead(12) ==HIGH)
if (digitalRead(5) ==HIGH){
digitalWrite(10,HIGH);
}

else{digitalWrite(10,LOW);

}

if (digitalRead(7) ==HIGH)
if (digitalRead(6) ==HIGH){
digitalWrite(11,HIGH);
}

else{digitalWrite(11,LOW);
}

}


Picture of my circuit

Thanks :slight_smile:

Please go read the forum guidelines in the sticky post, then edit your post above and put in the code tags and add your picture as an attachment.

I suspect lines like these are not doing what you wanted:

if (digitalRead(7) ==HIGH)
if (digitalRead(6) ==HIGH){
  digitalWrite(11,HIGH);  
}

else{digitalWrite(11,LOW);
}

The reason is that when input pin 7 is not HIGH, output pin 11 does not get updated and stays HIGH or LOW from when it was previously updated, which is when input pin 7 was HIGH.

Try this instead:

if (digitalRead(7) ==HIGH && digitalRead(6) ==HIGH){
  digitalWrite(11,HIGH);  
}
else{
  digitalWrite(11,LOW);
}

Will do, thanks for the help, i'll try it out :slight_smile: