Return value is not updated?

Personally I find multiple nested if/else/ifelse constructs very unclear and therefore very prone to errors and misinterpretation.

Can I suggest a more structured approach?

void loop()   {
 
    int state = 0;
    
    state = (digitalRead(buttonmode) << 1) | modechange);
    
    switch (state) {
    
        // 0 and 2 not really needed, just shown for completeness
        case 0;                        // modechange = 0, no button pressed
        case 2;                        // modechange = 1, no button pressed
            break;
            
        case 1;                        // modechange = 0, button pressed
            counter();              //calling function counter and return value final value = y, calculated in counter function
            modechange=1;            
            break;
    
        case 3;                        // modechange = 1, button pressed
            counter2();             //calling function counter2 and return final value y, calculated in counter2 function
            modechange=0;            
            break;

        default:
            lcd.setCursor(0,0);
            lcd.print("Current value");
            lcd.setCursor(0,1);
            lcd.print(y);            //should display what is initial, and if any changes occur(due to counter or counter2), this value should change
        
    }
}

I'm not sure I got the logic right because as I said I have trouble understanding the if/else way of doing things. But surely that's easier to follow.

Also there is no switch debouncing. The various LCD and Serial prints do provide a defacto debounce I think but I'm not sure that's going to stop getting multiple presses.

You could add a delay(100); at the end of loop() as a poor man's switch debounce.


Rob