Hi everyone,
I am new to Arduino and am piecing together a rain sensor and a motor to help me close my window when the rain starts, and open the window when the rain sensor dries up. The hardware is set up to spin the motor in both directions and pick up signal from the sensor. But as I tested my sketch, it repeats the closing act as long as the board is wet. But the window just needs to be closed once, leaving the only subsequent action to be the opening of the window. Does anyone know how to fix the code I have below? Thank you!
//pin 8 = direction
//pin 9 = enable
const int floodSensors = 2; // the number of the Flood Sensor pin
const int motorDirection = 8; // the number of the Motor Direction pin
const int motorEnable = 9; // the number of the Motor Enable pin
int floodSensorState = 0; // variable for reading the floodSensors status
int windowClosed = false;
void setup() {
pinMode(floodSensors, INPUT); // initialize the flood Sensor pin as an input:
pinMode(motorDirection, OUTPUT); //set direction pin as output
pinMode(motorEnable, OUTPUT); //set enable pin as output
}
void loop()
{
floodSensorState = digitalRead(floodSensors); // read the state of the flood Sensor value:
if (windowClosed == false)
{
closeWindow();
}
if (windowClosed == true)
{
openWindow();
}
}
void closeWindow()
{
if (floodSensorState == HIGH)
{
//start off going forward at 50% throttle
digitalWrite(8, HIGH); //forward
analogWrite(9,128); //50% PWM
delay(5000);
digitalWrite(9, HIGH); //full speed
delay(10000);
digitalWrite(9, LOW); //turn enable pin off
delay(5000);
windowClosed == true;
}
}
void openWindow()
{
if(floodSensorState == LOW)
{
digitalWrite(8, LOW); //backward
analogWrite(9,128); //50% PWM
delay(5000);
digitalWrite(9, HIGH); //full speed
delay(10000);
digitalWrite(9, LOW); //turn enable pin off
delay(5000);
windowClosed == false;
}
}
Breath_Rain_Sensing_Motor_V2.ino (1.38 KB)