Hello everyone,
I'm new to this forum, and this is my first post here. I hope I'm not posting in the wrong place.
Alright, so I have built a laptop carrying robot that is powered by two DC motors. I'm using the Arduino as an interface between my laptop and the robot's electronics. I want the Arduino to tell my computer the RPM of the robot's wheels.
Since the motors I'm using don't have built-in encoders, I have used a reed switch/magnet combination.
To test it, I fixed a magnet to the robot's wheel, and connected a reed switch to the Arduino. I have set up hardware interrupts to fire (on a "falling" pulse) whenever the magnet passes the reed switch. It fires on a falling pulse because the reed switch connects pin 2 to ground whenever a magnet passes it.
I have also enabled the internal pull-up on pin 2 so that it normally stays high. See code below:
pinMode(2, INPUT); //Pin2 is connected to the reed switch
digitalWrite(2, HIGH); //Enable internal pull-up
attachInterrupt(0, rpm_fun, FALLING);
The rpm_fun function just sends a message to the computer:
Serial.println("Received a pulse!");
Now, the problem is that this function fires twice whenever the magnet passes it. I tried turning the wheel slowly, and noticed that as soon as the reed switch senses the magnet, it fires the interrupt function twice. Why is that? Shouldn't there be just one "falling pulse" from high to low?
Looking forward to a response.