if statement within a switch case??

Hi guys,

Is it possible to have an if statement within a switch case for example:

  switch(counter)
  {
    case 0:
      mainMenu();
      time();
      break;      
    case 1:
      lcd.setCursor(0,1);
      lcd.write(Symbol_2);
        lcd.setCursor(0,2);
        lcd.write(Symbol_1);
          lcd.setCursor(0,3);
          lcd.write(Symbol_1);
            lcd.setCursor(10,1);
            lcd.write(Symbol_1);
            break;
    case 2:
      if (counter == 1)
      {
          delay(300);
          lcd.clear();
          pumpStatus("Pump One");
       }
       break;
  }

Is it possible to have an if statement within a switch case

Yes. But, if the switch statement case selected case 2 because counter contained 2, then the likelihood of counter equaling 1 is pretty slim.

and the curly braces would be as shown?

Junblud:
and the curly braces would be as shown?

Yes, but the if statement will never be true.

yes i am experiencing what you said. Maybe i think the answer is another counter? so how do i stop the first counter and start another? hmm?

thanks for your help! :slight_smile:

this is what i have so far:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
#include <TimeAlarms.h>
#include <Time.h>
#include <LiquidCrystal.h>
#include <SPI.h>

LiquidCrystal lcd(12,11,5,4,3,2);
int backLight = 10;
const int latchPin = 8;
const int dataPin = 9;
const int clockPin = 7;

byte switchVar = B00000000;
byte counter = 0;
byte SymbolPosition = 5;

const int numRows = 4;
const int numCols = 20;
const byte Symbol_1 = B10110000;
const byte Symbol_2 = B01111110;

/*Switch Binary Code Reference (in order according to breadboard)
32 == Down
64 == Up 
129 == Right
2 == left
4 == Options 
8 == Ok
16 == Back
*/

void setup()
{
  pinMode (backLight, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, INPUT);
  
  //RTC boot up
  Wire.begin();
  RTC.begin();
  
//lcd boot up screen
  digitalWrite(backLight, HIGH);
  lcd.begin(numCols,numRows);
  lcd.setCursor(6,0);
  lcd.print("WELCOME");
  lcd.setCursor(1,1);
  lcd.print("Dosing Pump Module");
  lcd.setCursor(5,2);
  lcd.print("By JUNBLUD");
  lcd.setCursor(4,3);
  lcd.print("Loading:");
  for (int i = 0; i <= 100; i++)
  {
    lcd.setCursor(12,3);
    if (i<100) lcd.print(" "); 
    if (i<10) lcd.print(" ");
    lcd.print(i);
    lcd.print("%");
    delay(100); 
  }
   delay(2000);
   lcd.clear();
}

void time ()
{
  DateTime now = RTC.now();
  lcd.setCursor(12,0);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
}

void mainMenu()
{
  lcd.setCursor(0,0);
  lcd.print("Main Menu");
     lcd.setCursor(0,1);
     lcd.write(Symbol_1);
     lcd.setCursor(2,1);
     lcd.print("Pump 1");
       lcd.setCursor(0,2);
       lcd.write(Symbol_1);
       lcd.setCursor(2,2);
       lcd.print("Pump 2");
         lcd.setCursor(0,3);
         lcd.write(Symbol_1);
         lcd.setCursor(2,3);
         lcd.print("Set Schedule");
           lcd.setCursor(10,1);
           lcd.write(Symbol_1);
           lcd.setCursor(12,1);
           lcd.print("Pump 3");
}

void pumpStatus(const char *pumpStatusNum)
{
  lcd.setCursor(0,0);
  lcd.print(pumpStatusNum);
   lcd.setCursor(2,1);
   lcd.print("Dosage 1 =");
     lcd.setCursor(13,1);
     lcd.print("00:00");
       lcd.setCursor(2,2);
       lcd.print("Dosage 2 =");
       lcd.setCursor(13,2);
       lcd.print("00:00");
}

void setScheduleMenu()
{
   lcd.setCursor(0,0);
   lcd.print("Set Schedule");
     lcd.setCursor(0,1);
     lcd.write(Symbol_1);
     lcd.setCursor(2,1);
     lcd.print("Pump 1");
       lcd.setCursor(0,2);
       lcd.write(Symbol_1);
       lcd.setCursor(2,2);
       lcd.print("Pump 2");
         lcd.setCursor(0,3);
         lcd.write(Symbol_1);
         lcd.setCursor(2,3);
         lcd.print("Pump 3");
}

void adjustPump(const char *adjustPumpNum)
{
   lcd.setCursor(0,0);
   lcd.print(adjustPumpNum);
     lcd.setCursor(0,1);
     lcd.write(Symbol_1);
     lcd.setCursor(2,1);
     lcd.print("Everyday at");
       lcd.setCursor(0,2);
       lcd.write(Symbol_1);
       lcd.setCursor(2,2);
       lcd.print("everyday at");
         lcd.setCursor(0,3);
         lcd.write(Symbol_1);
         lcd.setCursor(2,3);
         lcd.print("Amount 000ml");
}

void options()
{
  lcd.setCursor(0,0);
  lcd.print("Options");
     lcd.setCursor(0,1);
     lcd.write(Symbol_1);
     lcd.setCursor(2,1);
     lcd.print("Pump Flush");
       lcd.setCursor(0,2);
       lcd.write(Symbol_1);
       lcd.setCursor(2,2);
       lcd.print("Manual Dose");
       lcd.setCursor(0,3);
       lcd.write(Symbol_1);
       lcd.setCursor(2,3);
       lcd.print("Schedule Reminder");
}

void pumpFlush()
{
   lcd.setCursor(0,0);
   lcd.print("Pump Flush");
     lcd.setCursor(0,1);
     lcd.write(Symbol_1);
     lcd.setCursor(2,1);
     lcd.print("Flush Pump 1");
       lcd.setCursor(0,2);
       lcd.write(Symbol_1);
       lcd.setCursor(2,2);
       lcd.print("Flush Pump 2");
         lcd.setCursor(0,3);
         lcd.write(Symbol_1);
         lcd.setCursor(2,3);
         lcd.print("Flush Pump 3");
}

void scheduleReminder()
{
  lcd.setCursor(0,0);
   lcd.print("fist line goes here");
     lcd.setCursor(0,1);
     lcd.write(Symbol_1);
     lcd.setCursor(2,1);
     lcd.print("second line goes here");
       lcd.setCursor(0,2);
       lcd.write(Symbol_1);
       lcd.setCursor(2,2);
       lcd.print("third line goes here");
         lcd.setCursor(0,3);
         lcd.write(Symbol_1);
         lcd.setCursor(2,3);
         lcd.print("forth line goes here");
}

void shiftRegister()
{
  digitalWrite(backLight, HIGH);
  
  digitalWrite(latchPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(latchPin, LOW);

  switchVar = shiftIn(dataPin, clockPin, MSBFIRST);
}

void loop()
{
  while (counter == 0,1,2,3,4,5)
  {
    shiftRegister();
    if(switchVar == 32)
    {
      delay(300);
      counter+1 < SymbolPosition? counter++ : counter = 1;
    }
    shiftRegister();
    if (switchVar == 64)
    {
      delay(300);
      counter-1 > 0? counter--: counter = SymbolPosition-1;
    }
    switch(counter)
    {
      case 0:
        mainMenu();
        time();
        break;      
      case 1:
        lcd.setCursor(0,1);
        lcd.write(Symbol_2);
          lcd.setCursor(0,2);
          lcd.write(Symbol_1);
            lcd.setCursor(0,3);
            lcd.write(Symbol_1);
              lcd.setCursor(10,1);
              lcd.write(Symbol_1);
              break;
      case 2:
        lcd.setCursor(0,1);
        lcd.write(Symbol_1);
          lcd.setCursor(0,2);
          lcd.write(Symbol_2);
            lcd.setCursor(0,3);
            lcd.write(Symbol_1);
              lcd.setCursor(10,1);
              lcd.write(Symbol_1);
              break;      
      case 3:
        lcd.setCursor(0,1);
        lcd.write(Symbol_1);
          lcd.setCursor(0,2);
          lcd.write(Symbol_1);
            lcd.setCursor(0,3);
            lcd.write(Symbol_2);
              lcd.setCursor(10,1);
              lcd.write(Symbol_1);
              break;   
      case 4:
        lcd.setCursor(0,1);
        lcd.write(Symbol_1);
          lcd.setCursor(0,2);
          lcd.write(Symbol_1);
            lcd.setCursor(0,3);
            lcd.write(Symbol_1);
              lcd.setCursor(10,1);
              lcd.write(Symbol_2);
              break;
      case 5:
        lcd.setCursor(0,1);
        lcd.write(Symbol_2);
          lcd.setCursor(0,2);
          lcd.write(Symbol_1);
          break;
      case 6:
        lcd.setCursor(0,1);
        lcd.write(Symbol_1);
          lcd.setCursor(0,2);
          lcd.write(Symbol_2);
          break;
    }
    {
      if (counter == 1 && switchVar == 8)
      {
        delay(300);
        counter = 5;
        lcd.clear();
        pumpStatus("Pump One");
        {
          if (switchVar == 32 && counter == 5)
          {
            counter = 6;
          }
        }
      }
      if (counter == 2 && switchVar == 8)
      {
        delay(300);
        lcd.clear();
        pumpStatus("Pump Two");
      }
      if (counter == 3 && switchVar == 8)
      {
        delay(300);
        lcd.clear();
        setScheduleMenu();
      }
      if (counter == 4 && switchVar == 8)
      {
        delay(300);
        lcd.clear();
        pumpStatus("Pump Three");
      }
      if (switchVar == 16)
      {
        delay(300);
        lcd.clear();
        counter = 0;
      }
      if (switchVar == 4)
      {
        delay(300);
        lcd.clear();
        options();
      }
    }
  }
}

Maybe i think the answer is another counter? so how do i stop the first counter and start another? hmm?

That would
depend to a
large extent on
what you are counting.

Why
does
your
code
keep
creeping
across
the
screen?

well i want to count to 5 and down again which is shown here

{
  shiftRegister();
  if(switchVar == 32)
  {
    delay(300);
    counter+1 < SymbolPosition? counter++ : counter = 1;
  }
  shiftRegister();
  if (switchVar == 64)
  {
    delay(300);
    counter-1 > 0? counter--: counter = SymbolPosition-1;
  }

now i want to stop that counter and start another here which counts up to three.

the problem is that i want to use the shift registers outputs again i.e. 32 and 64 with another counter as well as the above counter. as it is in a loop there is confusion between each counter when the shift register reads 32 and 64

Im not sure, i think it helps me to read it better.

well i want to count to 5 and down again which is shown here

Count what? Are watermelons rolling by? One does not just count for the hell of it. One counts events, and uses a variable name that reflects the event being counted.

Meaningful names would go a long ways towards making your code better. I can guess, with a reasonable degree of certainty, what digitalRead() does. I haven't a clue what shiftRegister() does.

shiftregister()
is a function called upon from void setup.

facetious much!?/ read my whole code post

i am using meaningful names! shiftregister is a function i created that tells the arduino to read what binary code the shiftregister is sending

i'm not specifically counting anything, but how many times a button is pressed. i.e. if x button is pressed +1, if y button is pressed -1.

is it not possible to stop one counter and start another?

Sure. Why not ?

But if you want two different variables to count , you have to give them different names.

ive tried doing that but becasue the +1 and -1 is coming from the same buttons from the shiftregister, is gets confused as to which counter is to be added too or subtracted

shiftregister()
is a function called upon from void setup.

I know it's a function, and I can see that it is called from setup(). However, digitalRead(), which reads from a digital pin, isn't called digital(). The name tells us exactly what the function does. shiftRegister() doesn't. A shift register is a piece of hardware that can be read from or written to. A name like readShiftRegister() or writeShiftRegister() would tell us more.

i am using meaningful names!

You are NOT. counter? shiftRegister()? Don't make me laugh. Neither of these names means squat.

It's not getting confused. You are.

ok ive fixed that.

now, whilst using the same variables from the shift register how do i get them to control two different counters.

what i am trying to achieve is a lcd menu where you can scroll up and down. due to there being less options to select in the sub menu i need a different counter. there are 5 different options in the main menu and in the sub menu there are 2.

when the sub menu is selected i need to stop the main menu counter and start the sub menu counter

Junblud:
when the sub menu is selected i need to stop the main menu counter and start the sub menu counter

So you need some type of state variable to keep track of what menu you are on:

enum { MAIN_MENU, SUB_MENU1, SUB_MENU2 } myLCDState = MAIN_MENU;
...
switch (myLCDState )
{
  case MAIN_MENU:
    mainMenuCounter++;
    break;
  case SUB_MENU1:
    subMenu1Counter++;
    break;
  case SUB_MENU2:
    subMenu2Counter++;
    break;
}

Then you can set myLCDState when you change menus.

indeed, and the counter keeps track of the position of the menu selection.

so how to i integrate the two switch cases?

Junblud:
so how to i integrate the two switch cases?

By combining them in a way that makes the code do what you want it to do.

(General questions get general answers)