I have a pulse meter on a fuel pump that sends out a pulse for every 0.1 gallons. That pulse meter triggers a relay to turn on and off. The switched side of the relay switches a 5v signal from the Arduino and the other side goes to pin 2 on the Arduino. I also have a 10k resistor being used as a pullup resistor.
I am using interrupts to keep track of gallons passed through the meter. The problem I have is that the interrupts are getting triggered inconsistently. Sometimes it double triggers, triple, etc. Sometimes it doesn't even fire at all.
The signal triggering the relay is consistent. I have even tried a second arduino to trigger the relay on consistent intervals and I come across the same problem. I have many hours into trying to solve this problem. Any help would be appreciated. I have attached my code below.
Why? If you are using a pullup resistor, all the relay needs to do is ground the wire from pin 2. The way you have the circuit, pin 2 is switched from 5 volts on the resistor to 5 volts on the Arduino.
From your description, it's contact bounce. An interrupt can trigger many times in a millisecond, so if the contact makes 10 bounces before settling(usually within a few ms), you'll have many increments to your flow.
You need to debounce the signal, and debouncing(filtering) and interrupts don't go well together. An electronic debounce with a single output transition would do you better if you're sticking with the interrupt approach.
However, how fast is your pump pumping? My guess is 60 gpm would be extreme; at that rate, you'd be pulsing 10 times per second. No need for an interrupt here, it's just a simple contact closure and debounce-in-software.
First, there's no reason to use a 'float' to count discrete events. Use an unsigned integer type, it will be more efficient, especially on processors that don't have hardware support for floating point calculations.
What type of Arduino board? Access to a 4-byte float will be non-atomic on an 8-bit AVR processor. It needs to be protected with a Critical Section.
EDIT: @alto777 just mentioned that below while I was typing.