I'm having trouble with pulseIn ignoring the timeout. I have pin 10 connected to the LED on a mobile phone, and I want the arduino to check that that the LED is flashing, so I know the phone is switched on. When the phone is switched on and has a signal from the network, it flashes the LED for 64ms every 3000ms, so I want to set the pulseIn timeout to be about 5 seconds, to make sure I don't miss a flash from the LED. However it doen't seem to matter what value I use as a timeout, the pulseIn function times out after about 500ms regardless. Even if I leave the timeout value blank, it times out after half a second. From the documentation I thought the default timeout was 1 second?
Even if there's not enough voltage on this LED pin to trigger the pulseIn the function should still wait for the the timeout period to expire, yes? I have tried using a 22k pulldown on this pin to avoid spurious noise on the input, but this has no effect. The timeout is still ignored.
int LED_Pin = 10;
unsigned long LED_Duration;
unsigned long Timeout_Duration = 5000000; //5 seconds
void setup()
{
pinMode(10,INPUT);
Serial.begin(9600);
}
void loop()
{
LED_Duration = pulseIn(LED_Pin, LOW, Timeout_Duration); //even if I leave Timeout_Duration blank, it times out after half a second
Serial.println(LED_Duration); //this outputs a value of 0 every half a second, regardless of the value of Timeout_Duration
}
I hope this makes sense, it's my first post on this forum so let me know if I've made a mistake!
Yes, there's a common ground shared with the phone and the Arduino. I'm using a pull-down resistor from the pulseIn pin to ground. I could probably work around the problem if I use interrupts but I think it makes sense to use a built-in function if I can.
The timeout parameter in the function seems to make no difference to the result, as if it's being ignored. Perhaps there's a mistake with my syntax, but it's a simple function and I can't see it
I would edit the source code for the pulseIn function (in wiring_pulse.c) and change the return type from unsigned long to long. Then, I'd change the value set by the 3 return statements, to return -1, -2, and -3, just to see which condition is causing pulseIn to return.
It appears from looking at the code that the timeout function is not actually implemented (at least not in the beta 1.0 version).
Thanks Paul, for now I am using an interrupt instead, to detect when the LED is flashed.
I haven't done anything to make the code below pretty but I hope these snippets make sense.
Interrupts are useful because now the code doesn't "hang" waiting for the pulse (although with the timeout not working the code wasn't hanging for long enough!) at the expense of some memory space.
volatile unsigned long GSM_LED_Period_Start = 0;
volatile unsigned long GSM_LED_Period_End = 1; //initialised to 1 so that the first "if" statement on the ISR runs first time
volatile long GSM_LED_Period = 0;
void setup()
{
attachInterrupt(0, GSM_LED_Flash, RISING);
//other setup stuff here
}
void loop()
{
//main code here
}
void GSM_LED_Flash()
{
if (GSM_LED_Period_Start < GSM_LED_Period_End)
{
GSM_LED_Period_Start = millis();
}
else if (GSM_LED_Period_End < GSM_LED_Period_Start)
{
GSM_LED_Period_End = millis();
}
GSM_LED_Period = GSM_LED_Period_End - GSM_LED_Period_Start;
GSM_LED_Period = abs(GSM_LED_Period);
}