How to wait for input, whitout pausing the whole sketch

I have a simple touch button interface and at this point i am just waiting(whit while loop) for user to touch a touchbutton and then resume whit the program.

But now i want to be able to “wait” for user to press the button but in same time, i want my sketch to continue working in the background, bcs i have some processes running in the backgroudn that i dont want to pause them.

Any ideas or tehniques on how to do it?

Thanks in advance.

In 'loop()', instead of:-while(button==HIGH);maybe you just want:-

    // Do something

You might want something more, but without seeing your code we can only guess.

the main code calls the function for touchdetecttion, and then that code goes around in loop until one of touchbuttoons is pressed. After the button is pressed, function for touchdetection exits, and returns to main code.

For example.

//Main code
void loop(){
…do someting…
print on display: “button1(or 2 or 3 or…) is pressed”

//Touch detect
void touchdetect(){
do loop continisuly until one of buttons is detected, save which button is pressed and go back to main code

But now i want the main code to continisuly run, no mather how long the user waits before pressing a button.

Although the obvious solution to such an issue are threads, but threads arent supported in arduino. U could although use Protothreads instead.. Link: Arduino code for Protothreads:

A simpler solution would be to check if you can work your way with interrupts, especially timed interrupts. You can program a timer to interrupt the main routine every so many microseconds, to run a specific other routine. Or a work around with the Arduino timers, some browing on the internet will definitely solve the issue for you..

If all the OP needs is to check if the human user presses a button there is no need for either Threads or interrupts.

Simple polling will be plenty fast enough to detect human activity.

The trick is to ensure that other parts of the code don’t prevent loop() from repeating fast enough. The code in Several Things at a Time detects a button press without preventing other things from happening.


Thanks for all the help, i will try robin solutiuon, and if that doesnt prove to be good enough ill just use threads. I tend to avoid protothreads bcs they tend to be buggy in some situations, and i just port my arduino project to atmel studio and assembel threads there.

Thanks again :D