How to detect digitalRead while chip is doing something else?

Hi.. My micro-controller is doing a lot of things but sometimes when I press button, it doesn't work(of course) because its doing something else.

Sometimes I need to press and wait about 500 milliseconds to work.

Is there some capacitor or some hardware I could use to "store" the button action and then, when the micro controller detect it, runs my functions?

Usually you need to eliminate delays and use millis() so that you are not busy waiting. Or you could use an interrupt.

You could delete delay() from your code and change to Blink Without Delay style code instead.

Hi.. Not using delay at all..

I have 3 Serial instances

One Running my Debug window
One running my Xbee module
One running communication between 2 atmega3280

Maybe the time sending UART information to LCD? (nextion)?

KeithRB:
Usually you need to eliminate delays and use millis() so that you are not busy waiting. Or you could use an interrupt.

I Didn't think about it..

Or just check your button as many times as possible in your code.

How about you share your code with us ?
Maybe you are using blocking functions to read from Serial, hence the delay.

you guys could be right, both!

I'm not home now but I will share the code when I get there!

Tks for now!

Programming your input pin to trigger an interrupt should cause the processor to immediately stop what it's doing and process your input. That in itself can cause a problem, but you could do something very simple in your interrupt route such as just set a global boolean flag to true indicating that the button was pushed, then handle that flag later in your loop(). This would reduce the chance that the interrupt would disrupt something else. Maybe something like:

#define INTERRUPT_INPUT_PIN  2
volatile bool bGlobalButtonPressedFlag ;


setup()
{
   pinMode(INTERRUPT_INPUT_PIN , INTPUT_PULLDOWN);
   bGlobalButtonPressedFlag = false;
   attachInterrupt(digitalPinToInterrupt(INTERRUPT_INPUT_PIN), ButtonPressedFunction, RISING /* Trigger when pin is pulled high */);
}


// The processor will stop everything and execute this function the instant the button is pushed
void ButtonPressedFunction()  
{
   bGlobalButtonPressedFlag = true;
}

loop()
{
   // blah blah...
 
   if( bGlobalButtonPressedFlag == true )
   {
        // Do stuff...
       bGlobalButtonPressedFlag = false;   // Reset 
   }

}

I'm betting UKHeliBob has it right - OP is using blocking input reads.

Maybe have a look at the demo Several Things at a Time

...R