while 循环的问题
Issue with while loop
对于我的项目,我正在尝试测量传感器和物体之间的距离以触发警报。如果警报响起,一条消息将发送到我的电报帐户,其中包含一个图书馆(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,所以我无法自己测试。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在java中解决这段代码时面临循环中的问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 退出简单while循环时出现问题
- 循环无限运行C++解决骑士之旅问题
- Visual Studio 2017循环自动向量化问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 嵌套While循环不起作用(C++问题)
- 使用 GMP 的 while 循环出现问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- OpenMP for 循环并行性问题
- 这个返回元素位置的基于循环的函数有什么问题?
- 为什么循环会导致指针出现问题?
- 使用基于范围的循环迭代矢量时出现问题
- 在 c++ 中循环遍历数组时遇到问题
- 运行无限循环的最小二叉树问题
- 在C++中循环访问自定义结构列表的小问题
- 在 for 循环中更新两个变量时遇到问题C++