Hi guys,
My brains' fried trying to figure this out...
In my project I have 1 (Nokia 5110) display and 2 sets of information to display. I want to use a 2 button momentary switch (Normally open) with hardware debounce on an interrupt so that when the button is pressed I can tell the arduino to display one set of information or the other.
Both displays are currently written in their own routine so in the loop I want to be able to say go run a or b routine.
Ive read on here about using the 2 button switch and using the internal pull-up - that works a treat but is bouncy and somewhat unpredictable when in operation (partly because of the bounce and partly because the loop takes a while to process.
My idea is that if an interrupt could be used for the toggling of the switch then that would solve my problem.
Ive looked at and spent good while thinking about Jeremy Blum's hardware bounce and interrupt tutorial:
For some reason my inexperience won't allow me to join all the dots together that I need.
Im currently trying to use the Schmitt trigger Inverter with a 2-button and having no success. The 3button works 100% as per Jeremy's tutorial.
I am confused. I don't know what you mean by "...a 2 button momentary switch...".
I think that you mean that you have two switches, each one a momentary normally-open switch. Each one is going to have one side connected to ground and the other side connected to a different Arduino pin, with the Arduino pins set up for internal pull-ups.
Right so far?
If so, you don't need interrupts and you don't need debouncing. You just need to sample both switches in the loop() function and keep track of whichever one was pressed last (most recently). Then, execute the a routine or the b routine.
You haven't said what to do if both buttons are pressed, but this scheme lets you choose what happens. You also haven't said what to do at the beginning before any button has been pressed. Again, this scheme will let you choose what happens.
Interrupts are helpful in some cases, but not when things are happening at slow or human speed. Interrupts will just greatly complicate your life.
Thanks for the prompt replies ad apologies for my poor descriptions!
I definitely only have 1 switch (to be mounted on my enclosure), it has 2 pins / terminals on it whereas all the momentary switches Ive seen on the arduino learning site have 3 terminals that are used.
This was my starting point with buttons on the bread-board.
Now my code collates GPS, temp, pressure, Thermocouple & RPM data and writes them to an SD Card & displays parts of the information on the display. Whilst its doing the above I want to be able to press a button and change whats displayed on the screen. I thought I could use an interrupt to set a flag based on the button press so that as soon as the code loops it could check the flag and display the appropriate screen info.
Currently (using my 2 pin button switch and internal pull-up) it works but I have to hold the button down for a longer than desirable time whilst the code returns through the loop to the button state check.
I can get the Jeremy Blum example to work using the Schmitt trigger and the 3-pin momentary, but have not been able to figure out how to make it work for my 2-pin switch.
...I have to hold the button down for a longer than desirable time whilst the code returns through the loop to the button state check.
=====>>>>> Your code, please.
Perhaps you are using delay(...) or delayMicroseconds(...), which are evil and should be avoided for all but the simplest example programs. They are easily avoided but the avoidance requires structuring your code in a different way.
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking and has a very simple and effective debounce system.
I will have another look at the "running several things at once thread" and see if I can re-arrange my code.
Before reading the attached code, please note this is my first sketch and I haven't used an Arduino before - please be gentle, I know its far from perfect!!!
Attached is the current work in progress code.
I have started an updated version that initiates a second interrupt (already using one for a tachometer pickup from an alternator via a PC817) to check button state, but thats not really relevant at this stage. The attached works, just not real well on the button side of things.