红正,黑伏,黄接D2 (3.6-26.4V)
根据已知的1升的脉冲数计算
#define io_shuiLiu_chuanGanQi 12
long xianZai_Millis = 0; //现在时间
long yiQian_Millis = 0; //以前时间
long timeMillis=0;
int jianGe_time = 1000; //间隔时间
volatile byte maiCong_jiShu=0; //脉冲计数
byte pulse1Sec = 0; //脉冲1秒
float liuLiang=0.0; //流量
unsigned int liuLiang_haoSheng=0; //流量毫升
unsigned long liuLiang_haoSheng_zongJi=0; //总计毫升
int maiChong_meiSheng=1319; //每升总共多少脉冲,铜的1L=553HZ±10%
void IRAM_ATTR maiCong_jiShuer()
{
maiCong_jiShu++;
}
void setup()
{
Serial.begin(9600);
pinMode(io_shuiLiu_chuanGanQi, INPUT);
attachInterrupt(digitalPinToInterrupt(io_shuiLiu_chuanGanQi), maiCong_jiShuer, FALLING);
}
void loop()
{
xianZai_Millis = millis();
timeMillis=xianZai_Millis-yiQian_Millis;
if (timeMillis>jianGe_time){
pulse1Sec = maiCong_jiShu;
maiCong_jiShu = 0;
liuLiang = 1000.0/timeMillis*pulse1Sec/maiChong_meiSheng*60;//L/min
yiQian_Millis = millis();
liuLiang_haoSheng = liuLiang/60*1000;//mL
liuLiang_haoSheng_zongJi += liuLiang_haoSheng;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(liuLiang,1); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
Serial.print("Output Liquid Quantity: ");
Serial.print(liuLiang_haoSheng_zongJi);
Serial.print("mL / ");
Serial.print(liuLiang_haoSheng_zongJi / 1000);
Serial.println("L");
}
}
中断测量上升沿测量方式:
volatile int shangShengYan; //测量信号的上升沿
float jiSuan;
int io_shuiLiu_chuanGanQi = 2; //传感器的针脚位置
void rpm () //这是interupt调用的函数
{
shangShengYan++; //此功能测量霍尔效应传感器信号的上升沿和下降沿
}
void setup()
{
pinMode(io_shuiLiu_chuanGanQi, INPUT); //将数字管脚2初始化为输入
Serial.begin(9600); //串口波特率
attachInterrupt(io_shuiLiu_chuanGanQi, rpm, RISING); //中断被附加
}
void loop ()
{
shangShengYan = 0; //将NbTops设置为0,以便进行计算
sei(); //启用中断
delay (1000); //等待一秒
cli(); //禁用中断
jiSuan = (shangShengYan * 60 / 24); //(脉冲频率x 60)/7.5Q,=流速(L/小时)
Serial.print (jiSuan, 2); //打印上面计算的数字
Serial.println (" L/h");
Serial.print (jiSuan/60); //打印上面计算的数字
Serial.println (" L/s");
}
测量高电平和低电平的时间和,算频率的计算方式:
const int input = 2;
unsigned long X;
unsigned long Y;
float TIME = 0;
float PinLv = 0;
float WATER = 0;
float TOTAL = 0;
float LS = 0;
void setup()
{
Serial.begin(9600);
pinMode(input, INPUT);
}
void loop()
{
X = abs(pulseIn(input, HIGH));
Y = abs(pulseIn(input, LOW));
TIME = X + Y;
PinLv = 1000000 / TIME;
// WATER = PinLv / 7.5;
// LS = WATER / 60;
// TOTAL = TOTAL + LS;
Serial.print(PinLv);
//升每小时
Serial.println("Hz");
Serial.print(PinLv/22*60);
Serial.println("L/H");
//升每分钟
Serial.print(PinLv/22);
Serial.println("L/S");
delay(100);
}