
The method offered here, however, is straight forward, logical, concise and works - please do keep reading. Having said all that, there are numerous examples that do the job effectively. So, things can be a bit messy - a switch interrupt can be triggered any time and we cant register that the switch has been pressed until we get to point D at the soonest. But, in truth, we need to account for false readings that may arise from noise at any point between A and D.

The time the switch can be said to be reliably on is from B to C and this is a function for how long the switch is kept pressed.

It shows an example of noise as soon as the switch is pressed (point A) to being fully on (point B) and from the time it is released (point C) to being fully off (point D). To understand what is going on, Figure 2 presents a simplified trace of a typical button switching cycle. Indeed, the offered sketch does incorporate this technique. The good news is that such noise can be accommodated through a technique known as ' debouncing '. If we also throw into the mix that switches are not perfect digital devices - they will invariably generate noise which can result in false triggering. Note also that the time it is kept pressed is variable too - I can keep my finger on the button for as long as I please. However, the nature of mechanical button switches means that switching must go through the cycle of off-on and back to off before it can be said to have been fully pressed. So the switch is only on for the duration it is pressed. Switching style - Button switches are momentary switches, by which I mean that the switch's normal state is off until pressed when it is on until released when its state reverts back to off. For example, attachInterrupt (., RISING).

As the circuit will cause the voltage to rise from 0v to +5v when the switch is closed, then the obvious choice of mode and respective parameter for attaching the interrupt is 'RISING'.

Interrupt mode - Next, we need to define the mode or type of interrupt trigger the digital pin will be sensitive to for firing the interrupt. When the switch is closed (on), the the digital pin rises to +5v and triggers the interrupt, as we require (see next section). The circuit ensures that the digital input pin we declare and use as the interrupt pin is maintained at 0v when the switch is open (off), thereby removing spurious inputs arising from interference which would have the effect of falsely triggering the interrupt. This is a very common way to wire up a simple switch circuit. Conversely, if you know all there is to know about button switches, wiring them, inherent issues and external interrupts then you may also find this particular contribution of interest as it presents a different method of linking and processing button switches and their respective interrupts.īefore we look at the offered method, we need to define our starting point as it has a bearing on the coded method.īutton circuit - The button switch circuit is configured as per Figure 1 (Circuit 1), below. If you are new to the idea of connecting up button switches and wiring up a simple circuit then see the tutorial Understanding and Using Button Switches - this will provide you with some of the fundamentals before looking next at an example implementation with an external interrupt.
