极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17583|回复: 5

我需要把3个霍尔水量传感器接到ARDUINO 板子上

[复制链接]
发表于 2017-8-31 17:35:45 | 显示全部楼层 |阅读模式
我需要把3个霍尔水量传感器接到ARDUINO 板子上,分别接到了pin 2 4 7引脚上,可是为什么只有一个传感器能在串口上显示数据,其他没有反应,然后显示三个一样的数?求大仙帮忙


// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://hotmcu.com
#define NbTopsFan1_PIN 2     
#define NbTopsFan2_PIN 4     
#define NbTopsFan3_PIN 7   
volatile int NbTopsFan1;                       // measuring the rising edges of the signal
volatile int NbTopsFan2;                       // measuring the rising edges of the signal
volatile int NbTopsFan3;                       // measuring the rising edges of the signal
int Calc1;
int Calc2;
int Calc3;


void rpm ()                                   // This is the function that the interupt calls
{
    NbTopsFan1++;
    NbTopsFan2++;
    NbTopsFan3++;// This function measures the rising and falling edge of the hall effect sensors signal
}


void setup()
{

    Serial.begin(9600);                       // This is the setup function where the serial port is initialised,
    attachInterrupt(0, rpm, RISING);          // and the interrupt is attached
}


void loop ()
{
    NbTopsFan1 = 0;  
    NbTopsFan2 = 0; // Set NbTops to 0 ready for calculations
    NbTopsFan3 = 0;
    sei();                                    // Enables interrupts
    delay (1000);                             // Wait 1 second
    cli();                                    // Disable interrupts

    Calc1 = (NbTopsFan1 * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
    Calc2 = (NbTopsFan2 * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
    Calc3 = (NbTopsFan3 * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour

    Serial.print (Calc1, DEC);                 // Prints the number calculated above
    Serial.print (Calc2, DEC);                 // Prints the number calculated above
    Serial.print (Calc3, DEC);                 // Prints the number calculated above

    Serial.print(" L/hour\r\n");             // Prints "L/hour" and returns a  new line
}
回复

使用道具 举报

发表于 2017-9-1 09:32:03 | 显示全部楼层
attachInterrupt(0, rpm, RISING);   
这里只用了int.0啊,当然只有接在pin2上的有反应了。普通Arduino板子,中断0本来就就在pin2上,中断1在pin3
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-6 19:22:22 | 显示全部楼层
wangbin526 发表于 2017-9-1 09:32
attachInterrupt(0, rpm, RISING);   
这里只用了int.0啊,当然只有接在pin2上的有反应了。普通Arduino板 ...

我是改程序还是加扩展?谢谢您改程序怎么改?加扩展用什么板子?谢谢
回复 支持 反对

使用道具 举报

发表于 2017-9-8 22:41:34 | 显示全部楼层
本帖最后由 wangbin526 于 2017-9-9 12:38 编辑
lona21 发表于 2017-9-6 19:22
我是改程序还是加扩展?谢谢您改程序怎么改?加扩展用什么板子?谢谢


    不是说了啊,一般普通Arduino板也就2个中断,也就是说如果想用中断精确数脉冲的话,最多只能接两个水量计。要接两个以上,你得选中断多点的板子,比如 Mega2560有6个,Due所有IO都行,当然十几块的nodemcu也是全端口都行,同样用Arduino IDE。当然非得用普通328的arduino接两个以上中断,也有(https://github.com/GreyGnome/PinChangeInt)这个库可以用。
    按你的要求改了下代码,2、3口都能接一个流量计。但建议自己去看看中断教程,非得像你给的例子,在主循环里开关中断和delay的话,用这代码的板子,将来除了测流量之外就啥也别干了。


  1. #define NbTopsFan1_PIN 2     
  2. #define NbTopsFan2_PIN 3
  3. volatile  unsigned long NbTopsFan1;                       // measuring the rising edges of the signal
  4. volatile  unsigned long NbTopsFan2;                       // measuring the rising edges of the signal
  5. int Calc1;
  6. int Calc2;

  7. void rpm1 ()                                   // This is the function that the interupt calls
  8. {
  9.     NbTopsFan1++;
  10. }

  11. void rpm2 ()                                   // This is the function that the interupt calls
  12. {
  13.     NbTopsFan2++;
  14. }

  15. void setup()
  16. {

  17.     Serial.begin(9600);
  18.     pinMode(2, INPUT_PULLUP);
  19.     pinMode(3, INPUT_PULLUP);
  20.     attachInterrupt(0, rpm1, RISING);          // and the interrupt is attached
  21.     attachInterrupt(1, rpm2, RISING);          // and the interrupt is attached
  22. }

  23. void loop ()
  24. {
  25.     NbTopsFan1 = 0;  
  26.     NbTopsFan2 = 0; // Set NbTops to 0 ready for calculations
  27.     interrupts();                               // Enables interrupts
  28.     delay (1000);                             // Wait 1 second
  29.     noInterrupts();                           // Disable interrupts

  30.     Calc1 = (NbTopsFan1 * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
  31.     Calc2 = (NbTopsFan2 * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour

  32.     Serial.print (Calc1, DEC);                 // Prints the number calculated above
  33.     Serial.print (Calc2, DEC);                 // Prints the number calculated above

  34.     Serial.println(" L/hour");             // Prints "L/hour" and returns a  new line
  35. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-11 17:44:37 | 显示全部楼层
谢谢您,
我买了一个 IO Sensor Shield V1,是不是可以解决3个霍尔水量传感器的问题,请您赐教
回复 支持 反对

使用道具 举报

发表于 2017-9-12 18:34:28 | 显示全部楼层
本帖最后由 wangbin526 于 2017-9-12 18:37 编辑
lona21 发表于 2017-9-11 17:44
谢谢您,
我买了一个 IO Sensor Shield V1,是不是可以解决3个霍尔水量传感器的问题,请您赐教


    无语,老大,你木有基础的吗?之前说的很清楚了啊,UNO只有2个中断,这是主控的问题,外设扩展板只是接线省力点,又木有换主控,还是只有2个中断可以用啊。
    再说一遍,你要接3个霍尔水量,下面三条路随你选,还看不懂的话建议重看遍基础教程吧
1、买块MEGA 2560,这货主控有6个中断,可以接6个
2、如果你这板子就只用来测流量,那别用中断数脉冲了,直接loop里轮询引脚状态数脉冲,这样不用中断,UNO有几个引脚就能接几个水量传感器,最多流速快的时候不太准而已,反正霍尔流量传感器本身就不太准,不在使用环境中标定的话或者水压不稳定的话,偏差个50%都一点不奇怪。
3、用PinChangeInt库(https://github.com/GreyGnome/PinChangeInt),给UNO扩展几个中断出来,当然代价是部分PWM和Timer不能用,非官方的冷门库不一定太稳定。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 15:39 , Processed in 0.056079 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表