RGB program runs automatically


I’m in my first year civil engineering and for our first project we need to create a lamp of which we can choose the color we want. For us it’s the first time we are making an electrical circuit and the first time programming with an Arduino. It’s a project and we don’t get a lot of help from the prof.
We are trying to use 3 RGB LEDs and 3 buttons to control the brightness of the 3 colors. We have tried to adjust a program we have found on a tutorial on YouTube for 1 LED. Normally, when we press a button, that LED should shine a bit more brightly.
But when we try it out (even with only the red component in the circuit) it doesn’t work. The LED do change color but it ‘ignores’ our button and do change automatically from color. So, it’s doing the things we want but not when we want.

A friend told me that, we have put to many current over our button. But we can’t measure it, because we don’t have a multimeter at home. The person who has the Arduino at home has tried to put the resistors from the green and blue components before the button of the red component. But that didn’t work out either.

This is the program I have written.

int switchPinRed = 8;
int ledPinRed = 11;
boolean lastButtonRed = LOW;
boolean currentButtonRed = LOW;
int ledLevelRed = 0;

int switchPinGreen = 12; 
int ledPinGreen = 9;
boolean lastButtonGreen = LOW;
boolean currentButtonGreen = LOW;
int ledLevelGreen = 0;

int switchPinBlue = 7;
int ledPinBlue = 10;
boolean lastButtonBlue = LOW;
boolean currentButtonBlue = LOW;
int ledLevelBlue = 0;

void setup() {
 pinMode(switchPinRed, INPUT);
 pinMode(ledPinRed, OUTPUT);

 pinMode(switchPinGreen, INPUT);
 pinMode(ledPinGreen, OUTPUT);

 pinMode(switchPinBlue, INPUT);
 pinMode(ledPinBlue, OUTPUT);
 boolean debounceRed(boolean lastRed)
  boolean currentRed = digitalRead(switchPinRed);
  if (lastRed != currentRed)
    currentRed = digitalRead(switchPinRed); 
  return currentRed;

 boolean debounceGreen(boolean lastGreen)
  boolean currentGreen = digitalRead(switchPinGreen);
  if (lastGreen != currentGreen)
    currentGreen = digitalRead(switchPinGreen); 
  return currentGreen;
 boolean debounceBlue(boolean lastBlue)
  boolean currentBlue = digitalRead(switchPinBlue);
  if (lastBlue != currentBlue)
    currentBlue = digitalRead(switchPinBlue); 
  return currentBlue;

void loop() {
 currentButtonRed = debounceRed(lastButtonRed);
 if (lastButtonRed == LOW && currentButtonRed == HIGH)
   ledLevelRed = ledLevelRed + 51;
 lastButtonRed = currentButtonRed;
 if (ledLevelRed > 255) ledLevelRed = 0;
 analogWrite(ledPinRed, ledLevelRed);

 currentButtonGreen = debounceGreen(lastButtonGreen);
 if (lastButtonGreen == LOW && currentButtonGreen == HIGH)
   ledLevelGreen = ledLevelGreen + 51;
 lastButtonGreen = currentButtonGreen;
 if (ledLevelGreen > 255) ledLevelGreen = 0;
 analogWrite(ledPinGreen, ledLevelGreen);

 currentButtonBlue = debounceBlue(lastButtonBlue);
 if (lastButtonBlue == LOW && currentButtonBlue == HIGH)
   ledLevelBlue = ledLevelBlue + 51;
 lastButtonBlue = currentButtonBlue;
 if (ledLevelBlue > 255) ledLevelBlue = 0;
 analogWrite(ledPinBlue, ledLevelBlue);

Can someone find our mistakes? Please let me know what we can try.
Thanks for your help!


Hi Jana.

If you want help, you must ask in the right way.

You must edit your post above and put code tags around your sketch so it looks like this

Next, you must give us a schematic diagram showing how you have connected the led and switches to the Arduino.

But when we try it out (even with only the red component in the circuit) it doesn't work. The LED do change color but it 'ignores' our button and do change automatically from color.

That is because the way you have wired it is that the inputs are permanently connected to ground, no matter what you do to the button. This is how to cope with inputs:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html