Raindrop Sensor with ardunio pro mini

Hi All,

(So sorry for my English not so good)

I'm a new about Arduino, I need your help with my code as below.

My explanation about the function of my system

With Arduino Pro Mini by Deek Robot

I'm using with the remote control by other together with raindrop sensor for control motor.

  • 2 signal from remote control to do forward and reverse of motor, relay output is operated accordingly
  • When raindrop sensor detected, only forward relay take activity, reverse relay will be cancel

Raindrop sensor output is attached by interrupts function.

My problem that when motor is running by remote control if raindrop is detected, motor is running as required, it is correct. But the system is ready standby, if raindrop is detected at this time, motor did not run smoothly, output relay have taken ON then OFF in short time then ON again, look like blink.

Here is my code, kindly take your time to help me with my best regards:

int limitted = 5; //limitted switch
int remoteFo = 9; //input for Forward from remote control board
int remoteRe = 7; //input for Reverse from remote control board
int FoPin = 10; //output for Forward relay
int RePin = 11; //output for Reverse relay
int CoPin = 12; //output for magetic brake

void setup()
{

pinMode(FoPin, OUTPUT);
pinMode(RePin, OUTPUT);
pinMode(CoPin, OUTPUT);
pinMode(remoteFo, INPUT_PULLUP);
pinMode(remoteRe, INPUT_PULLUP);
pinMode(limitted, INPUT_PULLUP);
attachInterrupt (0, Rain, LOW);
}
void Rain()
{
int limitedFo = digitalRead(limitted);
delay(200);
digitalWrite(RePin, HIGH);
delay(500);
if (limitedFo == LOW) {
digitalWrite(CoPin, LOW);
delay(500);
digitalWrite(FoPin, LOW);
}
else {
digitalWrite(FoPin, HIGH);
delay(500);
digitalWrite(CoPin, HIGH);
}
}

void loop()
{
int stateFo = digitalRead(remoteFo);
if (stateFo == LOW) {
digitalWrite(CoPin, LOW);
delay(100);
digitalWrite(FoPin, LOW);
}
else {
digitalWrite(FoPin, HIGH);
digitalWrite(CoPin, HIGH);

}

int stateRe = digitalRead(remoteRe);
if (stateRe == LOW)
{
digitalWrite(CoPin, LOW);
delay(100);
digitalWrite(RePin, LOW);

}
else {
digitalWrite(RePin, HIGH);
digitalWrite(CoPin, HIGH);

}
}

You have used delay() statements in an interrupt service routing, in this case Rain().
This will cause all sorts of unpredictable behaviour. An interrupt service routine must run very quickly because it, among other things, blocks everything else.

Usually, you set flags in the interrupt service routine, for example a flag that rain has been detected, and this flag is acted on by code in the loop().

Thanks for your replied, i will delete and try again as your commented, i will update again

Thanks 6v6gt so much, my code is done after i deleted delay() statement in interrupt routing