# How can do something like this: IF HIGH && > 3000ms

I need some help with some delay or timing Code on my receiver.
I need to wait for a 3 second pulse or greater from the xbee 20, if not don’t turn on the LED.

const int xbeePin20 = 7; // pin 7
int led = 12;
int value20;

void setup() {
Serial.begin(9600); // For testing
pinMode(xbeePin20, INPUT);
}

void loop() {
// Read xbeePin20 value on Arduino
value20 = digitalRead(xbeePin20); // Pin 7
Serial.println(value20); // for testing only

if (value20 == HIGH) // something like this: IF HIGH && > 3000ms
{
digitalWrite(led, HIGH); // LED 12
}

}

3000ms since when? Form the start of the pulse?

Look at the technique for timing in the Blink Without Delay example sketch. There is a longer demo in the first post in this Thread.

...R

you can also do something like this

``````void loop() {
// Read xbeePin20 value on Arduino
value20 = digitalRead(xbeePin20); // Pin 7
Serial.println(value20);  // for testing only

while(value20 == HIGH)
{
delay(3000);
digitalWrite(led, HIGH); // LED 12
}
``````

not really neat but does the trick

Yes, you can do it like that, but is better don't use the delay.

Couldn't pulseIn be used? - Scotty

If you want time to enter the equation, you need it to literally
enter the equation...

``````const int xbeePin20 = 7;       // pin 7
int led = 12;

void setup() {
Serial.begin(9600);                // For testing
pinMode(xbeePin20, INPUT);
}

unsigned long went_high = 0L ;
boolean pin20_oldstate = false ;

void loop()
{
boolean value20 = digitalRead(xbeePin20); // Pin 7
if (value20 != pin20_oldstate) // detect changes
{
if (value20)
went_high = millis () ; // capture rising edge timestamp
pin20_oldstate = value20 ;
}

if (digitalRead (xbeePin20) && millis () - went_high > 3000)
digitalWrite(led, HIGH); // LED 12
else
digitalWrite (led, LOW) ; // must turn it off too
}
``````

So you need a variable to record timestamp, and then compare current time
to that timestamp if appropriate.

Note the idiom of detecting a pin state change - use a boolean variable to record
the previous state. Then the timestamp only gets set when the pin transitions HIGH.

The actual test turning on the LED will repeatedly fire, but that's OK because
repeatedly turning an LED on isn't a problem. I've added code to turn it off too,
that's probably needed somewhere!