Help with RGB cycle that starts and stops with a button press.

Hi! Im using an arduino leonardo and I am trying to create a script that cycles through the red, green, blue leds by pressing a button and it stops by pressing another button. I can only get it to stop by holding the button, really frustrated at the moment and any help would be appreciated. I have attached the script, not sure if this is the correct way to do it or if I should just paste it in this message as its my first time posting. :slight_smile:

rgbcyclewithbuttons.ino (1.09 KB)

Please read "Read this before posting a programming question" at the top of the forum.

This statement gives a warning (because the compiler can't figure out what you want):

if (buttonState1, buttonState2 == LOW) {

I think you want to know if both buttons are low, then:

if (buttonState1 && buttonState2 == LOW) {

here is the OP code

//RGB cycle with buttons.

const int buttonPin1 = 2;
const int buttonPin2 =3;
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
int counter = 0;

void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  int buttonState1;
  buttonState1 = digitalRead(buttonPin1);

  int buttonState2;
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState1, buttonState2 == LOW) {
    counter++;
    delay(150);
  }
     
  
  else if (counter == 0) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }
 
  else if (counter == 1) {
    digitalWrite(redPin, HIGH);
    delay(1000);
    digitalWrite(redPin, LOW);
    delay(1000);
    digitalWrite(greenPin, HIGH);
    delay(1000); 
    digitalWrite(greenPin, LOW);
    delay(1000);
    digitalWrite(bluePin, HIGH);
    delay(1000);
    digitalWrite(bluePin, LOW);
    delay(1000);
  }
  
  else {
   
    counter = 0;
  }

}

use pinMode(buttonPin1, INPUT_PULLUP); to force your pin to read HIGH all the time, and LOW when your button (normally open, connected between Gnd and that pin) is pressed

use 'const byte' for the pins, you save some precious RAM for that

use byte for the counter as well you are pretty sure counter wont go below 0 and won't go further than 254

when you read the pin in a loop, use

if ( digitalRead(button_pin) == LOW ) { // when pin pressed

delay(100); // debounce

// do your stuffs here

}

alright I edited my code but I am still having the issue of having to hold buttonPin1 down to stop it and it only stops after its finished the cycle. I need it stop whenever I push buttonPin2 but buttonPin2 is doing nothing. Thanks for replying!

//RGB cycle with buttons.

const byte buttonPin1 = 2;
const byte buttonPin2 = 3;
const byte redPin = 9;
const byte greenPin = 10;
const byte bluePin = 11;
byte counter = 0;

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  int buttonState1;
  buttonState1 = digitalRead(buttonPin1);
  int buttonState2;
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState1 && buttonState2 == LOW) {
    counter++;
    delay(150);
  }
     
  
  else if (counter == 0) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }
 
  else if (counter == 1) {
    digitalWrite(redPin, HIGH);
    delay(500);
    digitalWrite(redPin, LOW);
    delay(500);
    digitalWrite(greenPin, HIGH);
    delay(500); 
    digitalWrite(greenPin, LOW);
    delay(500);
    digitalWrite(bluePin, HIGH);
    delay(500);
    digitalWrite(bluePin, LOW);
    delay(500);
  }

  else if (digitalRead(buttonPin2) == LOW ) {   // when pin pressed
    delay(100);   // debounce
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
}
 
  else {
   
    counter = 0;
  }


}