• Hello
    I thought I had my power meter puck all ready, but after about a week of successful running, I got into a problem that my power meter LED that blinks for every watt, gets stuck with LED always on =(
    I have solar power and possibly it causes issues with a meter (have to call my power company).
    Anyway... Is it possible to somehow work around the issue with the code?
    This is my code to trigger on the light pulse, similar to the example code:

      pinMode(D2,"input_pullup");
      setWatch(function(e) {
        c.inc(1);
        pulseTotal++;
        update();
        if (LEDStatus==1){
          digitalPulse(LED1,1,1); // show activity
        }
      }, D2, { repeat:true, edge:"falling" });
    
    

    Not sure what I can do to detect that D2 is short for too long and somehow stop draining the battery if it gets stuck for too long. Once the sun sets, it goes back to normal blinking on power use

  • Actually yes, I guess you could remove the pullup if it's on for too long.

    So maybe some additional code:

    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;
      } else {
        onCount++;
        if (onCount>6) { // >60 secs on
          pulledUp = false;
        }
      }
      if (!pulledUp) pinMode(D2,"input");
    }, 10000);
    
    //And then in your setWatch
    setWatch(function(e) {
      if (!pulledUp) return;
      //...
    
  • Thank you!
    Can you please explain the code? I am a bit confused with all the "pullups" =)
    So we set a variable pulledUp to true and counter to zero.
    Then every 10s we check.
    I am confused with the else part - if we set pulledUp to false during "stuck LED" condition, then the first IF will get executed after 10 seconds, which will cause a reading update?
    Should it not be other way around? I am confused =)

      if (D2.read()) { // LED is off - all ok
        onCount = 0;
        pulledUp = FALSE;
      } else {
        onCount++;
        if (onCount>6) { // >60 secs on
          pulledUp = TRUE;
        }
      }
    

    Also, what is the difference between

     if (!pulledUp) pinMode(D2,"input_pullup");
    

    in the beginning and

    if (!pulledUp) pinMode(D2,"input");
    

    in the end? Or the first one suppose to be true and the last one false?

  • Did you try it at all? does it work?

    The idea is:

    • If pulledUp==true, the pin is currently set to input_pullup, so there's no need to change pin state, hence the if (!pulledUp) pinMode. This is the normal state for reading the pulses of light when the LED is not on
    • if pulledUp==false, we have turned it off to save power when the LED is on, but to get a usable reading we have to turn it the pullup on temporarily

    As it happens, because of the order of the if (!pulledUp) pinMode stuff we can just do what we want by setting pulledUp.

  • well, it works but trying to understand how it saves power.
    Just because we set variable pulledUp to true does not mean that we set pinmode to input_pullup
    first line only sets the pinmode to input_pullup if pulledUp is false

    if (!pulledUp) pinMode(D2,"input_pullup");
    

    so I am trying to understand when does it actually set pinmode, if the condition when everything is ok is setting it to false...

  • so I am trying to understand when does it actually set pinmode, if the condition when everything is ok is setting it to false...

    Lets say the LED is off, and pulledUp==false... The flow of execution looks like this:

      // pulledUp==false, so pinMode(D2,"input_pullup"); is called
      if (!pulledUp) pinMode(D2,"input_pullup");
      // LED off, so D2.read() == true
      if (D2.read()) { // LED is off - all ok
        // we execute in here
        onCount = 0;
        pulledUp = true;
      } else {
        // not executed
      }
      // pulledUp is now true, so this is not executed
      if (!pulledUp) pinMode(D2,"input");
    

    So at the end of that, pulledUp==true, but pinMode(D2,"input_pullup"); was called at the start of the function

  • but what if LED is on (stuck)? So the loop that reads it every 10s sets pin pull up and then sets to input.
    I think I am missing the understanding of what input_pullup and input means.
    The confusion comes as I used to set pinmode to input_pullup before the setWatch

  • pinMode info is here: http://www.espruino.com/Reference#l__global_pinMode

    input_pullup adds a resistor inside the chip, pulling the LDR pin towards 3v. If you keep that on when the LED is on then it's going to suck up lots of power - hence the reason for just setting to an input to turn it off

  • ok... I think I get it now =) thank you for your patience - my brain must be functioning in another way =) But running through it I feel I still dont get it =)
    Lets imagine that LED is not stuck and its functioning as needed. And LED is currently OFF during the time this check is done.
    Running through this function makes it:
    pulledUp=true
    first IF is not fired up, as pulledUP variable is true
    second IF is fired up and sets pulledUP variable to true again
    third IF is not fired up as pulledUP variable is true

    Now, there is a brief pulse of LED light, which is a normal meter functioning, but outside the function's 10second loop.
    How does my setWatch function work if we did not set the pinMode for D2 to be input_pullup?

      setWatch(function(e) {
        if (!pulledUp) return;
        c.inc(1);
        pulseTotal++;
        update();
        if (LEDStatus==1){
          digitalPulse(LED1,1,1); // show activity
        }
      }, D2, { repeat:true, edge:"falling" });
    

    I keep wanting to change

    if (!pulledUp) pinMode(D2,"input_pullup");
    

    to

    if (pulledUp) pinMode(D2,"input_pullup");
    

    Just dont understand how it works without changing the pinMode if pulledUp variable is staying true on the "unstuck" day

  • Lets imagine that LED is not stuck and its functioning as needed. And LED is currently OFF during the time this check is done.

    In this case, you'd hope that the setInterval would do nothing at all, which I think is what happens? You don't want it to mess with pinMode if it's not needed because that might cause the pin to change state, setWatch would fire, and you'd register an invalid reading.

    In the case where the LED was on previously and then it turns off which I think is what you mean:

    pulledUp is actually false, so:

      if (!pulledUp) pinMode(D2,"input_pullup"); // this is called
      if (D2.read()) { // LED is off - this is called.
        onCount = 0;
        pulledUp = true;
      } else {
        // not called
      }
      if (!pulledUp) pinMode(D2,"input"); // pulledUp now true, so not called. pin mode is left as input_pullup
    }, 10000);
    
  • problem is that it's called in interval and there is a high chance that during the day LED may flash in between the checks.
    I still want to capture the LED flash, which is done through setWatch.
    Why there was pinmode set in the original code before the setWatch? Maybe it's a key to my understanding

  • problem is that it's called in interval and there is a high chance that during the day LED may flash in between the checks.
    I still want to capture the LED flash, which is done through setWatch.

    Yes, that's fine. I should still work - and as far as I understand you tested and it does work?

    Why there was pinmode set in the original code before the setWatch? Maybe it's a key to my understanding

    As I said above, the pullup is enabled because it's like a 'potential divider'. When the light level is low, LDR resistance is high and the voltage on the pin goes high. When you have light, LDR resistance is low and it pulls the pin voltage low.

    But I'm sorry, I'm really busy this week and I can't spend any more time walking you through the code. I think I've already covered what it does line by line in every circumstance

  • thank you for your help

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Puck.js with LDR - how to deal with light that is always on?

Posted by Avatar for user130485 @user130485

Actions