Problem with RGB led and small buttons

Hi, I've just started learning Arduino, and I'm trying to make an RGB led "add" a certain amount of red light when I click on one button, the same amount of blue light when I click on another button, and the same amount of green light when I click on the third button.
When I tried the project none of the buttons worked... I'm using Arduino 1.8.8 for Windows

Comments are in Italian then in English

/quando premo un pulsante "aggiungo" un tot di quel colore al led RGB, dopo aver rilasciato il pulsante il led resta acceso/
/when I click on a button I "add" an amount of a certain colour, and after releasing the
button the led remains on
/
//definisco come costanti le porte a cui collego il led (analogico) e i bottoni (digitale)
//I define as constans the analogic pins of the led and the digital pins of the buttons
#define R_LED_PIN A7
#define V_LED_PIN A6
#define B_LED_PIN A5
#define R_BUTTON_PIN 7
#define V_BUTTON_PIN 6
#define B_BUTTON_PIN 5
void setup(){
//definisco come input i bottoni e come output il led
//I define as input the buttons and as output the led
pinMode(V_LED_PIN, OUTPUT);
pinMode(R_LED_PIN, OUTPUT);
pinMode(B_LED_PIN, OUTPUT);
pinMode(R_BUTTON_PIN, INPUT);
pinMode(V_BUTTON_PIN, INPUT);
pinMode(B_BUTTON_PIN, INPUT);
}

void loop(){
//quando il bottone rosso viene premuto aggiungo al led "25" di rosso al led RGB
//when i click on the red button "25" of red light are added to the RGB led
int valR, valPinR;
valPinR=0;
valR=digitalRead(R_BUTTON_PIN);
if(valR==HIGH){
do{
valPinR=valPinR+25;
analogWrite(R_LED_PIN, valPinR);
valR=digitalRead(R_BUTTON_PIN);
delay(50);
}while(valR==HIGH);
}

//quando il bottone verde viene premuto aggiungo al led "25" di verde
//when i click on the green button "25" of green light are added to the RGB led
int valV, valPinV;
valPinV=0;
valV=digitalRead(V_BUTTON_PIN);
if(valV==HIGH){
do{
valPinV=valPinV+25;
analogWrite(V_LED_PIN, valPinV);
valV=digitalRead(V_BUTTON_PIN);
delay(50);
}while(valV==HIGH);
}

//quando il bottone blu viene premuto aggiungo al led "25" di blu
//when i click on the blue button "25" of blue light are added to the RGB led
int valB, valPinB;
valPinB=0;
valB=digitalRead(B_BUTTON_PIN);
if(valB==HIGH){
do{
valPinB=valPinR+25;
analogWrite(B_LED_PIN, valPinR);
valR=digitalRead(B_BUTTON_PIN);
delay(50);
}while(valB==HIGH);
}
}

Hello, welcome :slight_smile:

I suggest you use this library for handling buttons : GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses

Here is an example that does what you want, in a better way!

#include <Arduino.h>
#include <JC_Button.h>

Button buttonIncrease( 15 );

void setup()
{
  Serial.begin( 115200 );
  buttonIncrease.begin();
}

void loop()
{
  buttonIncrease.read();

  static uint32_t buttonIncrease_lastCheck = 0;
  static uint8_t value = 0;
    
  if ( buttonIncrease.wasPressed() || ( buttonIncrease.pressedFor(500) && millis() - buttonIncrease_lastCheck >= 100UL ) )
  {
    buttonIncrease_lastCheck = millis();
    if ( value <= 255 - 15 )
    {
      value += 15;
      Serial.println( value );
    }
  }
}

When using push buttons, you should use a pullup resistor (internal or external). Your sketch could be much simpler if you used TButton from TDuino:

#define INCREMENT_BY 5
#define BUTTON_COUNT 3

const byte BUTTON_PINS[BUTTON_COUNT] = { 7, 6, 5 }; //R, G, B
const byte LED_PINS[BUTTON_COUNT] = { A7, A6, A5 }; //R, G, B
byte PIN_VALUES[BUTTON_COUNT] = { 0, 0, 0 }; //R, G, B
TButton buttons[BUTTON_COUNT];


void increment_pin(byte index)
{
  int new_value = PIN_VALUES[index];
  new_value += INCREMENT_BY;
  if (new_value > 255) new_value = 255;
  PIN_VALUES[index] = new_value;
  analogWrite(LED_PINS[i], new_value);
}

void button_press(byte pin, int state)
{
  for (byte i = 0; i < BUTTON_COUNT; i++)
  {
    if (pin == BUTTON_PINS[i])
    {
      increment_pin(i);
      return;
    }
  }
}

void setup()
{
  for (byte i = 0; i < BUTTON_COUNT; i++)
  {
    buttons[i].attach(BUTTON_PINS[i]);
    buttons[i].onPress(button_press);
    buttons[i].setRepeat(500, 250); //Allows you to hold down the button(s), like on a keyboard

    pinMode(LED_PINS[i], OUTPUT);
    analogWrite(LED_PINS[i], PIN_VALUES[i]);
  }
}

void loop()
{
  for (byte i = 0; i < BUTTON_COUNT; i++) buttons[i].loop();
}

The code above will allow you to activate multiple buttons at once.