I am using interrupts to read the frequency from six hall effect sensors to measure water flow rate. When I plug one sensor in and simulate flow, I get the frequency output for that sensor but I also get random frequencies being output from the other pins. It essentially works by counting every rising (high-low) condition of the pin within a second. I cannot figure out why this is happening. Maybe I do not know how the interrupts work exactly or maybe it is a hardware issue I am not aware of. Thanks in advance for the help.
Code:
volatile int pin0;
volatile int pin1;
volatile int pin2;
volatile int pin3;
volatile int pin4;
volatile int pin5;
double Calc0;
double Calc1;
double Calc2;
double Calc3;
double Calc4;
double Calc5;
int m0 = 2;
int m1 = 3;
int m2 = 18;
int m3 = 19;
int m4 = 20;
int m5 = 21;
void rpm0()
{
pin0++;
}
void rpm1()
{
pin1++;
}
void rpm2()
{
pin2++;
}
void rpm3()
{
pin3++;
}
void rpm4()
{
pin4++;
}
void rpm5()
{
pin5++;
}
void setup() //
{
pinMode(m0, INPUT);
pinMode(m1, INPUT);
pinMode(m2, INPUT);
pinMode(m3, INPUT);
pinMode(m4, INPUT);
pinMode(m5, INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(m0), rpm0, RISING);
attachInterrupt(digitalPinToInterrupt(m1), rpm1, RISING);
attachInterrupt(digitalPinToInterrupt(m2), rpm2, RISING);
attachInterrupt(digitalPinToInterrupt(m3), rpm3, RISING);
attachInterrupt(digitalPinToInterrupt(m4), rpm4, RISING);
attachInterrupt(digitalPinToInterrupt(m5), rpm5, RISING);
}
void loop ()
{
pin0 = 0;
pin1 = 0;
pin2 = 0;
pin3 = 0;
pin4 = 0;
pin5 = 0;
sei();
delay (1000);
cli();
Calc0 = pin0;
Calc1 = pin1;
Calc2 = pin2;
Calc3 = pin3;
Calc4 = pin4;
Calc5 = pin5;
Serial.print (Calc0);
Serial.print (" ");
Serial.print (Calc1);
Serial.print (" ");
Serial.print (Calc2);
Serial.print (" ");
Serial.print (Calc3);
Serial.print (" ");
Serial.print (Calc4);
Serial.print (" ");
Serial.print (Calc5);
Serial.print (" ");
Serial.println ("L/hour");
}
Results:
(In this case I had a single sensor plugged into digital pin 20, #4 in my code. Only that pin should have values in the serial monitor, but I get results on pin 3 as well. Most of the time it is just one extra pin but sometimes maybe as much as 3 or 4 other pins.)
0.00 0.00 0.00 0.00 0.00 0.00 L/hour
0.00 0.00 0.00 16.00 16.00 0.00 L/hour
0.00 0.00 0.00 231.00 231.00 0.00 L/hour
0.00 0.00 0.00 271.00 271.00 0.00 L/hour
0.00 0.00 0.00 172.00 172.00 0.00 L/hour
0.00 0.00 0.00 111.00 111.00 0.00 L/hour
0.00 0.00 0.00 68.00 68.00 0.00 L/hour
0.00 0.00 0.00 33.00 33.00 0.00 L/hour
0.00 0.00 0.00 6.00 6.00 0.00 L/hour
0.00 0.00 0.00 0.00 0.00 0.00 L/hour
Also, I am using a MEGA 2560 with YF-S201 Flow Sensors. The pins I chose are from the MEGA 2560 interrupt pins listed on this page: attachInterrupt() - Arduino Reference