rgb colour switch runs only once

Hi everybody,

I have a small project where with the help of 3 buttons i want to switch an RGBW led strip, so that one button turns on only the white leds, one button runs a continuous rgb colour switch loop and the third button to turns all the leds off.
i have managed to get the rgb colour switch working, but when i pust the button the colour switch runs only once and then it stays on at the last colour.

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int whitePin = 6;
const int whiteButton = 2;
const int colourButton = 3;
const int offButton = 4;

const int analogInPin0 = A0;  // Analog input pin that the potentiometer is attached to

int wait = 400;

int colourButtonState = HIGH;         // current state of the button
int reading;
int previous = LOW; 

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers


void setup() {
  pinMode(colourButton, INPUT);
}
void loop() {
  reading = digitalRead(colourButton);

  // 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 (colourButtonState == HIGH)
      colourButtonState = LOW;
    else
      colourButtonState = HIGH;

    time = millis(); {   
    
 analogWrite(redPin,  255);
 analogWrite(greenPin, 0);
 analogWrite(bluePin, 0);

 delay(wait);
  
 analogWrite(redPin, 0);
 analogWrite(greenPin, 255);
 analogWrite(bluePin, 0);

 delay(wait);

 analogWrite(redPin, 0);
 analogWrite(greenPin, 0);
 analogWrite(bluePin, 255);

 delay(wait);

 analogWrite(redPin, 255);
 analogWrite(greenPin, 255);
 analogWrite(bluePin, 0);

 delay(wait);

 analogWrite(redPin, 80);
 analogWrite(greenPin, 0);
 analogWrite(bluePin, 80);

 delay(wait);

 analogWrite(redPin, 0);
 analogWrite(greenPin, 255);
 analogWrite(bluePin, 255);

 delay(wait);
 }
 
 previous = reading;
 }
}

Any help would be greatly appreciated!

Rolf102

You take some action only when the switch changes state. When you press the switch, it changes to pressed only once, until you release it, when it changes state to released.

Your program is doing exactly what you describe, and exactly what I would expect.

If you have different expectations, they are incorrect.

But i would like it to run the colour switch continuously

But i would like it to run the colour switch continuously

I really have no idea what that means. You can't, in my world, "run a switch".

If you want something to start happening when a switch becomes pressed, that is possible.
If you want something to happen while a switch is pressed, that is possible.

How depends on what you actually want, which is not at all clear.

You have your curly braces messed up, easier to see if you use Tools=>Auto Format. Look at this section formatted:

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (colourButtonState == HIGH)
      colourButtonState = LOW;
    else
      colourButtonState = HIGH;

    time = millis(); 
    {

I think that you meant for that last curly brace to be turned the other way to close the if statement. The way it is now the color switch code is inside the if statement, so it makes sense that it only runs once.

After that you might have wanted an if statement based on colourButtonState?