LDR as LED pulse counter - stuck LED issues

Hi everyone
I am using a small board with LDR between two digital pins to detect when LED pulse is flashed through the power meter.
All works fine, but sometimes LED on the meter is not flashing but gets stuck in the ON position (mainly during the day when solar power is flowing the other way).
I am using pinMode(D2,"input_pullup") and detect the pulse on the falling edge.
But when LED gets stuck, it causes the whole board to hang until I either reset it or hide LDR from the light source.
I have tried changing pinMode to just pinMode(D2,"input") when the stuck LED is detected.
But as I said - for whatever reason sometimes it is just hangs the whole board.
Also, the board is battery powered and this stuck LED causes high battery drain

Looking for suggestions on how I can overcome it

Post your schematic and sketch well formated and in code tags "</>" as marked in this editor.

this is what I am doing with input_pullup and input. Maybe I should use input_pulldown instead of input for the time it it stuck... Still a bit confused about the modes

I have not posted it as its not written in pure Arduino code but in JS. I am just hoping to get some ideas on how I can overcome it, so that I can put it in the JS code =)
I am still a novice, just working on my first "real" project =)

  var onCount = 0;
  var pulledUp = true;
  setInterval(function() { // every 10 seconds
    if (!pulledUp) pinMode(D2,"input_pullup");
    if (D2.read()) { // LED is off - all ok
      onCount = 0;
      pulledUp = true;
      print("Power meter LED is not stuck");
    } else {
    if (onCount>6) { // >60 secs on
      pulledUp = false;
      print("Power meter LED is STUCK in the ON position for "+onCount*10+" seconds");
  if (!pulledUp) pinMode(D2,"input");
}, 10000);

  setWatch(function(e) {
   if (!pulledUp) return;
  }, D2, { repeat:true, edge:"falling" });

Did you have a schematic of your JS Project. didn´t you?

its just LDR between digital pins D1 and D2. Nothing else

Do you know how a LDR operates ?

I’d guess the LED can be stuck on or off… pull down isn’t going to help either.

You need to detect the positive rising edge of the LED, count it, then exit until the next ‘edge’.

Don’t wallow in the high state or the low state, you’re only interested in the rising edge.

All the other tine your code should be non blocking, so it can do whatever it wants.

If the signal is clean enough, and you want to learn a bit more, you can study interrupts, and maybe use a phototransistor instead of the LDR.

Just a note, like the LDR, you need to watch the input, because it may be inverted with respect to the LED state. LED on = low state on the input.

its a power company provided meterbox. My guess is that my solar panels pushing excess power to the grid which causes it to be on all the time. But its not doing it all the time...

I am detecting falling edge to execute main update function (see code above).
It is not doing anything in between, but my problem is that LDR resistance is low all the time, when LED on power meter is tuck in the ON state. Its like the button is always pressed.
I want to be able to force reset the "short" connection on timer

I am using D1 as ground and D2 as high. When LED is on, resistance on LDR is low and D2 gets a reading. But with stuck LED, D2 keeps getting a reading (like a switch that is always closed) and for whatever reason causing the board to eventually hang (plus drains the battery)
I was trying to somehow reset this "short". Maybe if I use input_pulldown instead of "input" in the last part of the stuck LED check in the code, it will make D2 a 0 until the next loop?

If there’s any light leakage, the LDR may never ‘go high’…
It could be worth checking the spectral response of the LDR to ensure it can reliably’see’ the LED.

It may be time for a schematic of your LDR arrangement with component values.

I have mentioned it above - there is just LDR between digital pins D1 and D2 and nothing else.
It can "see" LED reliably - code works fine for days, as long as LED on the power meter is not stuck in the always on position for too long

Have a nice day and enjoy coding.

the device is puck.js from https://www.espruino.com/

LDRs are not known for fast response - hence my suggestion of a phototransistor.

getting a reading its not an issue. Issue is with a stuck LED on a smartmeter that is causing hung board and battery drain =(

I’m sorry, I can’t help with JS, since you’re sure everything is correct, maybe a JS group can sort it out for you.

was not a JS specific question. I was more asking for an advise on how to go about dealing with the issue that I have. It can be written in Arduino. JS was just something that came preinstalled, so I am just using that

would appreciate a more specific pointer =)
How do I make it "unlook" for LED? It is only executing secondary code when LED is detected (which is a short between "ground" pin D1 and positive D2)
I have made D2 now as input_pulldown when I know that LED is stuck. But I don't know if that is the right action