I try to build an automatic roof with rain sensor input, when it's rain the roof then closed pulled by DC motor on the tracks and activate a buzzer. When it reach the end (limit switch pressed) then the motor stopped and buzzer off. And when the rain stopped, motor will turning again to open the roof until the second limit switch pressed to stop the DC motor.
I have try to make my own program but it have an error, when it's has no rain detected the motor spin to close the roof and stop when limit switch pressed (LOW logic), but when rain detected the buzzer turned on but the motor doesn't spin at all. Here is my code, I'm using L293D for motor driver and using its enable pin.
void setup() {
digitalWrite(enable_motor, HIGH);
digitalWrite(motor_open, LOW);
digitalWrite(motor_close, LOW);
digitalWrite(alarm, LOW);
}
void loop() {
if (analogRead(rainSensor) < 650){
digitalWrite(alarm, HIGH);
digitalWrite(motor_close, HIGH);
digitalWrite(motor_open, LOW);
}
else if (digitalRead(stop_close) == LOW){
digitalWrite(alarm, LOW);
digitalWrite(enable_motor, LOW);
}
else if (analogRead(rainSensor) > 850){
digitalWrite(alarm, LOW);
digitalWrite(motor_close, LOW);
digitalWrite(motor_open, HIGH);
}
else if (digitalRead(stop_open) == LOW){
digitalWrite(alarm, LOW);
digitalWrite(enable_motor, LOW);
}
else {
digitalWrite(enable_motor, HIGH);
digitalWrite(alarm, LOW);
}
}
Can I use If-Else logic? Or I make a mistake?