Turning off an LED

I’m trying to create a remote triggered light with my Imp and April board. I figured a good place to start with the code would be the Blink-O-Matic worked example. It has been extremely helpful, and I have figured out things on the remote side. My problem now is that I cannot for the life of me get the LED to turn off. Even when working with Blink-O-Matic I was having issues, when I sent 0 the blinking would stop but the LED would remain on. I have tried just write(0), and even tried changing the pin mode (while that sometimes worked), it didn’t give me the results I wanted. My modified Blink-O-Matic code is below:



// Variable to represent LED state
local ledState = 0;
 
// Variable to represent LED inhibit state
local inhibit = 1;
 
// blink function called every 100ms
function blink()
{
    // Check if we’ve been switched off
    if(inhibit)
    {
        // Blinking inhibited, turn off the LED
        hardware.pin9.configure(DIGITAL_OUT_OD_PULLDOWN);
        hardware.pin9.write(0);
    }
    else
    {
        // Change state
        //ledState = ledState?0:1;
 
        // Reflect state to the pin
        hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
        hardware.pin9.write(1);
        
        //inhibit
        //inhibit = 1;
        // Schedule the next state change
        imp.wakeup(10, blink);
    }
}
 
// input class for LED control channel
class input extends InputPort
{
  name = “LED control”
  type = “number”
 
  function set(value)
  {
      
      if(value == 0)
      {
          // 0 = Inhibit LED operation
          inhibit = 1;
      }
      else
      {
          // 1 = Enable LED operation
          inhibit = 0;
          blink();
      }
  }
}
 
// Configure pin 9 as an open drain output with internal pull up
hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
 //turn off
//hardware.pin9.write(0);
// Register with the server
imp.configure(“IoT Lamp”, [input()], []);

// Start blinking
//blink();
 
// End of code.

Hi


I suspect the problem is with the pin configuration. How is your LED attached?  You’re configuring it as open drain, but you’re changing the pull polarity when driving the pin, which probably isn’t correct. The Blink O Matic example suggests comnnecting the LED cathode to the pin, which should then be configured open drain (to sink current) with an internal pull up (so when the pin is not driven it’s pulled high and no current flows through the LED).

In your example you’re turning off the pin but changing to pull down, so the LED is going to illuminate.

Basically, decide how your circuit is wired and configure the pin appropriately once at the start.

Rob
Fen Consultants, UK
If I wire it oneway the LED is always off if I reverse the wiring it is always on flashing. What is the secret to get
Blink-O-Matic to work?

 // I commented out this line out of the example and it works - the LED turns on and off >  imp.wakeup(0.1, blink);

Did you get it going to what you want or so you need some more help?