I am just getting started with Arduino again after a few years of not touching it. I am using a water flow sensor that I need to count the pulses from in hz and I am also using an xbee shield. The xbee shield documentation seems to show that it uses digial ports 2 and 3 which are also the only two interrupts on the duemilanove. My question is how do I count the pulses from the water flow sensor on another digital pin without using the interrupt pins 2 & 3?
By the way this is my working code USING interrupts, but like I said I need to get away from pins 2 & 3 so I can use them for the xbee.
// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 2; //The pin location of the sensor
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
// The setup() method runs once, when the sketch starts
void setup() //
{
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is initialised,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 5.5); //(Pulse frequency x 60) / 5.5Q, = flow rate in L/hour
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new line
}
Well with a little digging in the xbee shield documentation, I was able to switch the board to use UART com instead of pins 2 and 3 so i was able to maintain the interrupt for the sensor.
How long does the pulse last? A 2 hour pulse could be read by an Arduino that was sleeping 99.9 % of the time, and still not be missed. A 2 nanosecond pulse may not be long enough to even trigger an interrupt.
What else is the Arduino doing? Have you determined that interrupts are necessary?
Perhaps a Leonardo, where pins 0 and 1 are associated with the Serial1 instance, rather than the Serial instance, could be used.