off-time detection using pulseIn

I'm working with this code to detect off-time of the pulse, but it is only working for 0.76 seconds and lower time, it is not working for the higher time e.g. 0.95 seconds. Can anyone please help me that how can I do it for higher off-time too. I tried replacing "unsigned long" to "float" data type but still same problem. Also I want the Arduino to generate a pulse when this duration becomes greater than 0.9 sec and no pulse is detected. HElp in this regard will be highly appreciated.

int a = 7;
unsigned long duration;

void setup()
{ Serial.begin(9600);
pinMode(a, INPUT);
}

void loop()
{
duration = pulseIn(a, LOW);
Serial.println(duration);
}

You can change the default timeout, standard = 1000mS. : https://www.arduino.cc/en/Reference/PulseIn

Thank you so much. It worked. can u please help in the second matter too? i.e. how to generate a pulse when a particular duration passes and no external pulse is detected?

pulseIn will return 0 if it times out without detecting a pulse

duration = pulseIn(pin,mode,timeout)
if(duration == 0)
{
//pulse generation code
}

I am using this code now with duration==0, but pulse generation part is not working :frowning:

int a = 7;
int b=13;
float duration;
unsigned long x;

void setup()

{ Serial.begin(9600);

pinMode(a, INPUT);
}

void loop()
{
x = pulseIn(a, LOW, 3000000);

duration=x/1000000.0;

Serial.println(duration);

if(duration == 0)
//pulse generation code

{digitalWrite(b,HIGH);

delay(1000);

digitalWrite(b,LOW);

delay (1000);
}}

I am using this code now with duration==0, but pulse generation part is not working :frowning:

You need to make pin b an OUTPUT.

You also need to learn how to post with code tags so you code appears in a box like this

//You code should be in a window like this
pinMode(b,OUTPUT);

Please read the sticky post at the top of the forum about "How to use the Forum"

Ohh okay okay. The issue is also resolved. Thank you for your help. Actually I m new to any forum and new to programming too therefore there are such mistakes. I'll take care of this next time :slight_smile: Thank you.