如果压力值在一个循环中变化超过一定量,我有此代码可以打开继电器。继电器只有在变为负极时才会继续。这段代码非常粗糙,请原谅我,但有些事情困扰我。有一个是只能满足一次的声明,但即使不满足值,它的内容仍会执行。
检查一下,特别是ldrStartk
。
#include <SFE_BMP180.h>
#include <Wire.h>
SFE_BMP180 pressure;
#define ALTITUDE 1667.0
int ldrValue = 0;
int ldrStart = 0;
bool lstate = 0;
int counter = 0;
int sen = 1;
int ddelay = 30;
static int boolS = 0;
int ldrStartk;
void setup() {
boolS = 1;
Serial.begin(9600);
Serial.println("REBOOT");
// Initialize the sensor (it is important to get calibration values stored on the device).
if (pressure.begin())
Serial.println("BMP180 init success");
else {
// Oops, something went wrong, this is usually a connection problem,
// see the comments at the top of this sketch for the proper connections.
Serial.println("BMP180 init fail\n\n");
while (1); // Pause forever.
}
pinMode(4, OUTPUT);
}
void loop() {
char status;
double T, P, p0, a;
status = pressure.startPressure(3);
if (status != 0) {
// Wait for the measurement to complete:
delay(status);
status = pressure.getPressure(P, T);
if (status != 0) {
// Print out the measurement:
Serial.print("absolute pressure HPA : ");
Serial.print(P * 10);
Serial.print(" hpa, ");
ldrValue = (P * 10);
Serial.print("ldrValue");
Serial.print(ldrValue);
Serial.print(" boolS:");
Serial.print(boolS);
if (boolS = 1) {
ldrStartk = ldrValue ;
Serial.print("ldrStartk:");
Serial.print(ldrStartk);
delay(200);
}
if (ldrValue < ldrStart - sen && counter < ddelay && ldrValue <= ldrStartk + sen) {
boolS = 2;
digitalWrite(4, HIGH);
lstate = 1;
if (lstate = 1)
counter = counter + 1;
if (counter >= ddelay) {
digitalWrite(4, LOW);
ldrStart = (ldrValue);
}
}
if (ldrValue >= ldrStart - sen) {
digitalWrite(4, LOW);
lstate = 0;
counter = 0;
ldrStart = (ldrValue);
}
Serial.print(" counter ");
Serial.print(counter);
Serial.print(" ldrStart ");
Serial.println(ldrStart);
delay(200);
boolS = 2;
} else Serial.println("error retrieving pressure measurement\n");
} else Serial.println("error starting pressure measurement\n");
}
这是监视器:
absolute pressure HPA : 7797.34 hpa, ldrValue7797 boolS:2ldrStartk:7797 counter 0 ldrStart 7797
absolute pressure HPA : 7797.07 hpa, ldrValue7797 boolS:2ldrStartk:7797 counter 0 ldrStart 7797
absolute pressure HPA : 7796.76 hpa, ldrValue7796 boolS:2ldrStartk:7796 counter 0 ldrStart 7796
absolute pressure HPA : 7797.17 hpa, ldrValue7797 boolS:2ldrStartk:7797 counter 0 ldrStart 7797
absolute pressure HPA : 7797.24 hpa, ldrValue7797 boolS:2ldrStartk:7797 counter 0 ldrStart 7797
即使未达到if
,也请查看其变化情况。
答案 0 :(得分:0)
第72行有错误的代码行。
你正在分配而不是比较。
更改
if (boolS = 1)
到
if (boolS == 1)
同样的情况在第61行:
if (lstate = 1)
应该是
if (lstate == 1)