我使用的是一个arduino uno,一个土壤水分传感器和一个微型水泵,当传感器检测到水分不足时,微型水泵被激活并泵水,直到传感器记录到水分增加(例如在土壤中),这就是它应该如何工作的方式,但是当我从潮湿的土壤水中取出传感器时,我必须再次开始运行代码,这就是我需要帮助的地方,我不希望它终止,需要修改的代码如下
int relayPin = 8;
int sensor_pin = A0; // Soil Sensor input at Analog PIN A0
int output_value ;
void setup() // put your setup code here, to run once:
{
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(sensor_pin, INPUT);
Serial.println("Reading From the Sensor ...");
delay(2000);
}
void loop()
{
output_value= analogRead(sensor_pin);
output_value = map(output_value,550,10,0,100);
Serial.print("Mositure : ");
Serial.print(output_value);
Serial.println("%");
if(output_value<20){
digitalWrite(relayPin, LOW);
}
else
{
digitalWrite(relayPin, HIGH);
}
delay(1000);
}发布于 2022-08-05 21:47:09
在将土壤传感器从地面移除后,当AN0模拟输入产生小于连续条件(< 20)的值时,继电器不会工作。
enum runMode {
DEBUG, // When the ground sensor isn't in the ground
RELEASE // When the ground sensor is in the ground
};
int relayPin = 8;
int sensor_pin = A0;
int output_value;
/* Debug mode has been activated. */
enum runMode mode = DEBUG;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(sensor_pin, INPUT);
Serial.println("Reading From the Sensor ...");
delay(2000);
}
void loop() {
output_value= analogRead(sensor_pin);
output_value = map(output_value,550,10,0,100);
Serial.println("Mositure: " + String(output_value) + "%");
if(mode == DEBUG)
debugMode();
else
releaseMode();
delay(1000);
}
// When debug mode is active, the relay changes position at 1 second intervals
void debugMode() {
if(digitalRead(relayPin) == HIGH) {
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
}
// When the release mode is active, the soil sensor must be in the ground.
void releaseMode() {
if(output_value < 20) {
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
}https://stackoverflow.com/questions/73234132
复制相似问题