fix state of the setting - up, down counter

Hi everyone,
not understand, how to enter and fix state the value_1 in setting mode,
have two button_B_1 and button_B_2, value_1,
if button_B_1 is pressed more than 2 seconds,
switch go to setting mode value_1,
next - value_1 setting mode,
when you press the button_B_1 , the value_1 goes up,
when the button_B_2 is pressed down, the value_1 goes down
exit the setting mode,
go to main mode

for example, I press button_B_1 and enters in setting mode value_1,
but if I stop pressing the button_B_1, everything immediately switches to the main mode,
How do I fix the state setting mode value_1 up, down?
thnx

can you post your code (use the tag "</>")

are there any emulators for this code? or menu code generators?

Button1 push more then 2 sec
active mode1
push Button1 up value1
push Button2 down value1
exit after 2 sec

Button2 push more 2 sec
active mode2
push Button1 up value2
push Button2 down value2
exit after 2 sec

for (i=5; i >= 0; i--)
{
ledprint(i);
__delay_ms(200);
}
while(1)
{
if (PORTAbits.RA5 == 0 && PORTAbits.RA4 == 0){
ledprint(value);
}
else if (PORTAbits.RA5 == 1 && PORTAbits.RA4 == 0){
__delay_ms(100);
if (PORTAbits.RA5 == 1){
ledprint(value1);
doLED_Blink();
}}

if (PORTAbits.RA4 == 1){
ledprint(value2);
doLED_Blink();
}
}
return;
}

I think that can't do without a millis timer,
and some other flag or boolean variable needs to be added,
I suppose need to make the button poll permanent,
Long press of the left button activates mode_1
Long press of the right button activates mode_2
If mode_1 is active, a timer is started for two seconds to poll if there any actions,
or try to set the timing 2 seconds for mode_1,
if mode 1 is active,
Short press of the left button, the value_1 is incremented,
Short press of the right button decreases the value_1,
exit from the mode_1 by time or inactivity,

with mode_2, too alls, only there is an increment or decrement of value_2,

for me the main difficulty now is to start mode_1 with a long left-click,
and then use the same left button to increment with a short press,
thanks

consider
i think this code is fairly awkward because of the timing required for the buttons

byte butPins [] = { A1, A2 };
#define N_BUT   sizeof(butPins)

#define NONE    -1

int  butSt [N_BUT] = { 1, 1 }; 
int  button        = NONE;

#define N_VAL  2
int  val [N_VAL] = {};
int  mode = NONE;

#define MODE_PERIOD     2000

unsigned long msecBut = 0;
unsigned long msecRel = 0;
unsigned long msec;

char s [80];

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

    for (unsigned n = 0; n < N_BUT; n++)
        pinMode (butPins [n], INPUT_PULLUP);
}

// -----------------------------------------------------------------------------
void
disp (void)
{
    sprintf (s, "%s: mode %2d, val0 %3d, val1 %3d",
        __func__, mode, val [0], val [1]);
    Serial.println (s);
}

// -------------------------------------
void
chkBut (
    byte   but )
{
    byte b = digitalRead (butPins [but]);

    if (butSt [but] != b)  {
        butSt [but] = b;

        if (LOW == b)  {    // recognize press
            if (NONE != mode)  {    // if mode set, in/decrment value
                val [mode] += 0 == but ? 1 : -1;
                disp ();
            }
            else  {                 // capture time if mode not set
                button  = but;
                msecBut = msec;

                sprintf (s, "  %s: but %d, button %d, But %6ld",
                            __func__, but, button, msecBut);
                Serial.println (s);
            }
        }
        else  {             // capture release
            button = NONE;
            msecBut = 0;
            msecRel = msec;
        }
    }
}

// -------------------------------------
void
loop (void)
{
    msec    = millis ();

    // check for button pressed > MODE_PERIOD
    if (msecBut)  {
        if ((msec - msecBut) > MODE_PERIOD)  {
            msecBut = 0;
            mode    = button;
            disp ();
        }
    }

    // check for button released > MODE_PERIOD
    if (msecRel)  {
        if ((msec - msecRel) > MODE_PERIOD)  {
            msecRel = 0;
            mode    = NONE;
            disp ();
        }
    }

    // check each button
    for (unsigned n = 0; n < N_BUT; n++)
        chkBut (n);
}

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