On/Off button switch for RGB led

Hi there!
I am currently working on a small project with electronics. What I am trying to achieve is creating a light that changes colour based on the surrounding light (sensed by LDR). This has gone quite good so far. However, I am now trying to make a button 'switch' to switch the lamp on and off (it is a RGB led). Somehow, the led does not seem to switch off with my code.

Hopefully anyone can help me solving this problem.

This is my code:

#define ldr A0
#define Rpin 11
#define Bpin 10
#define Gpin 9

int Rvalue;
int Gvalue;
int Bvalue;

int ButtonPin = 2;          // the number of the input pin

int Redpin = 11;            // the number of the output pin
int Bluepin = 10;
int Greenpin = 9;

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 300;   // the debounce time, increase if the output flickers


void setup() {
  Serial.begin(9600);
  
  pinMode(ButtonPin, INPUT);
  pinMode(Redpin, OUTPUT);
  pinMode(Greenpin, OUTPUT);
  pinMode(Bluepin, OUTPUT);
}

void loop() {
  
 int x = analogRead(ldr);
  x = map(x, 0, 1023, 0, 100); // map LDR value to a scale of 0-100 (0 = no light)

reading = digitalRead(ButtonPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(Redpin, state);
  digitalWrite(Greenpin, state);
  digitalWrite(Bluepin, state);

  previous = reading;
 
// RGB control based on LDR value 

if((x <= 10)&&(state == HIGH)) // If light value less than 17 > purple
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 255);
  analogWrite(Gpin, 5);
  analogWrite(Bpin, 0);
  Serial.print("PURPLE"); Serial.println(x);
 delay(150);
}
if((x > 10 && x <= 20)&&(state == HIGH)) // If light value between 17-34 > blue
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 255);
  analogWrite(Gpin, 10);
  analogWrite(Bpin, 0);
  Serial.print("BLUE");Serial.println(x);
 delay(50);
}
if((x > 20 && x <= 30)&&(state == HIGH)) // If light value between 34-50 > green
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 255);
  analogWrite(Gpin, 20);
  analogWrite(Bpin, 0);
  Serial.print("GREEN");Serial.println(x);
 delay(150);
}
if((x > 30 && x <= 40)&&(state == HIGH)) // If light value between 60-67 > yellow
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 255);
  analogWrite(Gpin, 30);
  analogWrite(Bpin, 0);
  Serial.print("YELLOW");Serial.println(x);
 delay(150);
}
if((x > 40 && x <= 50)&&(state == HIGH)) // If light value between 67-84 > orange
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 255);
  analogWrite(Gpin, 40);
  analogWrite(Bpin, 5);
  Serial.print("ORANGE");Serial.println(x);
 delay(150);
}
if((x > 50 && x <= 60)&&(state == HIGH)) // If light value more than 84 > red
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 200); 
  analogWrite(Gpin, 40);
  analogWrite(Bpin, 10);
  Serial.print("RED");Serial.println(x);
 delay(150);
}
if((x > 60 && x <= 70)&&(state == HIGH)) // If light value more than 84 > red
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 200); 
  analogWrite(Gpin, 53);
  analogWrite(Bpin, 25);
  Serial.print("RED");Serial.println(x);
 delay(150);
}
if((x > 70 && x <= 80)&&(state == HIGH)) // If light value more than 84 > red
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 190); 
  analogWrite(Gpin, 67);
  analogWrite(Bpin, 40);
  Serial.print("RED");Serial.println(x);
 delay(150);
}
if((x > 80 && x <= 90)&&(state == HIGH)) // If light value more than 84 > red
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 180); 
  analogWrite(Gpin, 80);
  analogWrite(Bpin, 60);
  Serial.print("RED");Serial.println(x);
 delay(150);
}
if((x > 90 && x <= 100)&&(state == HIGH)) // If light value more than 84 > red
{Serial.println("Light Sensitve RGB");
  analogWrite(Rpin, 255); 
  analogWrite(Gpin, 150);
  analogWrite(Bpin, 100);
  Serial.print("RED");Serial.println(x);
 delay(150);
}
}

does your button pin need to be configured as INPUT_PULLUP?

why do you have the pins defined twice (e.g. Rpin, Redpin)

instead of retesting state in each case,

   if (! state)
        return;

instead of testing each range, you could test from one end and use if/else if

   if (90 < x)  {
    }
    else if (80 < x)  {
    }
    else if (70 < x)  {
    }
    .
    .
    .
    else {
    }

make the code easier more readable and less prone to bugs

When you have problems with conditional statements not doing what you want or expect then the first thing to do is to print the values being tested just before the test. Are they what you expect them to be ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.