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)
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
if button pressed, set led brightness
... instead doif button pressed set variable deltaRed=1
... then further in loop() doredValue += deltaRed; light red led
GREEN == HIGH
will never be true, that's a comparison between two constants