【问题标题】:Arduino Loop and alarmArduino循环和警报
【发布时间】:2013-01-01 20:07:43
【问题描述】:

我正在使用 arduino 进行我的第一个物理计算项目,实际上是 seduino stalker 2.1。我正在构建一个设备来记录一段时间内的集水率。

设置和运行项目并没有那么难,直到今天。在主循环中,我调用了处理日志记录的方法。我现在还有一个警报延迟来处理我需要的计时器重复,以便汇总数据并通过 SMS 将其发送到收件人号码。

问题在于,当 alarm.repeat() 处于活动状态时,它会抢先记录数据。问题是:为什么alarm.delay存在时,循环内的日志记录方法不起作用?

void setup() {
  Serial.begin(9600);
  Wire.begin();
  setTime(1,17,0,1,1,13); // set time
  Alarm.timerRepeat(60, Repeats); //set repeater

}

void loop(){
  logging(); //call logging
  Alarm.delay(1300); //for repeater
}

void Repeats(){
  Serial.println("the repeater fired"); // just to see it working
} 

void logging(){

   val = digitalRead(Sensor_Pin);  // read Sensor_Pin
  if (val == HIGH) {         
    // If Sensor N.C. (no with magnet) -> HIGH : Switch is open / LOW : Switch is closed 
    // If Sensor N.0. (nc with magnet) -> HIGH : Switch is closed / LOW : Switch is open 
    digitalWrite(Led_Pin, LOW);  //Set Led low
    //Serial.print("status -->");
    //Serial.println("low");
    //delay(500);
  } else {
    digitalWrite(Led_Pin, HIGH);  //Set Led high
      logdata();
  } 
}

void logdata(){

    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    File myFile = SD.open("datalog.txt", FILE_WRITE);

    // if the file opened okay, write to it:
    if (myFile) {

      //DateTime now = RTC.now();
      //String myString = readTimestamp(now);
      time_t t = now();
      String aDate = String(year(t))+"/"+String(month(t))+"/"+String(day(t))+" "+String(hour(t))+":"+String(minute(t))+":"+String(second(t));
      myFile.println(aDate);
    // close the file:
      myFile.close();
      Serial.println(aDate);
      delay(500); } else {
      // if the file didn't open, print an error:
      // Serial.println("error opening DATALOG.TXT");
    }
}

【问题讨论】:

    标签: arduino


    【解决方案1】:

    问:为什么我必须使用 Alarm.delay() 而不是 delay()? A:任务调度 在 Alarm.delay 函数中处理。任务被监控并 从 Alarm.delay 调用中触发,因此 Alarm.delay 应该是 每当您的草图需要延迟时调用。如果你的草图 等待外部事件(例如,传感器更改),确保 您在检查传感器时反复调用 Alarm.delay。

    来自警报库的常见问题解答。所以看起来 Alarm.Delay 就像标准延迟一样,但可以被预定事件打断。您的日志调用没有安排,它只是在循环开始时发生。 ..您的日志记录根本没有发生吗?看起来应该在每个循环开始时调用它,然后在延迟期间触发 1300 次延迟。

    【讨论】:

      【解决方案2】:

      在您的 logdata() 函数中,您调用的是 delay(50) 而不是 Alarm.delay(50)

      正如考德所指出的,当需要延迟时,您必须使用 Alarm.delay,否则延迟会干扰警报。

      【讨论】:

        【解决方案3】:

        我认为您可以使用计时器库以其他方式完成。如果您说数据必须每秒记录一次,那么通过计时器更容易完成。示例代码

        #include <SimpleTimer.h>
        
        // the timer object
        SimpleTimer timer;
        
        // a function to be executed periodically
        void repeatMe() {
            Serial.print("Uptime (s): ");
            Serial.println(millis() / 1000);
        }
        
        void setup() {
            Serial.begin(9600);
            timer.setInterval(1000, repeatMe);
        }
        
        void loop() {
            timer.run();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-14
          • 1970-01-01
          • 1970-01-01
          • 2016-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-21
          相关资源
          最近更新 更多