AttachInterrupt FALLING work as CHANGE (LM393 module)

Hey!

I am using that module :
http://www.ebay.fr/itm/171275012213?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

It is plugged on pin 3 of my arduino. And I am using it as a switch with attachInterrupt.

Here is my code :

void setup() { 
pinMode(3, INPUT);
attachInterrupt (1, button_push, FALLING);
}

void button_push(){
     ScreenChoice++;    
}

Here is the issue : ScreenChoice should increase each time i push the button. But it increses twice, when I touch the button and when I lift my finger.

Can you help me ?

Maybe the device sends two pulses, "make" and "break". Check for that using a digital I/O pin on the Arduino.

That could be a good thing. You can divide by two, or use the time between interrupts to make different choices.

I just checked with an I/O pin, and when i do not touch the button, it sends 1 and when i touch it it sends 0.
What more do i can do ?

What happens if you turn on the internal pullup resistor.

pinMode(3, INPUT_PULLUP);

Hmm... Perhaps the device shows the equivalent of "switch bounce". If so, there might sometimes be more than two changes with one touch or one release.

Does ScreenChoice always increment by exactly 2?

Riva:
What happens if you turn on the internal pullup resistor.

pinMode(3, INPUT_PULLUP);

Nothing has changed

jremington:
Hmm... Perhaps the device shows the equivalent of "switch bounce". If so, there might sometimes be more than two changes with one touch or one release.

Does ScreenChoice always increment by exactly 2?

You are true, I haven't noticed that as my "true" code is a bit different, but most of the time it increase by 3 or 4, and rarely by 2 and more than 4, but never by one.

Thanks !!

Is ScreenChoice declared volatile ?
If only you had posted your whole program ...

Yes declared volatile

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <TFT_ILI9163C.h>



// Color definitions
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0  
#define WHITE   0xFFFF

volatile int16_t curVal1 = 0;
volatile int16_t oldVal1 = 0;
bool echec=false;

int heure=13;
int minut=21;
float pression=1013.25;
float temp_int=24.54;
float temp_ext=40.32;
float humid_int=37.8;
float humid_ext=45.21;

#define __CS 10
#define __DC 9
#define __RESET 4

void DrawSecondaryScreenIndoor();
void DrawSecondaryScreenOutdoor();
void button_push();
volatile bool state=0;
volatile bool state_indoor=0;
volatile bool state_outdoor=0;
volatile bool switch_state=1;
volatile double instant=0;
volatile int ScreenChoice=1;

volatile bool state1 = 0;
volatile bool Oldstate1 = 0;

TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RESET); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  tft.begin();
  pinMode(3, INPUT);

  attachInterrupt (1, button_push, FALLING);
  
}

void loop() {

      
Serial.println(ScreenChoice);


   
}


void button_push(){
    state1 = !state1;   
  ScreenChoice++;
}

No worries for the useless variable.

It does sound like the equivalent of switch bounce. Most likely, there is a comparator measuring voltage levels, and noise on the input line causes multiple transitions on the output.

There are lots of posts on this forum, and even a library to deal with switch bounce.

Ok, thank you, i will try to find how to deal with that, or find the library.
Thanks

EDIT :
I found that library :
http://playground.arduino.cc/Code/Bounce
It works like this :

void setup() {

  // Setup the button with an internal pull-up :
  pinMode(BUTTON_PIN,INPUT_PULLUP);

  // After setting up the button, setup the Bounce instance :
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5); // interval in ms

}

void loop() {
  // Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  int value = debouncer.read();
}

It seems easy to use, but, how do i can implement it in my :

attachInterrupt (1, button_push, FALLING);

EDIT2 :
Maybe I should do it this way :

but, i will have to train to determine the debouncing time which fit the best!

Don't use attachInterrupt(). Just use the bounce library to read the sensor.

Trying to use an external interrupt with a bouncing switch does not follow the standard debounce template.

I would try a hardware debounce first with a .1uf cap between the output and ground.

If that doesn't work, you can revise the sketch to poll the pin with debounce.

Ok, I will try with a cap and let you know.

I am not really sure what does that mean :
"to poll the pin with debounce."

jremington:
Don't use attachInterrupt(). Just use the bounce library to read the sensor.

Seconded - reading a touch switch is not something requiring microsecond scale urgency,
just poll for it in loop() and software-debounce.