cancel
Showing results for 
Search instead for 
Did you mean: 

Long press button, increment fast value

Freedom_Neo
Senior

I am designing counter with button click. When long press button countter will increment fast, if normal press to button counter will normal. But when I hold pressed button, there isnt anything. How do we do makes it? When I debug code while I hold button, it dont call button increment function

void Main_MenuView::time_increment()
{
	if (g_buzzer_flag)
		buzer_on();
 
	time_counter++;
 
	if (time_counter > 500)
		time_counter = 500;
 
	Unicode::snprintf(textArea5Buffer, TEXTAREA5_SIZE, "%d", time_counter);
	textArea5.invalidate();
}

5 REPLIES 5
Yoann KLEIN
ST Employee

Hello @Community member​ ,

I don't really understand your question, can you please elaborate a little bit ?

  • What do you mean by "long press" and "normal press" ?
  • What is the difference between "long press" and "hold pressed" ?
  • How do you want the counter to behave exactly ?

Thanks,

/Yoann

Yoann KLEIN
ST Software Developer | TouchGFX

Hello @Yoann KLEIN​ 

>>What do you mean by "long press" and "normal press" ?

long press, I mean holding the button for a long time. For example 3 seconds or more, but normal press is button click

>>What is the difference between "long press" and "hold pressed" ?

Both mean the same thing

>>How do you want the counter to behave exactly ?

When the button is clicked normally, I want the counter to increase by 1, if the button is kept pressed(long press-hold press) for 3 seconds, I want the counter to increase by 10 each.

Thanks, that's much clearer for me now.

A similar question was already asked some years ago on another post. I recommend you to follow the steps described there, it should be enough to achieve what you want to.

But also please note that for your use, some modifications have to be made in Screen1View.cpp :

void Screen1View::handleTickEvent()
{
   if(buttonPressedFlag)
   {
       counter++;
       if(counter%180 == 0) // 3 seconds passed
       {
           display_counter += 10; //Add 10 to the counter that is displayed on the screen
           counter = 0;
       }
      elseif(counter%30 == 0) // 0.5 second passed, you can modify this value to be more realistic with a irl press/release duration
      {
         display_counter ++; //Add 1 to the counter that is displayed on the screen
      }
   }
   else //Reset the conter everytime the user releases the button
   {
       counter = 0;
   }
}

This should enable you to add 10 to your counter every time the user holds the button for 3 seconds, and add 1 if he only holds very quickly (0.5 sec in this example, but you can decrease this value).

Let me know if you have other issues,

/Yoann

Yoann KLEIN
ST Software Developer | TouchGFX

Hi @Yoann KLEIN​ 

When the button is pressed, I make buttonPressedFlag = 1, but how will I know when the button is removed? Because when he takes his hand off the button, I need to make buttonPressedFlag = 0.

Freedom_Neo
Senior

Hi @Yoann KLEIN​ 

I did work this application. But I need a little different. I need as in the video below. It will work as long as the button is pressed, and it will not work when I take my hand off the button. For example; When my hand is pressed on the button, the led will turn on, but when I take my hand off the button, the led will turn off.

What I want to do is, as long as my hand is pressed on the button, the numbers will increase, but when I take my hand off the button, the numbers will not increase.

I've been trying to solve this problem for a few days