Using Buttons to Cycle between Functions

Hello all,

This is my first project with arduino, and I’m running into some issues with trying to use a button to advance a state counter to select different functions.

Basically, one button should cycle through the cases, and restart at the end. The other button should always go to state 1.

Right now, its not cycling through states, just staying at state 0.

Any help would be greatly appreciated.

This is my current code for the selection section.

// Initialize

int RED_LED_1 = 3;
int GREEN_LED_1 = 4;
int BLUE_LED_1 = 5;
int RED_LED_2 = 6;
int GREEN_LED_2 = 9;
int BLUE_LED_2 = 10;
int RED_LED_3 = 23;
int GREEN_LED_3 = 22;
int BLUE_LED_3 = 21;

int BUTTON1 = 14; // Button that makes LED's go white
int BUTTON2 = 15; // Button that cycles through modes 

int state;
int val_1;
int val_2;
int old_val_2;
int NumberofModes = 5;

int t = 10; // Time for delay

void setup() {
  pinMode(RED_LED_1, OUTPUT);
  pinMode(GREEN_LED_1, OUTPUT);
  pinMode(BLUE_LED_1, OUTPUT);
  pinMode(RED_LED_2, OUTPUT);
  pinMode(GREEN_LED_2, OUTPUT);
  pinMode(BLUE_LED_2, OUTPUT);
  pinMode(RED_LED_3, OUTPUT);
  pinMode(GREEN_LED_3, OUTPUT);
  pinMode(BLUE_LED_3, OUTPUT);

  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);

  state = 1 ;
  val_1 = 0 ;
  val_2 = 0 ;
  old_val_2 = 0 ;
}

void loop()
{
  val_1 = digitalRead(BUTTON1);
  if (val_1 == HIGH)
  {
    state = 1;
  }
  val_2 = digitalRead(BUTTON2); //Reads the value of the button and stores
  if ((val_2 == HIGH) && (old_val_2 == LOW) && (state < NumberofModes))
  {
    state = state + 1; 
    // delay(10);
  }
  else if ((val_2 == HIGH) && (old_val_2 == LOW) && (state > (NumberofModes - 1)))
  {
    state = 1;
  }

  old_val_2 = val_2; // val_2 is now old val_2 so its stored

  switch (state)
  {
    case 1:
      Fade();                //Runs Function 1 - Rainbow Fade
      break;
    case 2:
      White();               //Runs Function 2 - White
      break;
    case 3:
      Random();       //Runs Function 3 - Random
      break;
    case 4:
      Merica_Static();              //Runs Function 3 - 'Merica Static
      break;
    case 5:
      Merica_Dynamic();              //Runs Function 3 - 'Merica Dynamic
      break;
  }
}

This is not the complete sketch!

Show us your wiring.

These: pinMode(BUTTON1, INPUT); pinMode(BUTTON2, INPUT); Might have to be pinMode(BUTTON1, INPUT_PULLUP); pinMode(BUTTON2, INPUT_PULLUP);

Basically, one button should cycle through the cases, and restart at the end.

Its far more common to re-start at the beginning.

 pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);

Wiring is far simpler if you use INPUT_PULLUP. Just how ARE the switches wired? Why are you calling the switches buttons?

Hello, thank you for your quick replies, I have adjust my code and wiring to have the pins be used with the internal Pull-up resistors. Below is my entire code.

// Initialize

int RED_LED_1 = 3;
int GREEN_LED_1 = 4;
int BLUE_LED_1 = 5;
int RED_LED_2 = 6;
int GREEN_LED_2 = 9;
int BLUE_LED_2 = 10;
int RED_LED_3 = 23;
int GREEN_LED_3 = 22;
int BLUE_LED_3 = 21;

int BUTTON1 = 14; // Button that makes LED's go white
int BUTTON2 = 15; // Button that cycles through modes 

int state;
int val_1;
int val_2;
int old_val_2;
int NumberofModes = 5;

int t = 10; // Time for delay

void setup() {
  pinMode(RED_LED_1, OUTPUT);
  pinMode(GREEN_LED_1, OUTPUT);
  pinMode(BLUE_LED_1, OUTPUT);
  pinMode(RED_LED_2, OUTPUT);
  pinMode(GREEN_LED_2, OUTPUT);
  pinMode(BLUE_LED_2, OUTPUT);
  pinMode(RED_LED_3, OUTPUT);
  pinMode(GREEN_LED_3, OUTPUT);
  pinMode(BLUE_LED_3, OUTPUT);

  pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);

  state = 1 ;
  val_1 = 0 ;
  val_2 = 0 ;
  old_val_2 = 0 ;
}

void loop()
{
  val_1 = digitalRead(BUTTON1);
  if (val_1 == HIGH)
  {
    state = 1;
  }
  val_2 = digitalRead(BUTTON2); //Reads the value of the button and stores
  if ((val_2 == HIGH) && (old_val_2 == LOW) && (state < NumberofModes))
  {
    state = state + 1; 
    // delay(10);
  }
  else if ((val_2 == HIGH) && (old_val_2 == LOW) && (state > (NumberofModes - 1)))
  {
    state = 1;
  }

  old_val_2 = val_2; // val_2 is now old val_2 so its stored

  switch (state)
  {
    case 1:
      Fade();                //Runs Function 1 - Rainbow Fade
      break;
    case 2:
      White();               //Runs Function 2 - White
      break;
    case 3:
      Random();       //Runs Function 3 - Random
      break;
    case 4:
      Merica_Static();              //Runs Function 3 - 'Merica Static
      break;
    case 5:
      Merica_Dynamic();              //Runs Function 3 - 'Merica Dynamic
      break;
  }
}


void Fade() {
  for ( int i = 255; i >= 0; i--) {
    analogWrite(RED_LED_1, i); // fade up
    analogWrite(GREEN_LED_1, 255 - i); // fade down
    analogWrite(BLUE_LED_1, 0); // do nothing
    analogWrite(RED_LED_2, i); // fade up
    analogWrite(GREEN_LED_2, 255 - i); // fade down
    analogWrite(BLUE_LED_2, 0); // do nothing
    analogWrite(RED_LED_3, i); // fade up
    analogWrite(GREEN_LED_3, 255 - i); // fade down
    analogWrite(BLUE_LED_3, 0); // do nothing
    delay(t);
  }
  for ( int i = 255; i >= 0; i--) {
    analogWrite(RED_LED_1, 255 - i); // fade down
    analogWrite(GREEN_LED_1, 0); // do nothing
    analogWrite(BLUE_LED_1, i); // fade up
    analogWrite(RED_LED_2, 255 - i); // fade down
    analogWrite(GREEN_LED_2, 0); // do nothing
    analogWrite(BLUE_LED_2, i); // fade up
    analogWrite(RED_LED_3, 255 - i); // fade down
    analogWrite(GREEN_LED_3, 0); // do nothing
    analogWrite(BLUE_LED_3, i); // fade up
    delay(t);
  }
  for ( int i = 255; i >= 0; i--) {
    analogWrite(RED_LED_1, 0); // do nothing
    analogWrite(GREEN_LED_1, i); // fade up
    analogWrite(BLUE_LED_1, 255 - i); // fade down
    analogWrite(RED_LED_2, 0); // do nothing
    analogWrite(GREEN_LED_2, i); // fade up
    analogWrite(BLUE_LED_2, 255 - i); // fade down
    analogWrite(RED_LED_3, 0); // do nothing
    analogWrite(GREEN_LED_3, i); // fade up
    analogWrite(BLUE_LED_3, 255 - i); // fade down
    delay(t);
  }
}


void White() {
  analogWrite(RED_LED_1, 0);
  analogWrite(GREEN_LED_1, 0);
  analogWrite(BLUE_LED_1, 0);

  analogWrite(RED_LED_2, 0);
  analogWrite(GREEN_LED_2, 0);
  analogWrite(BLUE_LED_2, 0);

  analogWrite(RED_LED_3, 0);
  analogWrite(GREEN_LED_3, 0);
  analogWrite(BLUE_LED_3, 0);
}


void Random(){
//R:0-255 G:0-255 B:0-255
colorRGB1(random(0,255),random(0,255),random(0,255));
colorRGB2(random(0,255),random(0,255),random(0,255));
colorRGB3(random(0,255),random(0,255),random(0,255));
delay(1000);
}
void colorRGB1(int red, int green, int blue){
analogWrite(RED_LED_1,constrain(red,0,255));
analogWrite(GREEN_LED_1,constrain(green,0,255));
analogWrite(BLUE_LED_1,constrain(blue,0,255));
}
void colorRGB2(int red, int green, int blue){
analogWrite(RED_LED_2,constrain(red,0,255));
analogWrite(GREEN_LED_2,constrain(green,0,255));
analogWrite(BLUE_LED_2,constrain(blue,0,255));
}
void colorRGB3(int red, int green, int blue){
analogWrite(RED_LED_3,constrain(red,0,255));
analogWrite(GREEN_LED_3,constrain(green,0,255));
analogWrite(BLUE_LED_3,constrain(blue,0,255));
}


void Merica_Static() {
  analogWrite(RED_LED_1, 0);
  analogWrite(GREEN_LED_1, 255);
  analogWrite(BLUE_LED_1, 255);

  analogWrite(RED_LED_2, 0);
  analogWrite(GREEN_LED_2, 0);
  analogWrite(BLUE_LED_2, 0);

  analogWrite(RED_LED_3, 255);
  analogWrite(GREEN_LED_3, 255);
  analogWrite(BLUE_LED_3, 0);
}


void Merica_Dynamic() {
  for(int i=255; i >= 0; i--) {
    // LED1 fade from white to red
    analogWrite(RED_LED_1, i);
    analogWrite(GREEN_LED_1, 255-i);
    analogWrite(BLUE_LED_1, 255-i);
    
    // LED2 fade from red to blue
    analogWrite(RED_LED_2, 255-i); 
    analogWrite(GREEN_LED_2, 255); 
    analogWrite(BLUE_LED_2, i);

    // LED3 fade from blue to white
    analogWrite(RED_LED_3, i); 
    analogWrite(GREEN_LED_3, i);
    analogWrite(BLUE_LED_3, i); 
    delay(t*2);
  }

  for(int i=255; i >= 0; i--) {
    // LED1 fade from red to blue
    analogWrite(RED_LED_1, 255-i);
    analogWrite(GREEN_LED_1, 255);
    analogWrite(BLUE_LED_1, i);

    // LED2 fade from blue to white
    analogWrite(RED_LED_2, i);
    analogWrite(GREEN_LED_2, i);
    analogWrite(BLUE_LED_2, i);

    // LED3 fade from white to red
    analogWrite(RED_LED_3, i);
    analogWrite(GREEN_LED_3, 255-i);
    analogWrite(BLUE_LED_3, 255-i);
    delay(t*2);
  }

  for(int i=255; i >= 0; i--) {
    // LED1 fade from blue to white
    analogWrite(RED_LED_1, i);
    analogWrite(GREEN_LED_1, i);
    analogWrite(BLUE_LED_1, i);

    // LED2 fade from white to red
    analogWrite(RED_LED_2, i);
    analogWrite(GREEN_LED_2, 255-i);
    analogWrite(BLUE_LED_2, 255-i);

    // LED3 fade from red to blue
    analogWrite(RED_LED_2, 255-i);
    analogWrite(GREEN_LED_2, 255);
    analogWrite(BLUE_LED_2, i);
    delay(t*2);
  }
}

Currently my wiring goes from my board, on pins 14 and 15, attach to a push button which connects the pins to GND when pressed.

The code still does not seem to work.

 pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);

With the change to INPUT_PULLUP you need to look for a LOW when pressed. Reverse the conditiional logic in this section.

val_1 = digitalRead(BUTTON1);
  if (val_1 == HIGH)
  {
    state = 1;
  }
  val_2 = digitalRead(BUTTON2); //Reads the value of the button and stores
  if ((val_2 == HIGH) && (old_val_2 == LOW) && (state < NumberofModes))
  {
    state = state + 1; 
    // delay(10);
  }
  else if ((val_2 == HIGH) && (old_val_2 == LOW) && (state > (NumberofModes - 1)))
  {
    state = 1;
  }

  old_val_2 = val_2; // val_2 is now old val_2 so its stored