How to prevent multiple commands caused by mistake

Hello all,
I want to make garden door opener which can be operated with smartphone and web app. My trouble is when I, by mistake, press button in app more than one time I get two pulses on relay and door don’t do what I want. I want that, when I send command to imp, that he ignore commands for approx. 10 sec and that they are not stored in memory.
I apologize myself for my bad English language but I hope that you understand what I want.
Here is device code for triggering relay:

// Turn relay on for 0.9 second and then off
//--------------------------------------------------------------------------------------------------------
function pulseRelay()
{
hardware.pin5.write(1);
imp.sleep(0.9);
hardware.pin5.write(0);
//imp.sleep(5);
}

//--------------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------------
agent.on(“buttonPressed”, function( value )
{
if( value )
{
server.log("HTTP: " + value );
local startIdx = value.find(“Button”);
server.log("IDX: " + startIdx );
if( startIdx != null )
{
pulseRelay();
//imp.sleep(5);
}
}

});

thank you for all your help

So you want it to ignore commands within 10 seconds from the last one?

First you need to make the relay pulse non-blocking (using an imp.wakeup), then add a timestamp, eg:

`function pulseRelay()
{
// Turn relay on, then queue a callback to turn it off 0.9s later
hardware.pin5.write(1);
imp.wakeup(0.9, function() { hardware.pin5.write(0); });
}

lasttrigger <- 0;

agent.on(“buttonPressed”, function(value)
{
// Has it been 10 seconds since the last trigger? If not, ignore this
if ((time() - lasttrigger) < 10) return;

if (value)
{
server.log("HTTP: " + value );
local startIdx = value.find(“Button”);
server.log("IDX: " + startIdx );
if( startIdx != null )
{
pulseRelay();

  // edit! missed this line before. Save when we were last triggered
  lasttrigger = time();

  }
}

}
}
`

…try that. No sleeps at all :slight_smile:

Is it a solenoid type of door latch?
How about adding a limit switch, or proximity switch so it knows when the door has been opened? That could also be an alarm to text your phone if someone opens the door without engaging the door latch.

Hello Hugo,

thank you for your help code working as a charm. Thank you again. If I will do some new project I will certainly need some help. My basic education is mechanical engineer have no experience in programming, so I do my electronic projects copy-paste and with help on forums. So to all of you who are helping us “lammers” THANK YOU. Together we will make world better.

With best wishes to all of you.

Bostjan

@buskovc there’s a first time for everything!

I hope you understand the changes I suggested - there’s not really much magic there, it’s just keeping track of when the last command was issued (time() is seconds since 1-Jan 1970) and discarding commands that arrive within 10 seconds of the last command that actually took action.

Good luck with your future projects!

Hello Hugo, I understand what you have done. Unfortunately I’m not familiar with program logic and commands therefore if I make new projects expect that I will put some questions here.

Bostjan

Sure :slight_smile: