Pages: [1] 2   Go Down
Author Topic: Buttons, interrupts and de-bouncing  (Read 1847 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-lol

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But there are just 2 interrupts on the Arduino?
There is also - http://arduino.cc/forum/index.php?topic=87195.0 -

Quote
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 - http://macetech.com/store/index.php?main_page=product_info&products_id=23 -
lots of shields on shieldlist.org

also check - http://playground.arduino.cc//Code/Bounce - for debouncing code
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
But there are just 2 interrupts on the Arduino?

there are numerous interrupts.

Quote
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.


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

It is useless only in the hands of useless programmers.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can buy a MEGA
I have two Mega but they also have just 6 interrupt pins.
Quote from: 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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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... smiley
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-neutral
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5345
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin

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:
Quote
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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin


(edit: RTOSs, obviously smiley-razz )
« Last Edit: December 23, 2012, 08:10:44 pm by tuxduino » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Go where you feel the fun is (tm) smiley-grin
I better don't comment that *lol
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5345
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
 smiley-lol
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

Pages: [1] 2   Go Up
Jump to: