我无法让布尔值上班,我不知道自己在做什么错。谁能看一下代码并给我提示一下它有什么问题吗?我测试了不同的编写方式,但没有成功。布尔值唯一起作用的时间是我将代码置于void循环之下。但是我不能在那里使用它。

    #include <RemoteReceiver.h>
boolean statusLed1 = false;
void setup() {
  Serial.begin(115200);

  // Initialize receiver on interrupt 0 (= digital pin 2), calls the callback "showCode"
  // after 3 identical codes have been received in a row. (thus, keep the button pressed
  // for a moment)
  //
  // See the interrupt-parameter of attachInterrupt for possible values (and pins)
  // to connect the receiver.
  RemoteReceiver::init(0, 3, showCode);
}

void loop() {
}

// Callback function is called only when a valid code is received.
void showCode(unsigned long receivedCode, unsigned int period) {
  // Note: interrupts are disabled. You can re-enable them if needed.

  // Print the received code.
  Serial.print("Code: ");
  Serial.print(receivedCode);
  Serial.print(", period duration: ");
  Serial.print(period);
  Serial.println("us.");

  if (receivedCode == 353805)
  {

    statusLed1 = true;
  }
  if (receivedCode == 352829)
  {
    statusLed1 = false;

  }
  if (statusLed1 = true) {
    Serial.print("on");
  }
  if (statusLed1 = false){
    Serial.print("off");
  }
}

最佳答案

if (statusLed1 = true) {



书中最古老的陷阱。 =是分配,==是相等比较。

另外,无论如何不要与这样的布尔值进行比较。

if (statusLed1) {

关于c++ - Arduino无法让我的 bool 值工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27242253/

10-12 13:35