2014-12-02 48 views
0

我不能讓我的布爾值工作我不知道我在做什麼錯了。任何人都可以看看代碼並給我一個關於它有什麼問題的提示嗎?我已經測試了不同的寫法,但沒有成功。布爾值唯一的工作時,我把代碼放在void循環。但我不能在那裏使用它。Arduino無法讓我的布爾值工作

#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"); 
    } 
} 

回答

2
if (statusLed1 = true) { 

在書中最古老的疑難雜症。 =是轉讓,==是平等比較。

此外,不要與這樣的布爾值進行比較。

if (statusLed1) { 
0

變化

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

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

if (statusLed1) { 
    Serial.print("on"); 
    } 
    if (!statusLed1){ 
    Serial.print("off"); 
    } 
} 
+0

TNX傢伙。有效。但我需要它得到所需的功能。 – 2014-12-02 16:56:25