我将我的 YF-S201 流量计连接到我的 Arduino Uno 并让它工作正常。
然后,我使用 LoLin 的 ESP8266 NodeMCU V3
开发板复制了代码。
我修改了添加 ICACHE_RAM_ATTR 的中断函数。
代码运行正常,但即使仪表旋转,我现在也只得到“0”值。
我需要修改原始 Arduino Uno 代码中的其他内容才能使其正常工作吗?
代码如下:
谢谢
代码:
全选vola
tile 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
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");
}
}