Skip to main content
Freedom_Neo
Senior
May 17, 2022
Question

Long press button, increment fast value

  • May 17, 2022
  • 5 replies
  • 3649 views

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();
}

This topic has been closed for replies.

5 replies

Yoann KLEIN
ST Employee
May 18, 2022

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 KLEINST Software Developer | TouchGFX
Freedom_Neo
Senior
May 18, 2022

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.

Yoann KLEIN
ST Employee
May 18, 2022

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 KLEINST Software Developer | TouchGFX
Freedom_Neo
Senior
May 23, 2022

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