2015-12-09 24 views
1

我有低於編譯錯誤與一些簡單的代碼:錯誤:「空」設置整數時,0不是在這個範圍內聲明 - 光子/ Arduino的C++

scorer.cpp: In function 'void loop()': 
scorer.cpp:56:37: error: 'null' was not declared in this scope 
     feeler2ConsecutivePresses = 0; 

我試圖將其設置爲NULL而不是0,幷包括標準庫。我在想什麼(C++初學者在這裏)

int feeler1 = D0; 
int feeler2 = D2; 
int batteryLowIndicator = D4; 
int pingFrequency = 5000; 
unsigned long lastPing = millis(); 
bool batteryLow = false; 
bool sentOnlineMessage = false; 

int feeler1PreviousState; 
int feeler2PreviousState; 
int batteryLevelPreviousState; 

int feeler1ConsecutivePresses; 
int feeler2ConsecutivePresses; 
int consecutivePressThreshold = 30; // 3 seconds 

void setup() { 
    pinMode(feeler1, INPUT_PULLDOWN); 
    pinMode(feeler2, INPUT_PULLDOWN); 
    pinMode(batteryLowIndicator, INPUT_PULLUP); 
} 

void loop() { 

    int feeler1Pressed = digitalRead(feeler1); 
    int feeler2Pressed = digitalRead(feeler2); 
    int batteryLevel = digitalRead(batteryLowIndicator); 

    if (Particle.connected() && !sentOnlineMessage) { 
     sentOnlineMessage = true; 
     Particle.publish("online", "1", 60, PRIVATE); 
    } 

    if (feeler1Pressed == HIGH) { 
     if (feeler1PreviousState == HIGH) { 
      feeler1ConsecutivePresses += 1; 
     } else { 
      Particle.publish("scored", "1", 60, PRIVATE); 
     } 
    } else { 
     feeler1ConsecutivePresses = 0; 
    } 

    if (feeler2Pressed == HIGH) { 
     if (feeler2PreviousState == HIGH) { 
      feeler2ConsecutivePresses += 1; 
     } else { 
      Particle.publish("scored", "2", 60, PRIVATE); 
     } 
    } else { 
     feeler2ConsecutivePresses = 0; 
    } 

    if (feeler1ConsecutivePresses == consecutivePressThreshold 
      && feeler2ConsecutivePresses == consecutivePressThreshold) { 
     Particle.publish("endGame", null, 60, PRIVATE); 
    } 

    if (batteryLevel == LOW && batteryLevel != batteryLevelPreviousState) { 
     Particle.publish("batteryLow", "1", 60, PRIVATE); 
    } 

    // Ping server every x seconds 
    if (millis() - lastPing > pingFrequency) { 
     Particle.publish("ping", "1", 60, PRIVATE); 
     lastPing = millis(); 
    } 

    feeler1PreviousState = feeler1Pressed; 
    feeler2PreviousState = feeler2Pressed; 
    batteryLevelPreviousState = batteryLevel; 

    delay(100); 
} 
+0

'Particle.publish(「endGame」,null,60,PRIVATE);' –

+0

非常有用,謝謝 – timelf123

回答

0

在這一行

Particle.publish("endGame", null, 60, PRIVATE); 
你用一種叫做 null

,並且無論是你還是標準的聲明這樣的事情。

如果這是應該傳遞一個空指針,使用

Particle.publish("endGame", nullptr, 60, PRIVATE); 

或者,如果你是預C++ 11,其中包括一個合適的庫,

Particle.publish("endGame", NULL, 60, PRIVATE); 

注意大小寫在C++中很重要。

+0

非常有幫助,感謝您的解釋。當5分鐘結束時將標記爲解決 – timelf123

相關問題