Loop Conditional Statement within Switch Case Command

Good day! Noob here. I want to put a conditional statement inside a case command that is in a loop (because i think the 'break' command is preventing it). I'm using a push button to switch between 6 LED lights (modes) and within that modes i want to add another push button that when it is pressed there's 2 lights turning on and off with diff. patterns depending on the mode. Thank you for your help!

int switchPin1 = 3; 
int switchPin2 = 2; 
int led1Pin = 7;
int led2Pin = 8;
int led3Pin = 9;
int led4Pin = 10;
int led5Pin = 11;
int led6Pin = 12;
int relay1 = 5;
int relay2 = 6;



int val;          // variable for reading the pin status
int val2;         // variable for reading the delayed status
int buttonState1;  // variable to hold the button state
int buttonState2;
int Mode = 0;      // What mode is the light in?
boolean modeChanged = false;
const int NUM_MODES = 6;

void setup()
  {
     pinMode(switchPin2, INPUT);
     pinMode(switchPin1, INPUT);
     pinMode(led1Pin, OUTPUT);
     pinMode(led2Pin, OUTPUT);
     pinMode(led3Pin, OUTPUT);
     pinMode(led4Pin, OUTPUT);
     pinMode(led5Pin, OUTPUT);
     pinMode(led6Pin, OUTPUT);
     digitalWrite(switchPin1, HIGH);
     digitalWrite(switchPin2, HIGH);
  }

void loop()
  {
    buttonState2 = digitalRead(switchPin2);
    val = digitalRead(switchPin1);
    delay(100);
    val2 = digitalRead(switchPin1);
    if (val == val2)
      {
        if (val != buttonState1)
          {
            if (val == LOW)
              {
                Mode++;
                if (Mode >= NUM_MODES)
                  {
                    Mode = 0;
                  }
                modeChanged = true;
              }
          }
         buttonState1 = val;
      }
      
     if (modeChanged)
       {
         modeChanged = false;
         
         switch (Mode)
           {
             case 0:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, HIGH);
               break;
               
             case 1:
               digitalWrite(led1Pin, HIGH);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
            case 2:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, HIGH);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 3:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, HIGH);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 4:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, HIGH);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 5:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, HIGH);
               digitalWrite(led6Pin, LOW);
               break;
             
           }
           
           
   
       }
  }

Welcome to the forum.

What is not working?

Thanks for posting the code with code tags.

Show us a good schematic of your circuit. Show us a good image of your wiring.

.

What would happen if you did this at the top instead of the individual LED variables:

int ledPins[] = {12, 7, 8, 9, 10, 11};

and then took your switch/case code on mode and did this:

   // Comment out this code:
/*         
         switch (Mode)
           {
             case 0:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, HIGH);
               break;
               
             case 1:
               digitalWrite(led1Pin, HIGH);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
            case 2:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, HIGH);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 3:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, HIGH);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 4:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, HIGH);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 5:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, HIGH);
               digitalWrite(led6Pin, LOW);
               break;
             
           }
*/
// And add this line in place of them:

           WriteLeds(mode);

// And then add this new function at the bottom of the file.
void WriteLEDs(int whichPinIsHigh)
{
   int i;

   for (i = 0; i < sizeof (ledPins) / sizeof (ledPins[0]; i++) {
      if (i == whichPinIsHigh))
         digitalWrite(ledPins[i], HIGH);
      else
        digitalWrite(ledPins[i], LOW);
   }
}

With an array of LED’s, setup() could be simplified, too.

If I understand it correctly, you want to implement a second button. You can actually implement that in the beginning of your loop() just like you did for the first button.

  // read the two buttons
  valSW1 = digitalRead(switchPin1);
  valSW2 = digitalRead(switchPin2);
  // wait a little for debounce
  delay(100);
  // read the two buttons again
  debounceValSW1 = digitalRead(switchPin1);
  debounceValSW2 = digitalRead(switchPin2);

  if (valSW1 == debounceValSW1)
  {
    if (valSW1 != buttonState1)
    {
      if (valSW1 == LOW)
      {
        Mode++;
        if (Mode >= NUM_MODES)
        {
          Mode = 0;
        }
        modeChanged = true;
      }
    }
    buttonState1 = valSW1;
  }

  if (valSW2 == debounceValSW2)
  {
    if (valSW2 != buttonState2)
    {
      if (valSW2 == LOW)
      {
        ...
        ...
      }
    }
    buttonState2 = valSW2;
  }

Note that I've renamed your variables a little. For you to fill in the dots ;)

You're currently working with delay; if you want to implement a pattern (e.g. a 'walking' led) you probably need to get rid of that in favour of a millis() based approach.

Thank you for your replies.
But what I wanted to really do is,

  1. Use 1 push button to select between 6 LEDs (that will be my modes)
  2. When a mode is selected, another push button to give an output controlling the sequence of operation of 5V relay that is used for the car 12V horns.
  3. The sequence of HIGH and LOW tone is different between each mode selected.
    *Push buttons are 2 wire push-to-make.

Problems:

  1. I’m having problem with the switch case command, i dont know how to put conditional statement inside it.
  2. I want to remove delays but i dont know how to implement millis command.

Thank you!

econjack:
What would happen if you did this at the top instead of the individual LED variables:

int ledPins[] = {12, 7, 8, 9, 10, 11};

and then took your switch/case code on mode and did this:

   // Comment out this code:

/*        
        switch (Mode)
          {
            case 0:
              digitalWrite(led1Pin, LOW);
              digitalWrite(led2Pin, LOW);
              digitalWrite(led3Pin, LOW);
              digitalWrite(led4Pin, LOW);
              digitalWrite(led5Pin, LOW);
              digitalWrite(led6Pin, HIGH);
              break;
             
            case 1:
              digitalWrite(led1Pin, HIGH);
              digitalWrite(led2Pin, LOW);
              digitalWrite(led3Pin, LOW);
              digitalWrite(led4Pin, LOW);
              digitalWrite(led5Pin, LOW);
              digitalWrite(led6Pin, LOW);
              break;
           case 2:
              digitalWrite(led1Pin, LOW);
              digitalWrite(led2Pin, HIGH);
              digitalWrite(led3Pin, LOW);
              digitalWrite(led4Pin, LOW);
              digitalWrite(led5Pin, LOW);
              digitalWrite(led6Pin, LOW);
              break;
            case 3:
              digitalWrite(led1Pin, LOW);
              digitalWrite(led2Pin, LOW);
              digitalWrite(led3Pin, HIGH);
              digitalWrite(led4Pin, LOW);
              digitalWrite(led5Pin, LOW);
              digitalWrite(led6Pin, LOW);
              break;
            case 4:
              digitalWrite(led1Pin, LOW);
              digitalWrite(led2Pin, LOW);
              digitalWrite(led3Pin, LOW);
              digitalWrite(led4Pin, HIGH);
              digitalWrite(led5Pin, LOW);
              digitalWrite(led6Pin, LOW);
              break;
            case 5:
              digitalWrite(led1Pin, LOW);
              digitalWrite(led2Pin, LOW);
              digitalWrite(led3Pin, LOW);
              digitalWrite(led4Pin, LOW);
              digitalWrite(led5Pin, HIGH);
              digitalWrite(led6Pin, LOW);
              break;
           
          }
*/
// And add this line in place of them:

WriteLeds(mode);

// And then add this new function at the bottom of the file.
void WriteLEDs(int whichPinIsHigh)
{
  int i;

for (i = 0; i < sizeof (ledPins) / sizeof (ledPins[0]; i++) {
     if (i == whichPinIsHigh))
        digitalWrite(ledPins[i], HIGH);
     else
       digitalWrite(ledPins[i], LOW);
  }
}

Thank you for your help! But it didnt work like the original code.

delay(100) is about 10 times longer than most buttons need for debouncing.

What else is this going to do? You don't have any other delays.

I'm having problem with the switch case command, i dont know how to put conditional statement inside it.

You put conditional statements inside switch/case in the same way as you would do anywhere else. What is the problem ?

switch (aVariable)
{
case 0:
  if (this == true)
  {
    aVariable = 1;
  }
break:

case 1:
  if (that == true)
  {
    aVariable = 0;
  }
break:
}

UKHeliBob: You put conditional statements inside switch/case in the same way as you would do anywhere else. What is the problem ?

Im having a problem with the loop because it's only doing it one time. If i press the 2nd push button again nothing happens.

mantizaaldousbrian: Im having a problem with the loop because it's only doing it one time. If i press the 2nd push button again nothing happens.

Show your updated code.

sterretje: Show your updated code.

int switchPin1 = 3; 
int switchPin2 = 2; 
int led1Pin = 7;
int led2Pin = 8;
int led3Pin = 9;
int led4Pin = 10;
int led5Pin = 11;
int led6Pin = 12;
int relay1 = 5;
int relay2 = 6;



int val;          // variable for reading the pin status
int val2;         // variable for reading the delayed status
int buttonState1;  // variable to hold the button state
int buttonState2;
int Mode = 0;      // What mode is the light in?
boolean modeChanged = false;
const int NUM_MODES = 6;

void setup()
  {
     pinMode(switchPin2, INPUT);
     pinMode(switchPin1, INPUT);
     pinMode(led1Pin, OUTPUT);
     pinMode(led2Pin, OUTPUT);
     pinMode(led3Pin, OUTPUT);
     pinMode(led4Pin, OUTPUT);
     pinMode(led5Pin, OUTPUT);
     pinMode(led6Pin, OUTPUT);
     digitalWrite(switchPin1, HIGH);
     digitalWrite(switchPin2, HIGH);
  }

void loop()
  {
    buttonState2 = digitalRead(switchPin2);
    val = digitalRead(switchPin1);
    delay(100);
    val2 = digitalRead(switchPin1);
    if (val == val2)
      {
        if (val != buttonState1)
          {
            if (val == LOW)
              {
                Mode++;
                if (Mode >= NUM_MODES)
                  {
                    Mode = 0;
                  }
                modeChanged = true;
              }
          }
         buttonState1 = val;
      }
      
     if (modeChanged)
       {
         modeChanged = false;
         
         switch (Mode)
           {
             case 0:   //Here it is on Case 0. Thank you.
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, HIGH);
                    if (buttonState2 == LOW)
               {
                    digitalWrite(relay1, HIGH);
                    delay(300);
                    digitalWrite(relay1, LOW);
                    digitalWrite(relay2, HIGH);
                    delay(300);
                    digitalWrite(relay2, LOW);
                }
               break;
               
             case 1:
               digitalWrite(led1Pin, HIGH);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
            case 2:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, HIGH);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 3:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, HIGH);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 4:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, HIGH);
               digitalWrite(led5Pin, LOW);
               digitalWrite(led6Pin, LOW);
               break;
             case 5:
               digitalWrite(led1Pin, LOW);
               digitalWrite(led2Pin, LOW);
               digitalWrite(led3Pin, LOW);
               digitalWrite(led4Pin, LOW);
               digitalWrite(led5Pin, HIGH);
               digitalWrite(led6Pin, LOW);
               break;
             
           }
           
           
   
       }
  }

MorganS: delay(100) is about 10 times longer than most buttons need for debouncing.

What else is this going to do? You don't have any other delays.

I think i'll be having a problem for the 2nd push button on the delays if i'll know how to code it. Because when i de-press it, the 2 relays are still turning on and off for some time. I wanted it to stop as soon as i release the button.

mantizaaldousbrian: I think i'll be having a problem for the 2nd push button on the delays if i'll know how to code it. Because when i de-press it, the 2 relays are still turning on and off for some time. I wanted it to stop as soon as i release the button.

Is that why you started a new thread?

Paul

Im having a problem with the loop because it's only doing it one time. If i press the 2nd push button again nothing happens.

Your switch(Mode) code is inside a conditional loop for modeChanged. It will only run once since modeChanged is only set true when there is a debounced button press on button1. I don't see the need for a conditional test on modeChanged if you want to repeat the case code.

if (modeChanged)
       {
         modeChanged = false;
         
         switch (Mode)
           {

cattledog: Your switch(Mode) code is inside a conditional loop for modeChanged. It will only run once since modeChanged is only set true when there is a debounced button press on button1. I don't see the need for a conditional test on modeChanged if you want to repeat the case code.

Thank you for your help. What should I do then? Please see my updated code up the thread. Thank you!

mantizaaldousbrian:
What should I do then?

Is it not obvious from that post? You don’t want to have the switch case part inside an if checking for mode changing if you want to run through the switch case part even when the mode hasn’t changed.