1

DISCLAIMER : I have just started Arduino, like 2 days ago, I've been following some tutorials on YouTube, and I tried to do this myself, but I can't figure out why it isn't working, can't find what I'm doing wrong.

THE SETUP (I've already tested it with some other code that proved me that the setup is working, each buttons and each pin of the RGB LED, I know for sure that this is the code not working)

SIDE PICTURE ABOVE PICTURE

I am trying to light up the led to RED thanks to second button (from LEFT), Green with third, Blue, with fourth, and the first one to put the LED off . I am aswell trying to make a perfect visual transition between each colors going from 255 to 0 decimal..

Here is my code :

#define BLUE 10
#define GREEN 11
#define RED 5

int buttonBlueOn = 9;
int buttonGreenOn = 8;
int buttonRedOn = 3;
int buttonRgbLedOff = 2;


void setup() {
  // put your setup code here, to run once:
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(buttonRedOn, INPUT_PULLUP);
  pinMode(buttonGreenOn, INPUT_PULLUP);
  pinMode(buttonBlueOn, INPUT_PULLUP);
  pinMode(buttonRgbLedOff, INPUT_PULLUP);

}

int redValue;
int greenValue;
int blueValue;

void loop() {
  // put your main code here, to run repeatedly:

  // -------------------------------- RED ------------------------------ //

#define delayTime 20

  if (digitalRead(buttonRedOn) == LOW)
  {
    digitalWrite(RED, HIGH);
    redValue = 255;
  }

  if (digitalRead(buttonRedOn) == LOW and GREEN == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out green brings red full
    {
      redValue += 1;
      greenValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  if (digitalRead(buttonRedOn) == LOW and BLUE == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out blue brings red full
    {
      redValue += 1;
      blueValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(BLUE, blueValue);
      delay(delayTime);
    }
  }


  // -------------------------------- GREEN ------------------------------ //

  if (digitalRead(buttonGreenOn) == LOW)
  {
    digitalWrite(GREEN , HIGH);
    greenValue = 255;
  }

  if (digitalRead(buttonGreenOn) == LOW and RED == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out red brings green full
    {
      greenValue += 1;
      redValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  if (digitalRead(buttonGreenOn) == LOW and BLUE == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out blue brings green full
    {
      greenValue += 1;
      blueValue -= 1;
      analogWrite(BLUE, blueValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  // -------------------------------- BLUE ------------------------------ //

  if (digitalRead(buttonBlueOn) == LOW)
  {
    digitalWrite(BLUE, HIGH);
    blueValue = 255;
  }

  if (digitalRead(buttonBlueOn) == LOW and RED == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out red brings blue full
    {
      blueValue += 1;
      redValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(BLUE, blueValue);
      delay(delayTime);
    }
  }

  if (digitalRead(buttonBlueOn) == LOW and GREEN == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out green brings blue full
    {
      blueValue += 1;
      greenValue -= 1;
      analogWrite(BLUE, blueValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  // -------------------------------- OFF ------------------------------ //


  if (digitalRead (buttonRgbLedOff) == LOW)
  {
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, LOW);
    digitalWrite(BLUE, LOW);
  }


}

WHAT IT DOES : Nothing at all, nothing lights up, nothing happens, I'm a bit confused

I am so new to this, sorry if I'm doing something wrong on this forum

4
  • 2
    Looks like you did not connect the buttons to ground. Apr 14, 2022 at 9:58
  • 3
    You have connected the buttons to the blue power rail of the breadboard, but you haven't connected that to ground on the Arduino
    – chrisl
    Apr 14, 2022 at 11:13
  • insert a layer of separation between the detection of a keypress and the resulting change to LED brightness ... do not do if button pressed, set led brightness ... instead do if button pressed set variable deltaRed=1 ... then further in loop() do redValue += deltaRed; light red led
    – jsotola
    Apr 14, 2022 at 17:36
  • GREEN == HIGH will never be true, that's a comparison between two constants
    – Ben Voigt
    Apr 14, 2022 at 19:06

1 Answer 1

2

Your program makes things too complex. You should rethink your whole strategy.

My preferred method is to have a "target" colour and a "current" colour. When you press a button you simply change what your target colour is. And then every iteration through loop() (either slowed down with a short delay() or triggered with millis()) you compare the target and current colours, and if they differ then move the current colour a step closer to the target colour.

That way you remove all the logic of "if this is pressed but not this then fade between these two colours", instead it's just "if this is pressed then I want to go to this colour" and the changing of the colour is completely independent.

I wrote a blog post on the topic some time ago that you might find helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.