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 <SD.h>
#include <SPI.h>
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 <- I pressed here
2
3 <- I pressed here
4
5
6
7
8
9
10
11
12 <- I pressed here
13
14
15
16