下面是一些简单的编译错误代码:
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);
}
最佳答案
在这一行
Particle.publish("endGame", null, 60, PRIVATE);
你使用了一个叫做 null
的东西,而你和标准都没有声明这样的东西。
如果这应该传递一个空指针,使用
Particle.publish("endGame", nullptr, 60, PRIVATE);
或者,如果您使用的是 C++11 之前的版本并且包含了适当的库,
Particle.publish("endGame", NULL, 60, PRIVATE);
请注意,大小写在 C++ 中很重要。
关于c++ - 错误 : 'null' was not declared in this scope when setting integer to 0 - photon/arduino C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34190695/