RGB LED Strip Changes with Button Push

Hi All,

I have a project with an LED Strip that I have been able to compile correctly but haven’t got the desired result. I have one of the older style LED strips, 12V with separate R, G and B channels. I’d like them to stay white when switched on, but do a rainbow transition effect when a button is pushed. The button is a reed switch, and using the serial monitor, I’ve been able to verify the signal is being received.

I am using a standard Arduino Uno and have the LED strip powered with a separate power supply. When switched on, the LED strip stays white when the button is pushed and no rainbow sequence is played. I think I have not written the code correctly to work for this, any ideas/advice are much appreciated.

// LED Strip 
// This controls an LED strip to turn on white, and play a rainbow sequence when a button is pushed. 

#define REDPIN 11
#define GREENPIN 10
#define BLUEPIN 12
 
const int buttonPin = 9;
int r, g, b;

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(buttonPin, INPUT);
  
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
byte buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
  Serial.println("Button On");
   
} else {
  Serial.println("Button Off");


if (buttonState == HIGH) {

  // fade from blue to violet
 for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(200);
  } 
  // fade from violet to red
  for(b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(200);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(200);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(200);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(200);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(200);
  
  } }
 else {
 for (g = 255; b = 255; r = 255);
  analogWrite(GREENPIN,g);
  analogWrite(BLUEPIN, b);
  analogWrite(REDPIN, r);
 }

}
}

I think you had better show us the circuit you are using with your FETs to switch the LED strip and preferably, a photo for confirmation (photos always taken if full outside daylight but not direct sun, with perfect focus not less than 4 Megapixels!).

sjpiper145,

To me, it looked like you had a bracket out of place.

For me, I try to keep my logic together as much as I can, so allow me to suggest some slight changes. See attached code.

See if this works better. I can’t test because I don’t have those type of LEDs.

// LED Strip
// This controls an LED strip to turn on white, and play a rainbow sequence when a button is pushed.

#define REDPIN 11
#define GREENPIN 10
#define BLUEPIN 12
 
const int buttonPin = 9;
int r, g, b;

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(buttonPin, INPUT);
 
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
byte buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
  Serial.println("Button Off");
   for (g = 255; b = 255; r = 255);
     analogWrite(GREENPIN,g);
     analogWrite(BLUEPIN, b);
     analogWrite(REDPIN, r);
   
} 
  else {
  Serial.println("Button On");

  // fade from blue to violet
 for (r = 0; r < 256; r++) {
    analogWrite(REDPIN, r);
    delay(200);
  }
  // fade from violet to red
  for(b = 255; b > 0; b--) {
    analogWrite(BLUEPIN, b);
    delay(200);
  }
  // fade from red to yellow
  for (g = 0; g < 256; g++) {
    analogWrite(GREENPIN, g);
    delay(200);
  }
  // fade from yellow to green
  for (r = 255; r > 0; r--) {
    analogWrite(REDPIN, r);
    delay(200);
  }
  // fade from green to teal
  for (b = 0; b < 256; b++) {
    analogWrite(BLUEPIN, b);
    delay(200);
  }
  // fade from teal to blue
  for (g = 255; g > 0; g--) {
    analogWrite(GREENPIN, g);
    delay(200);
 
  }
 }
}

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