If statement within a case using 2 buttons

Hi i am using an Arduino Uno and I am trying to make what is essentially a really simple menu what I have so far allows me to use a button to shift through cases. but I want a button that when pressed in a certain case preforms a task. I've added an if statement inside one of the cases and it uploads but doesn't seem to do anything. I am very new to this so my fundamental thinking is probably wrong, what I have is below any help would be appreciated thanks.

#include <ezButton.h>
#include <LiquidCrystal.h>

ezButton button(7);    //switch
ezButton button1(A5);  //select
unsigned long lastCount = 0;
unsigned long count = 0;


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50);
  button.setCountMode(COUNT_FALLING);
  button1.setDebounceTime(50);
  lcd.begin(16, 2);
  lcd.print("Test BOX");
  lcd.setCursor(0, 1);
  lcd.print("Select Mode");
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  button.loop();  // MUST call the loop() function first
  count = button.getCount();

  if (count != lastCount) {
    Serial.println(count);

    int countIn3 = count % 6 + 1;
    lcd.clear();


    switch (countIn3) {
      case 1:
        lcd.setCursor(0, 0);
        lcd.print("EMERGENCY STOP");
        lcd.setCursor(0, 1);
        lcd.print("TEST 1");
        button1.loop();
        if (button1.isPressed())
          Serial.println("TEST ACTIVE");
        digitalWrite(9, HIGH);


        break;

      case 2:
        lcd.setCursor(0, 0);
        lcd.print("EMERGENCY STOP");
        lcd.setCursor(0, 1);
        lcd.print("TEST 2");

        break;

      case 3:
        lcd.setCursor(0, 0);
        lcd.print("EMERGENCY STOP");
        lcd.setCursor(0, 1);
        lcd.print("TEST 3");


        break;

      case 4:

        lcd.setCursor(0, 0);
        lcd.print("GUARD SWITCH");
        lcd.setCursor(0, 1);
        lcd.print("TEST 1 ");


        break;

      case 5:

        lcd.setCursor(0, 0);
        lcd.print("GUARD SWITCH");
        lcd.setCursor(0, 1);
        lcd.print("TEST 2 ");


        break;

      case 6:

        lcd.setCursor(0, 0);
        lcd.print("GUARD SWITCH");
        lcd.setCursor(0, 1);
        lcd.print("TEST 3 ");

        break;
    }
    lastCount = count;
  }

Welcome to the forum

What you have done in case 1 looks OK but try moving the button1.loop(); to the start of the loop() function

Try printing countIn3. Is the value what you expect ?

Which Arduino board are you using ?

Hi sorry for the slow reply I've moved the button1.loop(); but button 1 still seems to have no effect. Also I have pin 9 connected to an LED so i can see what's going on and it lights as soon as i get to case 1 rather than when I press button 1. I'm using an Arduino Uno R3.

That is because the digitalWrite() is not in the code block that depends on teh button press

Try this instead

                button1.loop();
                if (button1.isPressed())
                {
                    Serial.println("TEST ACTIVE");
                    digitalWrite(9, HIGH);
                }

Hi @henryw23

welcome to the Arduino-Forum.
Well done posting code as a code-section in your very first posting

Your code has too less and not informative enough serial printing / lcd printing for beeing able to analyse what your code is doing

In the debug / testphase your code should talk clear and unique
you used multiple times "Test1"

I changed this that in each case the case X is printed on your lcd

additionally you should use the serial monitor for analysing
because the serial monitor has much more lines and longer lines to offer
than your lcd
Here is the modificated code-version. I only changed textes and deleted too much empty lines
The logic is absolutely the same

#include <ezButton.h>
#include <LiquidCrystal.h>

ezButton button(7);    //switch
ezButton button1(A5);  //select
unsigned long lastCount = 0;
unsigned long count = 0;


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50);
  button.setCountMode(COUNT_FALLING);
  button1.setDebounceTime(50);
  lcd.begin(16, 2);
  lcd.print("Test BOX");
  lcd.setCursor(0, 1);
  lcd.print("Select Mode");
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  button.loop();  // MUST call the loop() function first
  count = button.getCount();

  if (count != lastCount) {
    Serial.println(count);

    int countIn3 = count % 6 + 1;
    lcd.clear();

    switch (countIn3) {
      case 1:
        lcd.setCursor(0, 0);
        lcd.print("EMERGENCY STOP");
        lcd.setCursor(0, 1);
        lcd.print("case 1");
        button1.loop();
        if (button1.isPressed())
          Serial.println("TEST ACTIVE");
        digitalWrite(9, HIGH);
        break;

      case 2:
        lcd.setCursor(0, 0);
        lcd.print("EMERGENCY STOP");
        lcd.setCursor(0, 1);
        lcd.print("case 2");
        break;

      case 3:
        lcd.setCursor(0, 0);
        lcd.print("EMERGENCY STOP");
        lcd.setCursor(0, 1);
        lcd.print("case 3");
        break;

      case 4:
        lcd.setCursor(0, 0);
        lcd.print("GUARD SWITCH");
        lcd.setCursor(0, 1);
        lcd.print("case 4");
        break;

      case 5:
        lcd.setCursor(0, 0);
        lcd.print("GUARD SWITCH");
        lcd.setCursor(0, 1);
        lcd.print("case 5 ");
        break;

      case 6:
        lcd.setCursor(0, 0);
        lcd.print("GUARD SWITCH");
        lcd.setCursor(0, 1);
        lcd.print("case 6 ");
        break;
    }

    lastCount = count;
  }
}

best regards Stefan

Thanks I've changed it to this but LED now wont activate at all regardless of button press , I've stuck the button into a different sketch to see if it works and seems to work fine.

        button1.loop();
        if (button1.isPressed())
        {
        Serial.println("TEST ACTIVE");
        digitalWrite(9, HIGH);
        }
        break;

Have you tried putting the button1.loop(); into the loop() function and printing the value of countIn3 as I suggested ?

Hi yes sorry I just sent the LCD into a meltdown managed to get it back to normal now but it seems printing countIn3 prints endless zeroes in the monitor which is probably why the LCD went mad I'm not sure why its doing that tho any ideas thanks for all your help btw.

Hello henryw23

Invest into a I²C module for the LCD to save time and nerves.

It's because the value of countIn3 never changes. Now try printing the value of count to find out whether it is changing

When value of count is printed it continuously prints the amount of button presses or the amount of time the case has been switched i guess.

If printing count shows the number of times that the button has been pressed, as it should, but countIn3 is always zero then there is something wrong with your calculation of countIn3

hmm maybe I've mixed up the buttons somewhere i might have a go at reorganising some of the wiring and see if that changes anything. I've had to leave it at work today so ill check back in when i can connect again, thanks for all your help :slightly_smiling_face:

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