Problem with Switch Case function

I am still having problems with my Switch Case options function so what I have done is isolated it from my main program onto a Scratchpad sketch so as it isn't influenced by anything else.

the code is below and the problem I am having is that I cannot stop it from freewheeling through the case options without a button press from digitalRead(P_0).
I have battled on with this for a good while now and cannot make any headway with it. Could someone kindly put me out of my agony.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

LiquidCrystal_I2C lcd(0x3F, 20, 4); // 20x4 LCD Display

  // constants won't change. They're used here to set pin numbers:
  const int P_0 = 10;     // the number of the pushbutton pin
  const int P_1 = 9;
  const int P_2 = 8;
  const int P_3 = 7;
  const int P_4 = 6;
  const int ledPin =  13;      // the number of the LED pin


// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status
int var = 0;
int Count = 0;
int ButtonPress = 0;
int menu = 0;
int XStepsPermm = 0;
int YStepsPermm = 0;
int ZStepsPermm = 0;
void setup() {

  lcd.begin();
  lcd.backlight();
  lcd.clear();

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(P_0, INPUT);    // Select
pinMode(P_1, INPUT);    // Left
pinMode(P_2, INPUT);    // Up
pinMode(P_3, INPUT);    // Down
pinMode(P_4, INPUT);    // Right

pinMode(P_0, INPUT_PULLUP);
pinMode(P_1, INPUT_PULLUP);
pinMode(P_2, INPUT_PULLUP);
pinMode(P_3, INPUT_PULLUP);
pinMode(P_4, INPUT_PULLUP);

    XStepsPermm = EEPROM.read(1);   
    YStepsPermm = EEPROM.read(2);   
    ZStepsPermm = EEPROM.read(3); 
}
void loop() {

  // void EditParams() {
  //  lcd.setCursor(2, 0);
  //  lcd.print("Surface Grinder");
  //  lcd.setCursor(5, 1);
  //  lcd.print("MAIN MENU");
        
 // ************** PARAMETERS ***************
 //              EDIT PARAMETERS
 //modus = X Steps per mm || Y Steps/mm || Z Steps per mm || Return
 //   lcd.clear();
     if(digitalRead(P_0)== LOW)
      {
      Count = Count + 1; }     
      delay (500);
    if (Count >= 4) {
        Count == 0; }
      
    switch (Count) {
  //-------------EDIT X--------------------      
    case 0:   
      lcd.setCursor(2,0);
      lcd.print("EDIT X PARAMETERS");
      lcd.setCursor(0,2);
      lcd.print("> Set 'X' Steps/mm");
      lcd.setCursor(7,3);
      lcd.print("<");
      lcd.setCursor(9,3);
      lcd.print (XStepsPermm);
      lcd.setCursor(13,3);
      lcd.print(">");
//     delay (200);
      
    if(digitalRead(P_2)== LOW)
      {
      XStepsPermm++;          // Increment the Count by 1                 
      }
      delay (300);
    if(digitalRead(P_3)== LOW)
      {
      XStepsPermm--;                     // Decrement the Count by 1
      }                                 
    if (XStepsPermm <=99) {           
      lcd.setCursor(11,3);              //Blank trailing zero
      lcd.print (" "); 
      }
      delay (200);
    if(digitalRead(P_0)== LOW)
      {        
      EEPROM.write(1, XStepsPermm);
      break;
      lcd.clear();
      }
  
  //-------------EDIT Y--------------------
    case 1:
      lcd.setCursor(2,0);
      lcd.print("EDIT Y PARAMETERS");
      lcd.setCursor(0,2);
      lcd.print("> Set 'Y' Steps/mm");
      lcd.setCursor(7,3);
      lcd.print("<");
      lcd.setCursor(9,3);
      lcd.print (YStepsPermm);
      lcd.setCursor(13,3);
      lcd.print(">");
    if(digitalRead(P_2)== LOW)          // if the pushbutton P2 (Up) is pressed.
      {                                   
      YStepsPermm++; }                    // Increment the Count by 1
      delay (200);
      if(digitalRead(P_3)== LOW)          //  if the pushbutton P2 (Up) is pressed.
      {                                    
      YStepsPermm--;                      // Decrement the Count by 1
      }                                         
      if (YStepsPermm <=99) {           
      lcd.setCursor(11,3);                //Blank trailing zero
      lcd.print (" ");
      } 
      delay (200); 
    if(digitalRead(P_0)== LOW)
      {        
      EEPROM.write(2, YStepsPermm);
      break; 
      }
//    delay (200);
      lcd.clear();
            
  //-------------EDIT Z--------------------  
    case 2:
      lcd.setCursor(2,0);
      lcd.print("EDIT Z PARAMETERS");
      lcd.setCursor(0,2);
      lcd.print("> Set 'Z' Steps/mm");
      lcd.setCursor(7,3);
      lcd.print("<");
      lcd.setCursor(9,3);
      lcd.print (ZStepsPermm);
      lcd.setCursor(13,3);
      lcd.print(">");
    if(digitalRead(P_2)== LOW)
      {                                   // check if the pushbutton P2 (Up) is pressed.
      ZStepsPermm++; }                    // Increment the Count by 1
      delay (200);
      
    if(digitalRead(P_3)== LOW)
      {
      YStepsPermm--; }                   // Decrement the Count by 1
      
      if (ZStepsPermm <=99) {           //Blank trailing zero
      lcd.setCursor(11,3);
      lcd.print (" "); }
      delay (300);
    if(digitalRead(P_0)== LOW)
       {       
      EEPROM.write(3, ZStepsPermm);
      break;
      }
      delay (300);
 
 } 
  }

At a glance, all your break statements are within if statements.

Typically, cases end with a break that will certainly be execute.d

a7

Each case has to be terminated by a break; statement.

That's it !!!

THANK YOU both (Dance at your wedding!)

shouldn't you only process the cases when there is a button pressed?

If you want the switch statement to executed only once per button press (with a new value of count), try something like this:

    if(digitalRead(P_0)== LOW { 
       Count = Count + 1;    
       if (Count >= 4) {
          Count == 0; 
       }

       switch ( Count) {
         case 0 : {
           // . . .
           break;
         }
        case 1 : {
           // . . .
           break;
         }
 
       } // switch

       delay( 500 ) ;
    }

probably not doing what is expected

consider
no delays

# include <Wire.h>
# include <LiquidCrystal_I2C.h>
# include <EEPROM.h>

// -----------------------------------------------------------------------------
LiquidCrystal_I2C lcd (0x3F, 20, 4); // 20x4 LCD Display

const int ledPin =  13;      // the number of the LED pin
int Count = 0;

char s [80];

// ---------------------------------------------------------
struct Xyz {
    int  addr;
    int  val;
    const char *desc;
};

Xyz xyz [] = {
    { 1, 0, "X" },
    { 2, 0, "Y" },
    { 3, 0, "Z" },
};
#define Nxyz    (sizeof(xyz)/sizeof(Xyz))

unsigned xyzIdx = 0;

// -----------------
void
dispXyz (
    int  n )
{
    Xyz *p = & xyz [n];

    sprintf (s, " Val %s %6d", p->desc, p->val);
    lcd.setCursor (2,0);
    lcd.print (s);
}


// -----------------------------------------------------------------------------
#define MyHW
#ifdef MyHW
byte ButPins [] = { A1, A0, A2, A3 };
#else
byte ButPins [] = { 10, 9, 8, 7, 6 };
#endif
#define N_BUT   sizeof(ButPins)

byte butState [N_BUT];

#define NO_BUT  -1

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

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

            delay (10);     // debounce

            if (LOW == but)
                return n;
        }
    }
    return NO_BUT;
}

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

    lcd.begin ();
    lcd.backlight ();
    lcd.clear ();

    // initialize the LED pin as an output:
    pinMode (ledPin, OUTPUT);

    // initialize the pushbutton pin as an input:
    for (unsigned n = 0; n < sizeof(ButPins); n++)  {
        pinMode (ButPins [n], INPUT_PULLUP); 
        butState [n] = digitalRead (ButPins [n]);
    }

    for (unsigned n = 0; n < Nxyz; n++)  {
        xyz [n].val =  EEPROM.read (xyz [n].addr);
    }

    dispXyz (xyzIdx);
}

// -----------------------------------------------------------------------------
void loop () {
    int but = chkButtons ();

    if (NO_BUT == but)
        return;

    switch (but)  {
    case 0:
        if (Nxyz <= ++xyzIdx)
            xyzIdx = 0;
        break;

    case 1:
        EEPROM.write (xyz [xyzIdx].addr, xyz [xyzIdx].val);
        break;

    case 2:
        xyz [xyzIdx].val++;
        break;

    case 3:
        xyz [xyzIdx].val--;
        break;
    }

    dispXyz (xyzIdx);
}

Oh yes. Switch on compiler warnings.

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