// Read four Hall Flow sensors with Arduino MEGA
unsigned long lastmillis1 = 0;
unsigned long lastmillis2 = 0;
unsigned long lastmillis3 = 0;
unsigned long lastmillis4 = 0;
volatile int rpmcount1 = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm1 = 0;
volatile int rpmcount2 = 0;
int rpm2 = 0;
volatile int rpmcount3 = 0;
int rpm3 = 0;
volatile int rpmcount4 = 0;
int rpm4 = 0;
void setup(){
Serial.begin(9600);
attachInterrupt(0, rpm_fan1, FALLING);//interrupt cero (0) is on pin 2.
attachInterrupt(1, rpm_fan2, FALLING);//interrupt cero (1) is on pin 3.
attachInterrupt(4, rpm_fan2, FALLING);//interrupt cero (4) is on pin 19.
attachInterrupt(5, rpm_fan2, FALLING);//interrupt cero (5) is on pin 18.
}
void loop(){
delay(1000);
detachInterrupt(0); //Disable interrupt when calculating
rpm1 = (rpmcount1 * 60 / 7.5) * 0.264172; /* Convert frequency to GPH, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
lastmillis1 = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan1, FALLING); //enable interrupt
detachInterrupt(1); //Disable interrupt when calculating
rpm2 = (rpmcount2 * 60 / 7.5) * 0.264172; /* Convert frequency to GPH, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
lastmillis2 = millis(); // Uptade lasmillis
attachInterrupt(1, rpm_fan2, FALLING); //enable interrupt
detachInterrupt(4); //Disable interrupt when calculating
rpm3 = (rpmcount3 * 60 / 7.5) * 0.264172; /* Convert frequency to GPH, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
lastmillis3 = millis(); // Uptade lasmillis
attachInterrupt(4, rpm_fan3, FALLING); //enable interrupt
detachInterrupt(5); //Disable interrupt when calculating
rpm4 = (rpmcount4 * 60 / 7.5) * 0.264172; /* Convert frequency to GPH, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
lastmillis4 = millis(); // Uptade lasmillis
attachInterrupt(5, rpm_fan4, FALLING); //enable interrupt
Serial.print("GPH1 =\t"); //print the word "GPH1" and tab.
Serial.println(rpm1); // print the GPH value.
Serial.print("GPH2 =\t"); //print the word "GPH2" and tab.
Serial.println(rpm2); // print the GPH value.
Serial.print("GPH3 =\t"); //print the word "GPH3" and tab.
Serial.println(rpm3); // print the rpm value.
Serial.print("GPH4 =\t"); //print the word "GPH4" and tab.
Serial.println(rpm4); // print the rpm value.
Serial.println("..........................");
rpmcount1 = 0; // Restart the RPM counter
rpmcount2 = 0; // Restart the RPM counter
rpmcount3 = 0; // Restart the RPM counter
rpmcount4 = 0; // Restart the RPM counter
}
//}
void rpm_fan1(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount1++;
}
void rpm_fan2(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount2++;
}
void rpm_fan3(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount3++;
}
void rpm_fan4(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount4++;
}
I couldn't find a sketch to read 4 of these at once so i managed to cut and paste one together that was originally for reading motor rpm. I added a conversion to Gallons per hour but it can be configured for whatever. Here is the link to the original article.
You need a 10k pull up resistor on each of the signal wires.
I solved this problem by writing a new FlowCounters library to support multiple flow meters. It is here:
http://forum.arduino.cc/index.php?topic=236326.0