I have a charger which powers my arduino. On the charger is an LED which indicates these states:
1 - solid light (powers on)
2 - flashing on and off at 1 second intervals (low battery)
3 - off
When the led lit it always PWM at 1khz. I'm trying to sense this led with an input to my arduino to monitor the charger/battery status periodically without blocking my program. My program uses fast-led and other code which I can't block.
I cannot writea nice code which reliably does this. Below is an example of it flashing (state 2): This is frustrating as I've done harder projects before I'm just mentally blocked.
I would just sample periodically at some close but not equal to 1kHz rate, and look for the last time the signal went HIGH. If the difference between NOW and that time exceeds some interval, it's flashing. If the difference between NOW and that time is greater than one flash cycle time, it's OFF.
The problem is my main loop has some functions that make sampling that fast difficult. Do you suggest a routine with a periodic interrupt? Hopefully there is a simpler solution than this if I am understanding you?
@lastchancename The scale is of signal peak is HIGH (5v) and 0V. Sorry unclear.
Thanks that would work but I was trying to avoid extra hardware as my project is already a bit cramped. Also I'm not quite sure how to design an RC filter that would give me clean enough analog output and I'm stuck having poll the signal still.
Yeah good idea I tried this but it takes so long to detect a second high point. Maybe I should just separately monitor the battery voltage directly from the arduino?
It takes perhaps a few square millimeters if it's SMD, and if it's through-hole/old style components you'll always be able to fit an R and a C piggy-backed to each other somewhere in the space of your case. So this sounds like a bit of a poor excuse really
This sounds like performance problems with your code in general, suggesting that there just may be (I'm putting this very conservatively...) opportunities for overall performance improvements that could make your life easier.
Perhaps it's time to post a schematic and/or picture of the contraption as well as some sketch code so it becomes easier to determine which solution is appropriate. It prevents a lot of back-and forth with solutions and the "yes, but..."'s.
I am experimenting with this in a separate sketch and you're correct my code is a mess but I will clean it up and post it. I implemented an RC filter with 1uF cap and 56k resistor. I had to use a 100k pullup as the internal wasn't enough. This is what I got using analogRead. The signal has been significantly attenuated I guess I would need an op-amp if I wanted?