if statement , making a pin high for certain amount of time

hello

I need a code for if statement that makes the pin hight for a certain amount of time if the condition has been met

for example

if(x>400 )
 {
  digitalWrite(pin, HIGH);// i need this to be high for t amount of time 

 }else{
  digitalWrite(pin, LOW);
 }

thanks

Start by detecting when x becomes greater than 400 rather than when it is greater than 400. See the StateChangeDetection example in the IDE

When x becomes greater than 400 save the value of millis() as the start time. Then, each time through loop() compare the current value of millis() with the start time and if the required period has elapsed take whatever action you need

thanks for your reply

in my code 400 is a sensor reading, I can't work out how to change the bottom example to the analog reading of a sensor

Something like this (untested)

void loop()
{
  previousValue = currentValue;  //save the previous value
  currentValue = analogRead(sensorPin);
  if (currentValue > 400 && previousValue <=400 && !timing)  //the value has become greater than 400
  {
    timing = true;
    startTime = millis();
    digitalWrite(pin, HIGH);
  } 

  if (timing && millis() - start time >= period)  //time's up
  {
    timing = false;
    digitalWrite(pin, LOW);  
  }
}

NOTE : there is work to do on the code such as declaring variables

thanks

I will try to work on it

UKHeliBob:
Start by detecting when x becomes greater than 400 rather than when it is greater than 400. See the StateChangeDetection example in the IDE

When x becomes greater than 400 save the value of millis() as the start time. Then, each time through loop() compare the current value of millis() with the start time and if the required period has elapsed take whatever action you need

why should I do "Start by detecting when x becomes greater than 400 rather than when it is greater than 400." ?

in another word
what would be a problem if i do like this ?

 s_currentValue = analogRead(A0);
        if (s_currentValue > 400 && !timing) 
        {
          timing = true;
          startTime = millis();
          digitalWrite(pump, LOW);
        }
        if (timing && millis() - startTime >= period)  //time's up
        {
          timing = false;
          digitalWrite(pump, HIGH);
        }

moeinipharmd:
why should I do “Start by detecting when x becomes greater than 400 rather than when it is greater than 400.” ?

Because you said:

“I need a code for if statement that makes the pin hight for a certain amount of time if the condition has been met”

larryd:
Because you said:

"I need a code for if statement that makes the pin hight for a certain amount of time if the condition has been met"

I think it is because you don't want to stuck with a relay that keeps going open and close continuously because it wants to maintain exactly lower or higher than the threshold

am I right ?

am I right ?

That depends on your application and in the case of your original post you were quite clear

I need a code for if statement that makes the pin hight for a certain amount of time if the condition has been met

If you were to detect when the condition is currently true rather when it became true that would not meet your requirement

UKHeliBob:
That depends on your application and in the case of your original post you were quite clearIf you were to detect when the condition is currently true rather when it became true that would not meet your requirement

it was just a case of bad english on my part :))

it was just a case of bad english on my part

So what is it that you really want to do ?