Reading LED Blink Status

Ok so I've been trying to work this out for a while now, and forum/Google searched haven't gotten me far...

I've got an LED (input) that I need to read the status of. It has three states:

  • on
  • off
  • blink

On and off are obviously easy, but when the LED is blinking I need to do something different in my code. This LED in particular is on about a 2Hz cylce (on for 1 sec, off for 1 sec). I have another project which needs a similar bit of code so this should be able to work for both with a bit of modification.

Hardware-wise, I'll be using the LED pins to drive an optocoupler which will in turn connect to an input pin on the Arduino.

Does anyone have any tips on how to do this?

Thanks! :)

Hi Pilbromatic

2Hz cylce (on for 1 sec, off for 1 sec)

That is a 0.5Hz cycle - one complete cycle every 2s ;)

One way to detect the state would be to measure the "on" and "off" times, a bit like software debouncing a very slowly changing switch contact.

  1. In loop(), check the input to see if it has changed from low to high or from high to low. When it changes, store the current millis() time in a variable and change state to "blink".

  2. Also in loop(), if the current state is "blink", check to see if the new current time minus that saved value is greater than 1.2s for example (i.e. longer than the blinking on and off times). If it is greater, the LED must have stopped in either on or off condition, so change state to "on" or "off" depending on the current input level.

If the LED changes before the 1.2s has expired, then the first bit of code above will restart the timer and keep the state as "blink".

This simple method does mean that the state will always change by going through "blink", for example "on" -> "blink" -> "off" -> "blink" -> "on". Would that be a problem for your application?

Regards

Ray

What is making the LED blink?

If the Arduino is making it blink then you know the state it is in because you put it in that state.

...R

Hackscribble: That is a 0.5Hz cycle - one complete cycle every 2s ;)

Haha whoops! I knew I was tired last night but that's just plain terrible! :P

Thanks for your help so far. It will need to show up as three different states; eg blink will have to be a solid output that doesn't change state until the output has definitely gone fixed on or off. I was actually playing around with debouncing code the other day but I kinda got nowhere, lol.

And Robin2, no unfortunately it's an external device that I need to monitor. :)

Cheers. :)

Pilbromatic: It will need to show up as three different states; eg blink will have to be a solid output that doesn't change state until the output has definitely gone fixed on or off.

Then your output will always have to operate sufficiently in arrears to be sure that it is not just detecting one of the phases of a blink.

When it goes off, record millis() in (say) lastChangeMillis When it goes on ditto - in the same variable

At any time if millis() - lastChangeMills > 2500 it means you are in a steady state (say, ON)

Then if that test subsequently fails it means you are in transition but you can't tell if you are in an OFF state or a blink state until another 2500msecs has passed (using a different time variable) - say transitionMillis. If the transitionMillis has passed AND the lastChangeMillis test continues to fail you are in a BLINK state.

...R

Thanks Robin, I'll have a play with that and see what I can do. It's not time critical so a few seconds for it to work out if a state is changing is perfectly acceptable for this application. Thanks!