Help with code for LED strip and momentary buttons

Hello. I haven't used Arduino in a while, so I'm a little rusty. I'm trying to control by LED strip lights with buttons to change the color and enter a "rainbow" mode to cycle through. I'm waiting on parts to come in so I haven't been able to test this yet, but I wanted to get the code done in the meantime. I think I have it figured out, but would like someone else to look it over and let me know if they see anything wrong with it.

My goal is to press a button and have the LEDs that color until a different button is pressed. I'm just not sure if I am using the if and else if statements correctly.

I guess my main questions are:
Are the if and else if used correctly?
Will the code work with pressing buttons rapidly?

Any feedback would be greatly appreciated. Thanks!

#define red 10
#define green 9
#define blue 11
#define rainbow_button 2
#define red_button 3
#define blue_button 4
#define green_button 5
#define white_button 6
#define purple_button 7
#define yellow_button 8
#define cyan_button 12
#define maxbright 255
int rbright = 0;
int gbright = 0;
int bbright = 0;

void setup() {
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(rainbow_button,INPUT);
pinMode(red_button,INPUT);
pinMode(blue_button,INPUT);
pinMode(green_button,INPUT);
pinMode(white_button,INPUT);
pinMode(purple_button,INPUT);
pinMode(yellow_button,INPUT);
pinMode(cyan_button,INPUT);
}

void loop() {
  int rainbow_state = digitalRead(rainbow_button);
  int red_state = digitalRead(red_button);
  int blue_state = digitalRead(blue_button);
  int green_state = digitalRead(green_button);
  int white_state = digitalRead(white_button);
  int purple_state = digitalRead(purple_button);
  int yellow_state = digitalRead(yellow_button);
  int cyan_state = digitalRead(cyan_button);
  int last_button;
  
  if(rainbow_state == HIGH){
    last_button = rainbow_button;
  }
  else if(red_state == HIGH){
    last_button = red_button;
  }
  else if(blue_state == HIGH){
    last_button = blue_button;
  }
  else if(green_state == HIGH){
    last_button = green_button;
  }
  else if(white_state == HIGH){
    last_button = white_button;
  }
  else if(purple_state == HIGH){
    last_button = purple_button;
  }
  else if(yellow_state == HIGH){
    last_button = yellow_button;
  }
  else if(cyan_state == HIGH){
    last_button = cyan_button;
  }
  
  if(last_button == rainbow_button){
    rainbow();
  }
  else if(last_button == red_button){
    red_on();
  }
  else if(last_button == blue_button){
    blue_on();
  }
  else if(last_button == green_button){
    green_on();
  }
  else if(last_button == white_button){
    white_on();
  }
  else if(last_button == purple_button){
    purple_on();
  }
  else if(last_button == yellow_button){
    yellow_on();
  }
  else if(last_button == cyan_button){
    cyan_on();
  }
}

void rainbow(){
  analogWrite(red,maxbright);
  for(bbright = 0;bbright<=maxbright;bbright++){
    analogWrite(blue,bbright);
    delay(10);
  }
  for(rbright = 255;rbright>0;rbright-=1){
    analogWrite(red,rbright);
    delay(10);
  }  
  for(gbright = 0;gbright<=maxbright;gbright++){
    analogWrite(green,gbright);
    delay(10);
  }
  for(bbright = 255;bbright>0;bbright-=1){
    analogWrite(blue,bbright);
    delay(10);
  }  
  for(rbright = 0;rbright<=maxbright;rbright++){
    analogWrite(red,rbright);
    delay(10);
  }
  for(gbright = 255;gbright>0;gbright-=1){
    analogWrite(green,gbright);
    delay(10);
  }  
}

void red_on(){
  analogWrite(red,maxbright);
}
void blue_on(){
  analogWrite(blue,maxbright);
}
void green_on(){
  analogWrite(green,maxbright);
}
void purple_on(){
  analogWrite(red,maxbright);
  analogWrite(blue,maxbright);
}
void yellow_on(){
  analogWrite(red,maxbright);
  analogWrite(green,maxbright);
}
void cyan_on(){
  analogWrite(green,maxbright);
  analogWrite(blue,maxbright);
}
void white_on(){
  analogWrite(red,maxbright);
  analogWrite(blue,maxbright);
  analogWrite(green,maxbright);
}

if you need to recognize a button press from a number of buttons i suggest you consider

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

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