Controlling multiple RGBs using different switches

Hello to everyone!

My name is Martin and I just registered here since the problem I'm currently facing with my Arduino Lilypad is driving me crazy and I can't find solution over the Internet.

My project involves 3 RGB LEDs which i want to control in this way: pressing red button will turn on all LEDs in red colour, green button will turn on all LEDs in green colour and so on (I wonder if it's possible to also program a button which will turn LEDs randomly?)

OK, how i connected everything: Each R G B pin is connected to different digital I/O (so to sum up i used 9 pin slots on my Lilypad board) plus last pin of each LED to (+) on tab. 4 basic buttons/ switches to 4 different digital I/O and to (-) tab on my Lilypad board.

I tried example codes but everything I came to was that: after compiling the program for let's say one button, it immediately lighted one diode and the switch only turned off LED and after that diode again started to shine. I would like to achieve a situation like when I press the button the RGB LED will turn on, when i press the same button again it will turn off or by pressing other button will change the colour to different one (for example I pushed red button and the all LEDs turned on red and now I'm pressing blue button and the all LEDs turn into blue colour.

Is there any wrong connection or something like that.? Any suggestions what I can do? I'm not so good in programming and electronics but I would like to try to finish that project. Thanks so much in advance and for your time and for any tips :)

Best regards, Martin

gromar: Any suggestions what I can do?

Hi Martin, post your sketch. Use code tags (the # button).


Thank you for the respond :) At the moment my code looks like this (I'm trying to light up one RGB [doesn't matter which colour] by pressing single button - but it works like I wrote above - LED flashes all the time and the button turns it off (if u let go the button RGB turns on); I want achieve a situation where button turns on RGB and stay like this until the button will be pressed again and turns RGB off.

int ledPin = 12;    // LED is connected to digital pin 13
int switchPin = 3;  // switch connected to digital pin 2
int switchValue;    // a variable to keep track of when switch is pressed

void setup()     
         pinMode(ledPin, OUTPUT);   // sets the ledPin to be an output
         pinMode(switchPin, INPUT); // sets the switchPin to be an input
         digitalWrite(switchPin, HIGH); // sets the default (unpressed) state of switchPin to HIGH

void loop() // run over and over again
          switchValue = digitalRead(switchPin);    // check to see if the switch is pressed
          if (switchValue == LOW) {    // if the switch is pressed then,
                    digitalWrite(ledPin, HIGH);   // turn the LED on
          else {   // otherwise,
                    digitalWrite(ledPin, LOW);    // turn the LED off

I thought about this code but at the end the button doesn't respond - RGB is on all the time:

//Button Toggle LED

int ledPin = 12;
int buttonPin = 3;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)

void setup(){
  pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);//initialize Serial connection

void loop(){
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW){//if button has just been pressed
    delay(1);//crude form of button debouncing
    //toggle the state of the LED
    if (ledState == HIGH){
      digitalWrite(ledState, LOW);
      ledState = LOW;
    } else {
      digitalWrite(ledState, HIGH);
      ledState = HIGH;
  lastState = currentState;