Can we set output working duration? Pls help

Basically I am doing a motor controlling circuit by using a few sensor(rain and ldr) on the microcontroller. So I wanted my motor to turn for just roughly 3 sec each time the microcontroller receive a input signal and close until a new input is received. I tried using delay() it is useful when setting the motor functioning duration but the outcome is not that efficient since the close duration is also fixed and new input during the duration is undetectable. So is there another way? Or is it possible?

Turn on the motor and start a millis() timer set for 3 seconds. Watch the timer and check the inputs every time through loop(). If the time runs out, turn off the motor. If there is an active input, reset the timer to 3 seconds.

Some millis() timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Hi, @kinchung
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

We need to see what code you have been working on, so some solutions can be suggested.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Ok thanks guys I am actually new to this so it's kind of confusing for me and that's y I seek for help in the forum

Read through the tutorials and give it a try. We are here if you get stuck, but do try it for yourself.

If you get stuck, post your code. Describe what the code actually does and how that differs from what you want.

Post code as described in the how to get the best of this forum post. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

#define rainfall 4rain sensor
#define ldrPin 3ldr sensor
#define relay1Pin 6this is the first relay
#define relay2Pin 8this is for the second relay

Void setup(){
Serial.begin(9600);

pinMode(rainfall,INPUT);
pinMode(ldrPin,INPUT);
pinMode(relay1Pin,OUTPUT);
pinMode(relay2Pin,OUTPUT);
}

Void loop(){

int sensorValue;
sensorValue = digitalRead(ldrPin);

int rainValue;
rainValue = digitalRead(rainfall);

int relay1;
relay1 = digitalRead(relay1Pin);

int relay2;
relay2 = digitalRead(relay2Pin);

If(digitalRead(rainValue) == HIGH);{

digitalWrite (relay1,HIGH);
}

else if(digitalRead(rainValue) == LOW && digitalRead(sensorValue) == LOW);{

digitalWrite(relay1,HIGH);
}

else if(digitalRead(rainValue) == LOW && digitalRead(sensorValue) == HIGH);{

digitalWrite(relay2,HIGH);
digitalWrite(relay1,LOW);
}
}Preformatted text

So this is my coding u guys can check if u guys wanted. I will try to do the rest by myself first.

I see no timer in that improperly posted code.

Put the code in code tags by selecting the code and click the </> (shown in red box).
code tags new

The code will look like this when posted in code tags and formatted.

#define rainfall 4rain sensor
#define ldrPin 3ldr sensor
#define relay1Pin 6this is the first relay
#define relay2Pin 8this is for the second relay

Void setup()
{
   Serial.begin(9600);

   pinMode(rainfall, INPUT);
   pinMode(ldrPin, INPUT);
   pinMode(relay1Pin, OUTPUT);
   pinMode(relay2Pin, OUTPUT);
}

Void loop()
{
   int sensorValue;
   sensorValue = digitalRead(ldrPin);

   int rainValue;
   rainValue = digitalRead(rainfall);

   int relay1;
   relay1 = digitalRead(relay1Pin);

   int relay2;
   relay2 = digitalRead(relay2Pin);

   If(digitalRead(rainValue) == HIGH);
   {

      digitalWrite (relay1, HIGH);
   }

   else if (digitalRead(rainValue) == LOW && digitalRead(sensorValue) == LOW);
   {

      digitalWrite(relay1, HIGH);
   }

   else if (digitalRead(rainValue) == LOW && digitalRead(sensorValue) == HIGH);
   {

      digitalWrite(relay2, HIGH);
      digitalWrite(relay1, LOW);
   }
} 

You should put in some comment to say what the program is doing (turn on motor, turn off motor, it's raining, ...).

Ou ok thanks wait for a while


#define rainfall 4
#define ldrPin 3
#define relay1Pin 6
#define relay2Pin 8

Void setup()
{
   Serial.begin(9600);

   pinMode(rainfall, INPUT);
   pinMode(ldrPin, INPUT);
   pinMode(relay1Pin, OUTPUT);
   pinMode(relay2Pin, OUTPUT);
}

Void loop()
{
   int sensorValue;
   sensorValue = digitalRead(ldrPin);

   int rainValue;
   rainValue = digitalRead(rainfall);

   int relay1;
   relay1 = digitalRead(relay1Pin);

   int relay2;
   relay2 = digitalRead(relay2Pin);

   If(digitalRead(rainValue) == HIGH);
   {

      digitalWrite (relay1, HIGH);
   }

   else if (digitalRead(rainValue) == LOW && digitalRead(sensorValue) == LOW);
   {

      digitalWrite(relay1, HIGH);
   }

   else if (digitalRead(rainValue) == LOW && digitalRead(sensorValue) == HIGH);
   {

      digitalWrite(relay2, HIGH);
      digitalWrite(relay1, LOW);
      delay(3000);
      digitalWrite(relay2,LOW);
   }
} 

I only coded delay in one outcome because I am trying whether it can works or not

Does the code do what you want?

Yeah the relay can be on and off exactly how I wanted but the delay() is not useful in my situation.

Then replace the delay with a millis() timer. Using the millis() non-blocking timer you can watch the states of the inputs all of the time.

Ok thanks still figuring out how the millis timer work

It is just like if you are cooking an cake. You put the cake in the oven, set a timer and go to do other things. Every once in a while you look at the timer and check the cake. If the cake is not done, put the time back on the timer. If the timer runs out, the cake is done, remove the cake from the oven.

This explaination is good and easily understanded. Currently, I am almost done with the concept but the coding is quite complex. Still need some time in it.

#define rainfall 4
#define ldrPin 3
#define relay1Pin 6
#define relay2Pin 8
const unsigned long sensorReading = 3000;
unsigned long previousTime=0;

Void setup(){
Serial.begin(9600);

pinMode(rainfall,INPUT);
pinMode(ldrPin,INPUT);
pinMode(relay1Pin,OUTPUT);
pinMode(relay2Pin,OUTPUT);
}

Void loop(){

int sensorValue;
sensorValue = digitalRead(ldrPin);

int rainValue;
rainValue = digitalRead(rainfall);

int relay1;
relay1 = digitalRead(relay1Pin);

int relay2;
relay2 = digitalRead(relay2Pin);

unsigned long current time=millis();
Serial.println(millis());

If(currentTime-previousTime >= sensorReading){

If(digitalRead(rainValue) == HIGH){

  If(digitalRead(relay1)== LOW)
  {
  digitalWrite(relay1,HIGH);
  digitalWrite(relay2,LOW);

  }else if (digitalRead(relay1) == HIGH)
  {
  digitalWrite(relay1,LOW);
  digitalWrite(relay2,LOW);

}else if(digitalRead(rainValue) == LOW && digitalRead(sensorValue) == LOW){
  
  if(digitalRead(relay1)==LOW)
  {
   digitalWrite(relay1,HIGH);
   digitalWrite(relay2,LOW);

  }else if(digitalRead(relay1)==HIGH)
 {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  }
}else if(digitalRead(rainValue) == LOW && digitalRead(sensorValue) == HIGH);{

  if (digitalRead(relay2)==LOW)
  {
   digitalWrite(relay2,HIGH);
   digitalWrite(relay1,LOW);

  }else if(digitalRead(relay2)==HIGH)
   {
    digitalWrite(relay2,LOW);
    digitalWrite(relay1,LOW);
   }
}
previousTime=currentTime;
}

This is my latest code but the thing is still not working. I tried to put millis() after the output but the complier shows too many arguments to function 'long unsigned int millis()'. What have I done wrong? Any suggestion?

Hi,
Sorry, that is not a complete code.
Brackets missing.

Tom... :smiley: :+1: :coffee: :australia: