while循环问题

时间:2019-10-07 10:57:59

标签: c++ arduino telegram telegram-bot esp8266

对于我的项目,我试图弄乱传感器与物体之间的距离以触发警报。如果警报响起,则会使用库(UniversalTelegramBot)将消息发送到我的电报帐户。为了弄清楚距离,我使用了一个到传感器和物体之间距离的循环,但是要检查我是否收到任何消息,我还需要使用一个循环。我要这样做,以使注释“ / off”再次变得混乱,但是当您键入其他内容时,它会等到注释“ / off”再次被使用。当警报打开并且正在检查消息时,我想使用“ /开始”作为注释,以查看要使用的其他注释以及如何使用它,而无需再次进入消息循环。

我的主要问题是,当警报响起并且我键入任何内容时,都没关系,该循环再次开始混乱,但是我只希望当我在电报中键入“ / off”时使其回到混乱状态

我的代码:

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);

int delayBetweenChecks = 250;
unsigned long lastTimeChecked;

void handleNewMessages(int numNewMessages) {

  for (int i = 0; i < numNewMessages; i++) {
    String chat_id = String(bot.messages[i].chat_id);
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;
    if (from_name == "")
      from_name = "Gast";

    Serial.print("De volgende knop is ingedrukt: ");
    Serial.println(text);

    if (text == F("/off")) {
      digitalWrite(LED_PIN, LOW);
      alarmStatus = 0;
      noTone(buzzer);
    }

    if (text == "/start") {
      String welcome = "Welkom bij jouw alarmsysteem, " + from_name + ".\n";
      welcome += "/off : om het alarm uit te zetten\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  cm = (duration / 2) / 29.1;   // Divide by 29.1 or multiply by 0.0343
  inches = (duration / 2) / 74; // Divide by 74 or multiply by 0.0135

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  while (cm <= 9) {
    Serial.println("ALARM!");
    digitalWrite(LED_PIN, HIGH);
    tone(buzzer, 250);
    alarmStatus = 1;
    if (alarmStatus = 1) {
      if (millis() > lastTimeChecked + delayBetweenChecks) {
        int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        handleNewMess ages(numNewMessages);
        if (numNewMessages) {
          Serial.println("got response");
          handleNewMessages(numNewMessages);
        }
        lastTimeChecked = millis();
      }
    }
    break;
  }
  delay(250);
}

1 个答案:

答案 0 :(得分:1)

如果我正确理解,则可以保留两个单独的状态变量。一种用于当您离开/返回家时打开和关闭闹钟,另一种用于触发闹钟时。

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

const int delayBetweenChecks = 250;

bool alarmStatus = false;    // off by default
bool alarmTriggered = false; // off by default

UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);

double measure_distance_in_cm() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  unsigned long duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  return static_cast<double>(duration) / (29.1 * 2.);
}

void trigger_on() {
  digitalWrite(LED_PIN, HIGH);
  alarmTriggered = true;
}

void trigger_off() {
  digitalWrite(LED_PIN, LOW);
  alarmTriggered = false;
}

void handleNewMessages() {
  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

  for (int i = 0; i < numNewMessages; i++) {
    String text = bot.messages[i].text;

    Serial.println(text);

    if (text == "/start") {     // when you're leaving home
      alarmStatus = true;
      trigger_off();
    }
    else if (text == "/stop") { // when you're returning home
      alarmStatus = false;
      trigger_off();
    }
    else if (text == "/off") {  // turn off a triggered alarm
      trigger_off();
    }
    else if (test == "/on") {   // force triggering of the alarm
      trigger_on();            
    }
  }
}

void loop() {
  static unsigned long lastTimeChecked = millis();

  handleNewMessages();

  if(alarmStatus) {                // you only need to do this if the alarm is on
    if(alarmTriggered == false) {  // only measure if the alarm isn't already triggered  
      double cm = measure_distance_in_cm();
      if(cm <= 9.) {
        Serial.println("ALARM!");
        trigger_on();
      }
    }
    // If it's triggered: Sound the alarm in 200ms bursts until /off or /stop is received 
    if(alarmTriggered) tone(buzzer, delayBetweenChecks-50);
  }

  // try to keep the polling at a steady pace
  lastTimeChecked += delayBetweenChecks;
  unsigned long now = millis();
  if(lastTimeChecked > now) delay(lastTimeChecked - now);
}

免责声明:我没有Arduino,所以无法自己测试。