Newbie needs help with LED RGB and buttons

Hi everyone,

I am playing with my Arduino uno trying to understand how does it works.
I havo no previous formation in electronic.

I wanted to use the LED RGB with 3 buttons, each button light a different color on the LED, and if no button is push the led doesn't light.
But my result was a red light and no changing by pushing the button...

This is my program :

int switchstateblue=0;
int switchstategreen=0;
int switchstatered=0;

const int blueLEDPin=10;
const int greenLEDPin=9;
const int redLEDPin=11;

int blueValue=0;
int greenValue=0;
int redValue=0;

void setup(){
  for(int pinNumber=2; pinNumber<5; pinNumber++){
    pinMode(pinNumber, INPUT);
  }
  pinMode(blueLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
}

void loop(){
  switchstateblue=digitalRead(blueValue);
  delay(5);
  switchstategreen=digitalRead(greenValue);
  delay(5);
  switchstatered=digitalRead(redValue);
  delay(5);
  
  if (switchstateblue==HIGH){
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(9, LOW);
 }else if (switchstategreen==HIGH) {
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(9, HIGH);
 }else if (switchstatered==HIGH){
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(9, LOW);
 }
 }

I don't know where is my mistake? is it in the program or in the wiring?

This is my wiring :

The LEDs have resistors. That's good. The switches do not. That's bad. The pins that the switches are connected to are floating. You can tell when the switch is pressed. You can not tell when it isn't.

Use the internal pullup resistors (pinMode(pinNumber, INPUT_PULLUP):wink: and connect one leg of the switch to the pin and the other leg to ground.

The pin will consistently read HIGH when the switch is not pressed and will consistently read LOW when it is.

First of all you wired it wrong. Switch Inputs to the arduino are all permenantly grounded. And some lines in your program makes little sense, like:
You have declared and initialized the variables blueLEDPin,redLEDPin etc... to 0 and they not changing at any time of execution of this program.

I will recommend to start with a simple sketch which will control one led by one switch and continue.

Thank you very much for your interest and your help.

I am a real noob, don't know anything about electronic and programming, I tried to mix 2 projects from the starter kit guide to do what it firstly seems to me an easy stupid thing to do, light a different color following the button pushed.
Well it is not that easy and stupid as I through ahahah!

Sorry, may I ask you to be more specific 'cause I don't understand everything you said :

  • what resistors should I use with the switch? from switch to gnd right?
  • I don't know the internal pullup resistor yet :blush:, but I think i understand what you mean for the wiring, I will try!
  • Saravananm you say the LED doesn't change, but I though this code would make them change :
  if (switchstateblue==HIGH){
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(9, LOW);
 }else if (switchstategreen==HIGH) {
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(9, HIGH);
 }else if (switchstatered==HIGH){
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(9, LOW);
  • what resistors should I use with the switch?

The internal ones.

I'd recommend that you use camelCase names for your variables. It's much easier to understand switchStateBlue than switchstateblue.

I'd recommend that you put every { on a new line.
I'd insist that nothing goes on the same line as the } (not even an else statement).
I'd recommend that you use spaces around operators.

  if (switchstateblue == HIGH)
  {
     digitalWrite(10, HIGH);
     digitalWrite(11, LOW);
     digitalWrite(9, LOW);
  }
  else if (switchstategreen == HIGH)
  {
     digitalWrite(10, LOW);
     digitalWrite(11, LOW);
     digitalWrite(9, HIGH);
  }
  else if (switchstatered == HIGH)
  {
     digitalWrite(10, LOW);
     digitalWrite(11, HIGH);
     digitalWrite(9, LOW);
  }

The structure of this code is much easier to see, in my opinion.

:slight_smile: You are right it is much easier to read, thank you very much for your tips!!

For info, I solved my problem (with help).Thanks PaulS for the suggestion!

This is the sketch:

int switchstateblue=0;
int switchstategreen=0;
int switchstatered=0;

const int blueLEDPin=10;
const int greenLEDPin=9;
const int redLEDPin=11;

int blueValue=0;
int greenValue=0;
int redValue=0;

void setup(){
  Serial.begin(9600);
  
  for(int pinNumber=2; pinNumber<5; pinNumber++){
    pinMode(pinNumber, INPUT);
  }
  pinMode(blueLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
}

void loop(){
  switchstateblue=digitalRead(2);
  delay(5);
  switchstategreen=digitalRead(3);
  delay(5);
  switchstatered=digitalRead(4);
  delay(5);
  
  /*Serial.print("Blue:");
  Serial.println(switchstateblue);
  
  Serial.print("Green:");
  Serial.println(switchstategreen);
  
  Serial.print("Red:");
  Serial.println(switchstatered);
  
  Serial.println("------------");*/

  if (switchstateblue==HIGH){
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(9, LOW);
  }
  else if (switchstategreen==HIGH) {
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(9, HIGH);
  }
  else if (switchstatered==HIGH){
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(9, LOW);
  } else {
    digitalWrite(9,LOW);
    digitalWrite(10,LOW); 
    digitalWrite(11,LOW);
  }
  
}