So.. i'm making a project that involves reading a state of a LED that is attached to a lithium battery (12V 280Ah). This LED turns on for 1 second, then turn off for another second, when i'm charging the battery this LED keeps blinking until the battery reaches full charge, then its fully on. I've already manage to setup a LDR and read the LED state, the arduino knows when its on and when its off.
Making the board understand that the LED is on and take some action is easy, the hard part is the blinking one.I need help to make a code that reads the blinks and do nothing, then reads the LED fully on and activates a relay. I'm not looking for someone to just write the code and done, i need just some directions like wich functions i should use.
I can't access the internals of the battery to hook the board directly into the wires of the LED.
I've tried the LDR in an analog pin, gave me results betwen 40 (LED off) and 730 (LED on), the LDR also worked as digital input returning 0(LED off) and 1(LED on).
Also i'm not posting the code because there's none yet, all the code that i've used in tests was lost.
Why do you want to read the LED status? If you are controlling the LED, you should already have the status information.
Have you study the "Blinking Without delay" sketch from the Examples in the IDE?
Use the LDR to read the state of the LED
When it becomes on save the value of millis() as start time and set a boolean to true
When it is off set the boolean to false
Every time through loop(), if the boolean is true and millis() - start time is greater than the blink period then the LED has remained on
figure out the timing of the charging blink, then in your sketch you read the LDR (Light Dependent Resistor), pause for half a blink cycle, then read again. While charging, the reads should be different. If not, then delay again then read the LDR one more time to make sure the charger is really finished.
The second read is because the LED blink and the Arduino are asynchronous from each other and you could be catching the end of one blink then the start of the next, both reading "on".
If you are doing anything else while the Arduino is waiting for the fully-charged state, then you should use millis() instead of a delay().
In fact, if you are fairly new to Arduino, then using millis() instead of delay() is strongly recommended. Learn to do timing correctly from the start.
" If you are controlling the LED, you should already have the status information." I'm not, as i've said:
" can't access the internals of the battery to hook the board directly into the wires of the LED."
UKHeliBob and SteveMann, good ideas, i'll try that and share the results and yes, there's more stuff for the board to work besides this, using delay was never an option.
Also, picture of the battery, the green led is the one that blinks all the time and when the battery is fully charged it stops blinking to be fully on until the battery starts discharging again.
Here's a variation on UKHeliBobs suggestion of how to time a state change from off to on.
This code needs to run every pass of loop() without any use of delay().
if (digitalRead(LDR) == 0)//sensor reads LOW, LED off
{
LastTimeLEDWasOff = millis();
}
if (millis() - LastTimeLEDWasOff >= 2000) //change this value for the blink period
{
// digitalRead(LDR)== 1 has been true for 2 seconds
//LED is no longer blinking and is ON
}
SteveMann:
That's one heck of a battery. 280AH, 12V. Can I ask what it is being used for?
A friend of mine wants to power a lot stuff in his van, like 12v refrigerator, an inverter to have AC power while away on a trip, and some other stuff. I've manage to build a pretty good system but full analogic, now i want to have more control over things and make some stuff fully automatic.
Guys it worked out fine. Cattledog, when i saw your reply i've already managed to build up a code based on what SteveMann and UKHeliBob replied but thanks, thanks all you guys for the patience and advices.