我有和arduino的草图,需要使用TimeAlarms.h库在一个定时的时间表上执行几个操作。然而,其中一个操作,通过中断读取霍尔传感器,似乎与TimeAlarms库交互很差。我在这里使用TimeAlarms库:TimeAlarms.html,并从这里修改了霍尔传感器脚本:sensor
我可以自己运行大厅感应器代码。但是,当我试图与Alarm.timerRepeat一起运行霍尔传感器代码时,它会在输入check_flow函数后挂起。
运行下面的代码只输出enter CF,然后挂起。如果您尝试使用check_flow_alarm_delay函数(它使用延迟的TimeAlarm版本),也会发生同样的情况。
但是,如果您在安装程序中注释掉Alarm.timerRepeat(10, showseconds);,循环中的Alarm.delay(0);则可以很好地工作。
奇怪的是,如果您在sei();函数中注释掉了check_flow和cli();,那么脚本工作得很好,并且似乎在霍尔传感器上进行了正确的计数。为什么要这么做?我是否应该担心,我没有积极地设置sei()和cli()之间的时间,从而导致传感器的可靠性问题?
注意:您应该能够在没有霍尔传感器的情况下运行代码,输出仅为0升/小时。
// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 2; //The pin location of the sensor
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //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,
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
Alarm.timerRepeat(10, showseconds);
}
void loop ()
{
// Serial.println( second() );
// stalls at enter CF
// check_flow();
// stalls at enter CF
check_flow_alarm_delay();
Alarm.delay(0);
}
void showseconds ()
{
Serial.println( second() );
}
void check_flow ()
{
Serial.println("enter CF");
int Calc;
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
// sei(); //Enables interrupts
delay(1000); //Wait 1 second
// cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 5.5); //(Pulse frequency x 60) / 5.5Q, = flow rate in L/hour
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new line
}
void check_flow_alarm_delay ()
{
Serial.println("enter CFAD");
int Calc;
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
// sei(); //Enables interrupts
Alarm.delay(1000); //Wait 1 second
// cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 5.5); //(Pulse frequency x 60) / 5.5Q, = flow rate in L/hour
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new line
}发布于 2016-04-03 21:01:55
delay()使用中断。禁用它们会干扰功能。
https://stackoverflow.com/questions/36391087
复制相似问题