Hi, can I ask about about my project ? My project is about automated clothesline. When the rain sensor detects rain/water, the dc motor run clockwise while when there's no presence of water/rain, the dc motor will run anticlockwise. The problem is the motor wont stop running. Do I have to change a little bit in the coding or do I must use limit switch to stop it ? Thank youuuuuu
int rainPin = A0;
int greenLED = 5;
int redLED = 6;
int CW = 7;
int CCW = 8;
int thresholdValue = 800;
void setup(){
pinMode(rainPin, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
Serial.begin(9600);
pinMode(CW, OUTPUT);
pinMode(CCW, OUTPUT);
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(rainPin);
Serial.print(sensorValue);
if(sensorValue < thresholdValue){
Serial.println(" - it is raining!!!");
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(CW,HIGH); //Motor runs clockwise//
delay(500);
digitalWrite(CW, LOW); //Motor stops//
}
else {
Serial.println(" - it is sunny ^_^");
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
delay(500);
digitalWrite(CCW, LOW);
}
}
You need something to stop the power to the motor, a relay and or a switch should fit your needs. Hope this helps.
Edit - I should ask, is this a basic DC motor with only a Voltage and ground connection?
Thank you for answering. Actually I'm using 2channel relay. If I use relay, do I just need to change the coding or I must use limit switch ?
You need to set a variable as a flag to indicate what the previous direction the motor had turned. Lets say if the flag is set to 0, meaning raining, the motor had turned clockwise. As the loop runs and sees that it is still raining. It wants to turn the motor back on for clockwise but before doing that the loop checks the flag. If it is at 0 then nothing will happen. The loop keeps running, checking the rain thingy. Eventually the sun comes out and now the loop wants to turn the motor on counter clockwise. It checks the flag and sees the last movement was clockwise, now it is ok to turn the motor on counter clockwise for one half second (very short clothesline?). Right after the command to start the motor for sunny day, the flag is set to 1, it's a bright, sun-shiny day. The motor runs for half a second, stops, and the program now continues looping, doing nothing with the motor until the rain starts.
Without the flag the loop keeps turning the motor back on several microseconds after it was turned off because all it is doing is checking the sensor and operating the motor.
Due_unto:
You need to set a variable as a flag to indicate what the previous direction the motor had turned. Lets say if the flag is set to 0, meaning raining, the motor had turned clockwise. As the loop runs and sees that it is still raining. It wants to turn the motor back on for clockwise but before doing that the loop checks the flag. If it is at 0 then nothing will happen. The loop keeps running, checking the rain thingy. Eventually the sun comes out and now the loop wants to turn the motor on counter clockwise. It checks the flag and sees the last movement was clockwise, now it is ok to turn the motor on counter clockwise for one half second (very short clothesline?). Right after the command to start the motor for sunny day, the flag is set to 1, it's a bright, sun-shiny day. The motor runs for half a second, stops, and the program now continues looping, doing nothing with the motor until the rain starts.
Without the flag the loop keeps turning the motor back on several microseconds after it was turned off because all it is doing is checking the sensor and operating the motor.
Can you give example how to do flag? I'm actually a beginner. I'm trying to do this for my school project
Without positional feedback the software flag method will have a 50% chance of starting up in the wrong position when first turned on or recovering from a power failure. You need to add some type of sensor that can reliably determine the status of the line during startup. Bear in mind that the sensor has to take into account mechanical inertia so that the mark on the line does not run past the sensor as the motor slows to a stop.
Once you have a sensor in place that gives good feedback then you can consider using that sensor as a limit switch or as an input to the MCU to set a status flag.