项目使用了土壤湿度传感器和超声波传感器。根据水分含量,它将决定是否浇水。同时使用超声波传感器,它将检查水箱的水位。如果水位不足,则无法启动电机。
int val; //stores the value received from Soil moisture sensor.
const int trigPin = 9; //pin for ultra sonic out
const int echoPin = 10; //pin for ultra sonic in
float duration, distance; //stores the values received from ultra sonic sensor.
void setup() {
pinMode(13,OUTPUT); //Set pin 13 as OUTPUT pin, to send signal to relay
pinMode(8,INPUT); //Set pin 8 as input pin, to receive data from Soil moisture sensor.
pinMode(trigPin, OUTPUT); //Set pin 9 as OUTPUT pin, to send signal from ultra sonic sensor
pinMode(echoPin, INPUT); //Set pin 10 as input pin, to receive data from ultra sonic sensor.
Serial.begin(9600); //start serial monitor
}
void loop() {
val = digitalRead(8); //Read data from soil moisture sensor
Serial.print("Moisture: "); //print
Serial.println(val); //print
digitalWrite(trigPin, LOW); //set ultra sonic sensor to low
delayMicroseconds(2); //delay
digitalWrite(trigPin, HIGH); //set ultra sonic sensor to high
delayMicroseconds(10); //delay
digitalWrite(trigPin, LOW); //set ultra sonic sensor to low
duration = pulseIn(echoPin, HIGH); //get the duration
distance = (duration*0.0343)/2; //calculate the distance
Serial.print("Distance: "); //print
Serial.println(distance); //print
if(val == LOW) {
digitalWrite(13,LOW); //if soil moisture sensor provides LOW value send LOW value to relay
} else {
if(distance < 20.00){
digitalWrite(13,LOW); //if distance is lesser than the limit, send LOW value to relay
} else {
digitalWrite(13,HIGH); //if distance is greater than the limit, send HIGH value to relay
}
}
delay(1000); //Wait for few second and then continue the loop.
}
问题是,根据土壤湿度传感器,它能够正常工作。但是超声波传感器的内部if if不起作用。即使计算距离的值总是电机开启而不是水位。如果水土壤湿度传感器相关,水位大于20.00,否则也不起作用。