How to control Rain Sensor and 2 DC Gear Motors?

How to control Rain Sensor and DC Gear Motors? I wanted to rotate clockwise the 2 dc gear motors if the rain sensor is wet and have a duration on it and wanted to rotate counterclockwise the 2 dc gear motors if the rain sensor is dry and also have a duration on it. How can I control it? How to stop and resume it? Please HELP me. I really need your help. I`m a beginner.

What have you tried?

What rain sensor? What drivers for the motors? The more we know about your project the more help we can give.

Provide a schematic and the code for your best effort and we can help.

Read the how to use this forum-please read stickies to see how to post code and some advice on asking questions.

My problem is how can I put a duration/time or stop and resume to my DC Gear motors and the rain sensor? I wanted that if the rain sensor is WET the DC gear motors will rotate clockwise then put a duration on it like (2 or 3 seconds) then stop. Until/And if the Rain sensor is DRY the DC Gear motors will rotates counterclockwise. Help me please :slightly_frowning_face:

I used this ff:

Rain Sensor
2 DC Gear Motor
2 Lithium Battery 18650
Driver Motor L298N
Breadboard
Wires

This is my code.

//rain Sensor
const int capteur_D = 4;
const int capteur_A = A0;

int val_analogique;

// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(capteur_D, INPUT);
pinMode(capteur_A, INPUT);
Serial.begin(9600);

}

void loop() {
if(digitalRead(capteur_D) == LOW)
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

else
{
// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}
}

1 Like

I already get the thing that if the rain sensor is WET the DC Gear motors will rotates clockwise and if the rain sensor is DRY the DC Gear motors will rotates counterclockwise. My problem now is if the Rain Sensor is WET or DRY it just continue in rotating. how can I put a duration or stop the DC motors after the duration runs out? Please Help.

1 Like

What does the posted code actually do? How does that differ from what you want?

Using the delay() function will make the program unresponsive. Better to use millis() or micros() for timing.
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

It may help if you try to learn how to use a state machine. Google "arduino state machine" for more pages.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

You need to look at the StateChangeDetection example in the IDE because I don't think you're describing your requirements properly.

What you actually want is when the detector first becomes wet (i.e. changes from dry to wet) you start the motors clockwise then stop them a few seconds later. And then later on when the detector changes from wet back to dry you run the motors counterclockwise for a few seconds. That's very different from what you're currently doing.

Steve

You want to run the motors only when the condition changes from dry to wet or wet to dry (transition) not when the conditions are wet or dry (state)? So the transitions from one to the other is what is important, not the state. The state change detection example shows how to detect the transitions by keeping track of the last state and only responding on the changes.

And perhaps learn about state machines, that provides a clear framework for programming a behaviour pattern.