Return value is not updated?

Hi there please help me, ive been working on this for a week... :frowning:

-The circuit will have 3 buttons, one for change mode, one for set the value and another one will count the number of button pushes,
-The circuit will initially start at 25, after change mode button is pushed once, the program will jump to fucntion count
-the initial value will be added with the number of button being pushes
-The set button is push so we can return the new value to the main loop function
-If we push the button mode once more, the program will jump to function count2
-at count2, the function suppose to decrease the value as we set before corresponding to the number of button push being pushes

-eg. (initial Y=25, >>>> set to Y=30 >>>> decrease back to Y=23 from Y=30)

-problem happens after executes function counter,
-once we updated the final value Y, we return to main loop and display the final value on the lCD
-but when we move to counter2, the final value Y is not updated from what we have done before, instead it will decrease from the intial value

-eg. initially Y=25 >>>>>> press buttonmode to HIGH >>>> counter (add value to initial value 25) >>>> (press buttonset to HIGH) set value to Y=30 >>>> press buttonmode to HIGH again>>>> counter2 (should decrease value from Y=30 but Y is not updated and decrease Y from intial 25 instead) >>> (press buttonset to HIGH) set value Y=27 for example)

Thanks! Cheers! XD

mode_button_xjadi.ino (4.46 KB)

Post your code, we can't tell anything without it. And use the

code

button (# above the editing area) to help make it readable.


Rob

Here's the code, thanks! :slight_smile:

#include <LiquidCrystal.h>

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

int buttonset = 8; //button set
int buttoncounter =9; //button counter
int buttonmode =10; //button mode

int pushcounter=0; //initiate counter
int pushcounter2 =0; //initiate counter 2
int buttonstate=0; //initiate buttonstate (for counter purpose)
int lastbutton=0; //initiate lasstbutton (for counter purpose)
int modechange=0; //initiate mode change (to toggle from counter one, to counter 2)

int x = 25; //initial value
volatile int y = x; //initial final value

void setup() //setup
{
pinMode(buttonset, INPUT);
pinMode(buttoncounter, INPUT);
pinMode(buttonmode, INPUT);

lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Start!");
lcd.clear();
}

void loop()
{

if (digitalRead(buttonmode) == HIGH && modechange==0) //once the buttonmode is pressed to HIGH and modechange=0
{
counter(); //calling function counter and return value final value = y, calculated in counter function

modechange=modechange+1; //update modechange=1
}

else if (digitalRead(buttonmode) == HIGH && modechange==1) //if the buttonmode is pressed to HIGH
{

counter2(); //calling function counter2 and return final value y, calculated in counter2 function

modechange=0;
}

else //as long as buttonmode is not press to HIGH and mode change is initial=0, the LCD will display the initial y=x=25
{

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

}

}

void counter() //function counter
{
while ( digitalRead(buttonset) == LOW)
{
buttonstate = digitalRead(buttoncounter);
if (buttonstate != lastbutton)
{
if (buttonstate == HIGH)
{
pushcounter++;

lcd.setCursor(0,0);
lcd.print("button pushes:");
lcd.setCursor(0,1);
lcd.print(pushcounter);
y= x +pushcounter;
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(y);
}

else
{
lcd.print("");
}

lastbutton=buttonstate;
}
y=x + pushcounter;
}
}

void counter2() //fucntion counter2
{
while ( digitalRead(buttonset) == LOW)
{
buttonstate = digitalRead(buttoncounter);
if (buttonstate != lastbutton)
{
if (buttonstate == HIGH)
{
pushcounter2++;

lcd.setCursor(0,0);
lcd.print("button pushes:");
lcd.setCursor(0,1);
lcd.print(pushcounter2);
y= x - pushcounter2;
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(y);
}

else
{
lcd.print("");
}

lastbutton=buttonstate;
}
y=x - pushcounter2;
}
}

And use the

code

button (# above the editing area) to help make it readable.

You can edit the post and do that.


Rob

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

In counter2() you have this line:

y= x - pushcounter2;

This is wrong, and is why the count is being subtracted from the initial value not the value after counter(). You need to change it to y = y - pushcounter2.

You also have this code in counter() which looks a bit dodgy:

y= x +pushcounter;

As written, it will reset the value to 25 each time you go through this sequence. If you want it to carry on from the previous value, you'd need to change that to y = y + pushcounter; as well.