I set up my YF-S201 flow meter with my Arduino Uno board and had it working fine.
I then tried to replicate it using a LoLin Node MCU V3 ESp 8266
I copied the code across and modified the interrupt function using ICACHE_RAM_ATTR to get it running OK.
However I am just getting 0 returned as the flowrate even if I blow through the meter.
Can anyone see where my code may be wrong for the device?
Code below:
Thanks
volatile int flow_frequency; // Measures flow meter pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowmeter = D2; // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;
void ICACHE_RAM_ATTR flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{
pinMode(flowmeter, INPUT);
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
// see http://arduino.cc/en/Reference/attachInterrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}