Buttons, interrupts and de-bouncing

Hi,

i just figured out that switching is not as easy as it seams.
Yes i recognized that sometimes the reaction on my buttons are poor - but i always
thought my buttons are cheap or i did not push enough XD

So now i was reading about de-bouncing and seams like that the Schmitt-Trigger solution is the
best choice. But when i was reading i stumbled over interrupts.
So attaching my buttons to the digital pins is not the best choice cause you need to
read the pins in a loop all the time to see if there is a change.

But there are just 2 interrupts on the Arduino?
So what to to if i want (for example) to switch 24 devices with 24 switches?

That also brings me to the question: every time i have something that only has a
low and high as input would better go to an interrupt (also if its maybe a optocoupler you read just to see on and off)?
But that would mean that "digitalRead INPUT" is useless?

But there are just 2 interrupts on the Arduino?

There is also - Arduino Forum -

So what to to if i want (for example) to switch 24 devices with 24 switches?

You can buy a MEGA or a shield like - Centipede Shield V2 - Macetech Electronics Store -
lots of shields on shieldlist.org

also check - Arduino Playground - HomePage - for debouncing code

So now i was reading about de-bouncing and seams like that the Schmitt-Trigger solution is the
best choice.

Whether something is best depends on the application.

But there are just 2 interrupts on the Arduino?

there are numerous interrupts.

So what to to if i want (for example) to switch 24 devices with 24 switches?

Switching 24 devices is not the same as reading 24 switches.

But that would mean that "digitalRead INPUT" is useless?

It is useless only in the hands of useless programmers.

robtillaart:
You can buy a MEGA

I have two Mega but they also have just 6 interrupt pins.

dhenry:
It is useless only in the hands of useless programmers.

Sound like a sentences from somebody who was born and did
know programming and never had to start and learn.

I have two Mega but they also have just 6 interrupt pins.

You don't have to tie each interrupt to just one button. You can for example have a row of buttons all trigger the same interrupt, and have the ISR figure out which button(s) it was that was pressed by immediately reading each button status.
Off of top of my head I don't know how this would be wired/coded in exact details, but I recall something similar is implemented in chips like PCF8574: it triggers an int, then the ISR asks the chip its pin status (8 pins => 1 byte, i2c => very fast). Have a sample sketch running on a breadboard with a UNO. 2 arduino pins, 8 buttons. Not bad... :slight_smile:

I have two Mega but they also have just 6 interrupt pins.

No, you can have an interrupt for any pin just as tuxduino describes.

There is a library for it. Read this thread - http://arduino.cc/forum/index.php?topic=87195.0 -

I will read i later. But just from overflying it i thought its just for the ATmega328p.

So does it make sense to always use interrupts for switching state instead of using
just digital IN and look at it all the time in a loop?
Please correct me if my logical thinking is not right :expressionless:

Polling is perfectly usable to manage buttons, provided you read the pin status at least once in 20 to 50 ms. That means banning delay() and switching to state-based programming.

I think the real problem is using interrupts to detect button presses.

It sounds neat/cool/whatever but it's not really a good idea in practice...

Cool? Hm, i think nobody ever will ask: "hey how does this button work? do you use interrupts?"
If i build a egg timer for my 60 years old Mother. She never ever will look at the code or ask.
An Arduino for a egg timer *looool
But who knows. When Skynet comes alive and all machines communicate, maybe the egg timer is
in charge for the timing of the Terminator sorties :smiley:

I just want to figure out whats the right way to do it. If you need to check the inputs
in the loops then the Atmega uses energy you maybe need for other stuff.
I always think that stuff needs to be build that it uses the least energy.
The other thing i read at Jeremy's (Blum) blog is:

Interrupts allow you run a program, while still being able to react to asynchronous input from the outside world.

So you can do stuff at the same time.

Yes, ints are cool. So i multitasking. ROTSs are even cooler. Superloops are a bit less cool. But they're easier to grasp, IMHO.

That said, use whatever technique you feel more inclined to learn. Go where you feel the fun is (tm) :smiley:

(edit: RTOSs, obviously :stuck_out_tongue: )

tuxduino:
Go where you feel the fun is (tm) :smiley:

I better don't comment that *lol

MrGlasspoole:
Cool? Hm, i think nobody ever will ask: "hey how does this button work? do you use interrupts?"
If i build a egg timer for my 60 years old Mother. She never ever will look at the code or ask.

I mean the person who writes the program...

fungus:
I mean the person who writes the program...

So we need shirts now for the cool people with the print on them: I am a interrupter!
XD

Come on, let's be serious. The t-shirt should read "I'm an ISR writer". Now how about that for making people go "wow" when you pass by ? :smiley:

So does it make sense to always use interrupts

No. Nor does it make sense to always use polling.

As a matter of fact, it doesn't make sense to always do anything.

Ok, more confused :roll_eyes:
I found this from Ladyada and she writes:
"To change the pins or number of buttons, just put them in the array called “buttons” and the rest of the code will automatically adjust."

The second sketch uses interrupts. But she is using analog inputs and how can i just ad buttons if i can't just
use every input pin as interrupt??

This sketch also says: "which pin to use for reading the sensor? can use any pin!"
https://raw.github.com/adafruit/Adafruit-Flow-Meter/master/Adafruit_FlowMeter.pde