Pages: [1]   Go Down
Author Topic: Unreliable input from button  (Read 372 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello ppl,
I am testing input on the Arduino via this kind of buttons:

http://www.robot-italy.com/it/pulsanti-quadrati-colorati-assortimento-15pz.html

Unfortunately, the input seems to be unreliable. I've linked the button to a "volatile" counter in my sketch, and I increment the counter each time the button is pressed. Unexpectedly, sometimes the counter is incremented more than one time for each press (say, it steps from 3 to 7 instead of 4).

This is the code I'm using to the bare minimum. I attach also an image of the circuit (but it's very simple).

Code:
#define INPUTPIN  3

volatile int counter=0;

void changeState()
{
  counter++;
}

void setup()
{
  digitalWrite(INPUTPIN,HIGH);
  attachInterrupt(1,changeState,RISING);
 
  Serial.begin(9600);
}

void loop()
{
  Serial.println(counter);
 
  delay(1000);
}

Thanks in advance for your help.


* 04012013-P1040656.jpg (319.97 KB, 1280x960 - viewed 11 times.)
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 508
Posts: 31431
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How are they wired?
You might have contact bounce problems using interrupts like this.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your problem is call "bounce". Look at the debounceing example.

Mark

Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1183
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

and you have the switch pulling down the input ( correct ) so you are looking for FALLING when you press the switch.

You would be better as the guys say to use the normal debounce methods
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

New Jersey
Online Online
Faraday Member
**
Karma: 48
Posts: 3409
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have a particular need to use interrupts? Polling will work just as well, unless you're using the button to emulate something that will generate large numbers of pulses later. You'll still have to debounce with polling though too.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for introducing me to the "bounce" problem smiley

The situation is much better using this type of approach in the interrupt function:

Code:
void changeState()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();

  // debounce 
  if ((interrupt_time - last_interrupt_time) > 100)
  { 
    counter++;
  }
 
  last_interrupt_time = interrupt_time;
}

Also, "millis" should work in this context since it should show the time the interrupt function was called.

Thanks
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use micros() not millis() for debouncing and note that millis() and micros() are not incremented during interrupts.

Mark
Logged

Pages: [1]   Go Up
Jump to: