# button Basics

**URL:** <https://forum.arduino.cc/t/button-basics/388555>\
**Category:** Programming\
**Created:** [May 27, 2016, 8:50pm UTC](https://forum.arduino.cc/t/button-basics/388555 "2016-05-27T20:50:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomzko](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@tomzko](https://forum.arduino.cc/u/tomzko)\
**Post date:** [May 27, 2016, 8:50pm UTC](https://forum.arduino.cc/t/button-basics/388555/1 "2016-05-27T20:50:02Z")

</div>

Hi everyone, im a little stuck with a simple code that i cant figure out how to solve, what i want is:  
press a button start doing a function (example blink)  
press button again function to stop  
press button again start function  
.  
.  
.

please if someone can help me! 🙂  
regards!

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [May 27, 2016, 9:01pm UTC](https://forum.arduino.cc/t/button-basics/388555/2 "2016-05-27T21:01:36Z")

</div>

The state change detection example in the IDE examples may help.

Or

> **[LMGTFY - Let Me Google That For You](https://lmgtfy.app/?q=Arduino%2Btoggle%2Bbutton)**
>
> For all those people who find it more convenient to bother you with their question rather than to Google it for themselves.

---

<div class="post-metadata">

**Author:** ![tomzko](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@tomzko](https://forum.arduino.cc/u/tomzko)\
**Post date:** [May 27, 2016, 10:41pm UTC](https://forum.arduino.cc/t/button-basics/388555/3 "2016-05-27T22:41:25Z")

</div>

what ive read is that i need a change interrupt, still cant figure it out how, the example in the IDE is not what im looking for.  
What im trying to do is to store data from an acc. to a micro sd card. so a push botton will start with de data aquisition, then anothe push of the button and stop the daq.  
im a bit lost ☹

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [May 27, 2016, 11:46pm UTC](https://forum.arduino.cc/t/button-basics/388555/5 "2016-05-27T23:46:47Z")

</div>

> Delta\_G:  
> You never need an interrupt for a button. Buttons are slow. Interrupts are for really fast and transient things.

False.

If you are alluding to reading a button every so often in a piece of code, it is quite possible that your button press might fall between these hardcoded readings and will not register the button press.

Using a very short interrupt to detect a button press (and possibly debounce the button as well) is the **BEST** possible practice and this technique is widely used.

Here are some guidelines for interrupts with Arduino:

Put this in your setup() function:

```arduino
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), button, CHANGE);

```

Where interruptPin is the digital input pin that will cause an interrupt to occur and button is the Interrupt Service Routine (or ISR). This special function will quickly execute whatever you want to do during the interrupt.

Also attach the definition of your ISR in the code like a normal funciton:

```arduino
void button() {
  //whatever you want (keep it simple and quick)
}

```

That's how you do interrupts in Arduino.

---

<div class="post-metadata">

**Author:** ![marco\_c](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@marco\_c](https://forum.arduino.cc/u/marco_c)\
**Post date:** [May 28, 2016, 12:20am UTC](https://forum.arduino.cc/t/button-basics/388555/7 "2016-05-28T00:20:03Z")

</div>

> Using a very short interrupt to detect a button press (and possibly debounce the button as well) is the BEST possible practice and this technique is widely used.

I think many (including me) would disagree with this for the vast majority of applications.

Switch presses are very long events in the time frame of an embedded microprocessor running code. What you need to do is learn how to write non-blocking code (in a single process environment) or event processing code in a multi thread/process environment.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:55pm UTC](https://forum.arduino.cc/t/button-basics/388555/8 "2021-05-05T21:55:01Z")

</div>

Closed on May 5, 2021, 9:55pm UTC


