简体   繁体   English

布尔运算符 Arduino

[英]Boolean operator Arduino

I'm trying to use this Arduino code and execute some lines while the VALUE is TRUE but, even if I set the VALUE to FALSE it seems that VALUE stay TRUE in void loop().我正在尝试使用此 Arduino 代码并在 VALUE 为 TRUE 时执行一些行,但是,即使我将 VALUE 设置为 FALSE,似乎 VALUE 在 void loop() 中仍为 TRUE。 I have tried changing the IF order inside the void loop() and also replace IF statement with WHILE, all with the same results.我曾尝试更改 void loop() 中的 IF 顺序,并用 WHILE 替换 IF 语句,所有结果都相同。 What am I doing wrong?我究竟做错了什么? Any suggestion is appreciated.任何建议表示赞赏。

#include <ESP8266WiFi.h>

const char* ssid = "YOURWIFI SSID";
const char* password = "YOURWIFI PASSWORD";
boolean value = true;

void setup() {

  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  //  if (static_ip) {
  //    WiFi.config(ip, gateway, subnet);
  //  }

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}


void reconnect() {
  Serial.print(" WiFi connected - IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("at the setup");
  Serial.println(value);

  if (value == true)

  {
    //
    //  DO SOMETHING REAL HERE
    //
    
    Serial.println("check before change");
    Serial.println(value);

    boolean value = false;

    Serial.println("after change to false");
    Serial.println(value);
  }
}

void loop() {
  Serial.println("just got in the loop()");
  Serial.println(value);

    if (value == false)
    {
      Serial.println("before sleep value should be false");
      Serial.println(value);
      ESP.deepSleep(0);
  
    }
    else
    {
      Serial.println("the value must be true if i got here");
      Serial.println(value);
      reconnect();
  
    }

}

in line 44 boolean value = false;在第 44 行布尔值 = false; this declare a local variable value and set it to false witch is not your global value这声明了一个局部变量值并将其设置为 false 女巫不是您的全局值

arduino suggest you use bool instead of boolean arduino 建议您使用 bool 而不是 boolean

and you should consider after wakeup from deep sleep probably your value set back to true并且您应该考虑从深度睡眠中醒来后您的值可能会重新设置为 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM