Go Down

Topic: How to simulate a time delay relay (Read 878 times) previous topic - next topic

nicolaas44

Hello all.

Does any of you know how to implement a time delay function on a Arduino UNO?
I have been using the delay() function in the following statement:
Code: [Select]

if (A0_Value > 522)
{
  delay(500);   
  if (A0_Value > 522)
{
  digitalWrite(LED1, HIGH);
}
}
else if (A0_Value < 522)
{   
  delay(5);
  digitalWrite(LED1, LOW);
}


This code will provide me with a 500 millisecond delay.
But my problem: The value is not monitored during the 500 milliseconds.
If the value drops below 522 within the 500 millisecond, and is back above 522 at the 500 millisecond point, the LED1 will switch on anyway.
It also delays the rest of the code.
I'm basically looking for an old-fashioned time delay relay function.
Sorry for comparing the Arduino with a PLC.

All suggestions are welcome :)
Cheers; nico.

tobyb121

Sorry if I've misunderstood, but you want to turn on an LED if A0 is above 522 for 500ms?

nicolaas44

Yes, you understood correctly.
But I only want it to switch on when A0 stays above 522 during the 500 ms.

tobyb121

The way I would do it is:
Code: [Select]

unsigned long startTime=millis();
char high=1;

while(millis()<startTime+500){
   high &= (analogRead(A0)>522);
}
if(high)
   digitalWrite(outputPin,HIGH);
else
   digitalWrite(outputPin,LOW);

You can also put additional code inside the while loop to avoid blocking the processor

Arrch

Code: [Select]
if (value is below threshold)
  set lastTimeBelowThreshold to now

if (now minus lastTimeBelowThreshold  > some time interval)
  do something

Go Up