while 循环的问题

Issue with while loop

本文关键字:问题 循环 while      更新时间:2023-10-16

对于我的项目,我正在尝试测量传感器和物体之间的距离以触发警报。如果警报响起,一条消息将发送到我的电报帐户,其中包含一个图书馆(UniversalTelegramBot(。为了发送距离,我使用了一个循环到传感器和物体之间的距离,但要检查我是否通过任何消息,我还需要使用循环。我想让它通过注释"/off"再次测量距离,但是当您键入其他内容时,它会等到注释"/off"再次使用。当警报打开并检查消息时,我想使用"/start"作为注释以查看要使用哪些其他注释以及如何使用它,而无需再次进入 messure 循环。

我的主要问题是,当闹钟响起并且我键入任何内容时,不管是什么,循环再次开始消息传递,但我只希望在我在电报中键入"/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 zettenn";
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);
}

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

#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,所以我无法自己测试。