2016-04-03 6 views
0

私は、TimeAlarms.hライブラリを使用して、時間通りのスケジュールでいくつかの操作を行う必要があるarduinoスケッチを持っています。しかし、割り込みの1つであるホールセンサーを読み取る操作の1つは、TimeAlarmsライブラリとのやりとりが難しいようです。 私はここからTimeAlarmsライブラリを使用しています:http://www.pjrc.com/teensy/td_libs_TimeAlarms.html そしてここからホールセンサのスクリプトを適応している: http://www.seeedstudio.com/wiki/G3/4_Water_Flow_sensorArduinoがTimeAlarms.hに干渉するのを中断します。

私はそれ自身の罰金にホールセンサのコードを実行することができます。しかし、と一緒にホールセンサコードを実行しようとすると、check_flow機能を入力した後にハングします。

以下のコードを実行するとenter CFのみが出力され、ハングアップします。代わりにcheck_flow_alarm_delay関数を試行し、TimeAlarmバージョンの遅延を使用する場合も同じことが起こります。

Alarm.delay(0);のループでは、Alarm.timerRepeat(10, showseconds);をコメントアウトするとホールセンサーが正常に動作します。

check_flow機能でsei();cli();をコメントアウトすると、スクリプトが正常に機能し、ホールセンサーで正しくカウントされたようです。なぜこれはうまくいくのですか?そして、私がsei()cli()の間に時間を積極的に設定していないことを心配しなければならないと、センサーに信頼性の問題が生じますか?

注:ホールセンサを実際に持たずにコードを実行できるようにする必要があります。出力は0L /時になります。

// 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 
} 

答えて

1

delay()は割り込みを使用します。それらを無効にすると、その機能に干渉します。

+0

これはあなたの問題だと思われます。おそらく割り込みを使わずにコードを書き直さなければならないでしょう。 http://stackoverflow.com/questions/36382676/arduino-uno-r3-input-pins-with-gsm-shield/36392173で似たような質問に返信(サンプルコードあり)を参照してください。 –

関連する問題