Count RISING edges of a wave form by using ARDUINO I have realised an electrical circuit that gives 5V in output. When I press a button, it gives 0V.
So I have in output a square wave (5V - 0V).
I would like to count how many times I press the button, by using an Arduino MEGA AT2560.
I have written this code below, but it doesn't work, because when I press the button, it increase the counter three or four times.
For example: I press the button 5 times, but the counter has arrived to 30!

#include &lt;SD.h&gt;
#include &lt;SPI.h&gt;

unsigned int count_hall_f = 0;

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, interrFront, RISING); //18  
}

void loop(){

}

void interrFront() {
  count_hall_f++;
  Serial.println(count_hall_f);
}


An example of the result is:

1 &lt;- I pressed here
2
3 &lt;- I pressed here
4
5
6
7
8
9
10
11
12 &lt;- I pressed here
13
14
15
16
I have choosen the values of R and C by experiments. Do you have a suggest for choose these values?


I have a page about switches - somewhat down the page is a discussion of debouncing, including some calculations which show how you can calculate the debounce time. With the switch pin pulled high by the internal pull-up (something you don't appear to be doing), and a 1 µF capacitor, that gives you a debounce of about 35 ms.



Example schematic:



Results on input pin:



(Maths supporting that figure on the above page).



Note that it is important to have a pull-up or pull-down for your switch or it will read random values if not closed.