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. 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.
Design Considerations
Before we look at the offered method, we need to define our starting point as it has a bearing on the coded method.
Button circuit - The button switch circuit is configured as per Figure 1 (Circuit 1), below. This is a very common way to wire up a simple switch circuit. 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. When the switch is closed (on), the the digital pin rises to +5v and triggers the interrupt, as we require (see next section).
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. 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'. For example, attachInterrupt(...,..., RISING).
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. So the switch is only on for the duration it is pressed. Obvious stuff, yes? Of course. 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. 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.
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. The good news is that such noise can be accommodated through a technique known as 'debouncing'. Indeed, the offered sketch does incorporate this technique.
To understand what is going on, Figure 2 presents a simplified trace of a typical button switching cycle. 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). 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. But, in truth, we need to account for false readings that may arise from noise at any point between A and D. 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. Quite a recipe of requirements!
Having said all that, there are numerous examples that do the job effectively. The method offered here, however, is straight forward, logical, concise and works - please do keep reading.
And The Solution Is...
The example solution is a sketch that addresses all of the above considerations and requirements in a simple, succinct and elegant way. Of note, and fundamental to its design, is that the method separates out switching initiation (the interrupt process) and switching completion (i.e. waiting for release and managing debounce).
It is also worthwhile mentioning that the switch reading process is non-exclusive (non-blocking), both when testing the digital input and when processing debounce. That is the approach allows the sketch to do other things even when the switch may be in transition.
Have a look at the sketch, load it up and try it out.
Further InvestigationIf you are happy with the sketch and the way it operates, why not look at what changes may be necessary if you were to use a different switch circuit? For example, one that does not require a pull down resister. The circuit would look like Figure 3, below.
How would the sketch need to be altered?
Hint: the digital read pin would need to be at +5v (HIGH) as the button switch will cause current to flow from the digital pin to ground when pressed which will result in the triggering of the interrupt (switch would be wired up as per circuit_C2, above). Also, in setup(), this will require the switch pinMode call using the INPUTPULLUP parameter, rather than INPUT and an attachinterrupt call using the parameter 'FALLING', rather than 'RISING'.
Is that all? Not quite, because under open circuit conditions the digital pin will read HIGH which means the switch is open and LOW when the switch is closed - the reverse. See if you can work it out, but if not, have a look at the tutorial on button switches below which should help.
More SophisticationIf you have enjoyed looking and working through this article, then have a look at how we can use a single interrupt service routine (ISR) to handle any number of switches, virtually, without any additional wiring beyond the number of switches we wish to configure. The article is called "Multiple Switches, One Interrupt" and it makes use of the ez_switch_lib
library (see below, Further Reading). The method is readily extensible to utilise any number of switches, button or toggle type and wired in either common wiring scheme.
You might also find these contributions interesting and useful, by the same author:
- A general switch library (ez_switch_lib), suitable for most switch types and wiring schemes, incorporating novel features.
- A general serial-in/serial-out library (ez_SIPO8_lib) - a library for defining banks of SIPO ICs, up to 255, single or linked into multiple SIPO banks, offering a theoretical upper limit of 2040 outputs. For example 74HC595 ICs.
- UnderstandIng and UsIng Button SwItches, the basIcs - button switches, a simple but often tricky piece of kit. The tutorial provides in ins and outs of implementing a simple button switch, with flexibility to explore differences in circuit design, different reading methods and debouncing.
- Toggle Switches - how to reliably read a toggle style switch.
- Buttons & Lights Game - a bit of fun using button switches and LEDs.
- External Interrupts, a generic framework supporting concurrent asynchronous multiple interrupts. Configure multiple external interrupts with different characteristics and add code to provide post-interrupt asynchronous processing.
- Programmatic Timed Reminder Alerting, a programmatic framework for both elapsed and real-time asynchronous alerting. Define any number of reminder alerts (sub second to hours) and process asynchronously.
Other Online Resources
enjoy!
Comments
Please log in or sign up to comment.