How to wait for pusb button

I need:

  • when i power up arduino and button is pressed to enter "Calibration" menu
  • release pressed button when Calibration menu appears on LCD
  • when i press again same button Calibration menu exit and do other stuff in program

i try to with this code:

void setup() {
  // INITIALIZE lcd
  lcd.begin(16, 2);

  // CALIBRATION - rotary encoder switch on power
  if (digitalRead(pinSW) == LOW) {
    // CALIBRATION - menu
    doCalibrationMenu();
  }
}

// CALIBRATION temperature menu
void doCalibrationMenu() {
    // DISPLAY calibration menu
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Calibration Temp");

    //  DISPLAY change calibration value unit switch pressed again
  do {
    // DISPLAY current calibration value
    lcd.setCursor(12, 1);
    lcd.print(calibr);
  } while (digitalRead(pinSW == LOW));

  lcd.print("OK!");
  
}

As you can see i use do {} while (digitalRead(pinSW == LOW)); but it is going in loop...and not working as expected...

So i need to when i power up arduino show "Calibration" menu, after release of button and one more time pressing the button it need to exit and show on LCD "Save OK!"

It's a good thing that pins default to inputs, isn't it?

I don't understand you what you mean with pins default to inputs? Could you please be more specific?

One of your problems is:

digitalRead(pinSW == LOW)

Another problem is that as soon as you release the button, you will be leaving the calibration routine, while you stated you wanted to be able to release the button, then press a 2nd time to leave. There is also nothing in the code to debounce the button input.

ronovar:
I don’t understand you what you mean with pins default to inputs? Could you please be more specific?

You don’t have any pinMode() instructions in your program before you use the pins. By default the mode is INPUT.

…R

void setup() {
  // INITIALIZE lcd
  lcd.begin(16, 2);

  // ENABLE the pinSW as input with PULLUP resistor
  pinMode(pinSW, INPUT_PULLUP);

  // CALIBRATION - rotary encoder switch on power
  if (digitalRead(pinSW) == LOW) {
    // CALIBRATION - menu
    doCalibrationMenu();
  }

Ok, i added this code (because i use internal pull up resistor on arduino pro mini), so for second press i need button debounce?

Could you please give example for button debounce?

I do this and this works as debounce and the way i want it but i need non blocking code..this is blocking my MCU so other stuff could not be made (like refreshing value on LCD).

byte debounce = 0;

  // WAIT rotary encoder SW release
  while (digitalRead(pinSW) == LOW && debounce == 0) {
    lcd.clear();
    lcd.print("SW PRESS");
    if (digitalRead(pinSW) == HIGH) {
      do {
        lcd.clear();
        lcd.print("STUFF");
        debounce = 1;
      } while (digitalRead(pinSW) == HIGH);
    }
  }

ronovar:
but i need non blocking code..this is blocking my MCU so other stuff could not be made

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

  if (digitalRead(pinSW) == HIGH) {
      do {
        lcd.clear();
        lcd.print("STUFF");
        debounce = 1;
      } while (digitalRead(pinSW) == HIGH);
    }

Why not simply use a while loop?

I wouldn't worry about the blocking code, this is a calibration routine run only when the button is pressed at startup, and would presumably need to be completed before starting the main code, so the entire calibration routine is by design blocking.

AWOL:
Why not simply use a while loop?

It is the same function while and do, and with do i want the code to be more clear when i later in few years read code, so with do i make different look at code so that i know that this is bounce code in while loop.