1

When a button is pressed, the Arduino might be busy with something (like drawing the screen). When the Arduino has finally time to check the button input, the uses might have release it already.

Is there a way that can store the input until the Arduino has time to check it? It would be awesome if there was ic that could take care of this.

Thanks for the help!

3
  • It's called an interrupt.
    – Majenko
    Commented Feb 13, 2021 at 16:49
  • 2
    I most cases, this isn't an issue. Humans are kind of slow, compared to an Arduino running at 16Mhz. Pressing a button takes tens to hundreds of milliseconds. An eternity for a CPU. Alternatively, you could add an RC circuit to the button. This has the added benefit of also debouncing the button signal.
    – Gerben
    Commented Feb 13, 2021 at 17:25
  • Just a SWAG but it sounds like you are using the delay(); function, that is blocking the arduino not allowing it to do anything else until finished. Work with the millis(); function, you code will be non blocking.
    – Gil
    Commented Feb 13, 2021 at 17:53

3 Answers 3

0

The answer to your direct question, is there an IC to store an input, is yes. It's called a Set-Reset (or SR) Flip-Flop. I don't know if they're packaged standalone in an IC but they're really simple to make with a pair of cross-connected NAND gates.

But that's not really the right solution to your problem. Your code should not be so "busy" that it misses a slow input like a push-button. I quoted "busy" because it is usually delay() calls, not computations, that occupy the processor for any large amounts of time, and delay()s are to be avoided if your code has any more than 1 thing to do.

Look up the Blink Without Delay example-sketch in the Arduino IDE for the unvarnished, basic approach to doing this. Then there are libraries that can help with the low-level details of doing it - SimpleTimer, is one such. Or, if your code is stuck waiting for slow external events, don't wait for them but check for them frequently and move on immediately to something else, if they haven't taken place yet.

0

The basic guidance when writing Arduino applications is to do a small amount of work on every pass through the loop function, then do a little bit more on the next pass. Check all your inputs, execute logic in response to those inputs, set up unsigned long variables with future millis() values, check those values to see if a future time has arrived, and repeat.

Do not use delay(). It is the death of a cooperative system like an Arduino. When your Arduino executes a call to delay(), everything stops until the delay time has passed.

0

Is there a way that can store the input until the Arduino has time to check it?

No, there is no way to remember/buffer input changes within the Arduino (You could use a flipfop IC like JRobert wrote. That can give you that functionality). But there are interrupts, that you can use with pins. A triggered interrupt will really interrupt the main code from running and execute its corresponding ISR (Interrupt Service Routine), to handle the interrupt cause, and then returning to execute the main code. A microcontoller has many different interrupts, each one connected to its special hardware. There are interrupts for the Serial interface, for SPI, I2C, and also some connected to the External Interrupt pins or the PinChange Interrupt (these last two could be used with your button). So this would not "store" the input change, but it will interrupt the main code to immediately act upon the input change. So that's a different principle.

If you want to bind your button to an interrupt, you could just stay in the Arduino Framework and use the attachInterrupt() function. There are many tutorials online for that. Keep in mind, that you should never do long things inside an ISR, and functions like delay() won't work there.


That said, most likely it is not really needed to use interrupts with buttons. Humans are rather slow, thus each button press will last in the ballpark of 100ms or longer.

You could now say, that your code is doing things longer than 100ms. But here the rabbit is buried (literal translation of a german saying, meaning "there is the problem"). When starting with Arduino, one is doing many blocking things, like delay() or blocking while loops. These are often very easy to implement, but nothing else will be running during these things.

Instead you should divide up your actions into small parts. When it is time to do something, you are doing that small part and exit fast. This enables other code to run in between. A good example is the BlinkWithoutDelay example. The normal blink example uses delay(), which blocks everything else. Instead you can use the millis() function as a clock and only take action, when it is time to do so, otherwise to further fast. Much like you would bake a pizza in your oven. You won't sit in front of the oven, waiting for the pizza to be ready. You would do something else and regularly look at your clock, if it is time to take the pizza out.

You didn't show your code, so we cannot advice you more in that direction. But you can find many tutorials about it, if you search for terms like Arduino blink without delay or Arduino millis tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.