The saga continues…
I believe I’m likely seeing EMI in the electrical cabinet that is causing some problems that I thought were debounce issues. That being said, I’m posting my device code to confirm my debounce is consistent with the example code. The input is pulled to ground with a 10K resistor and tied to 3.3 via the current switch input (normally open).
`
//Controls a timed output based upon an input going high
//Created: 20150402 ctm
//Revised: 20150415 - incresed debouncePeriod to 1.0 and then 2.0
inputPort <- hardware.pin9;
outputPort1 <- hardware.pin1;
outputPort2 <- hardware.pin2;
const on = 1;
const off = 0;
const timer1Period = 45;//Specifies the number of seconds output 1 should be on;
const timer2Period = 60;//Specifies the number of seconds output 2 should be on
const debouncePeriod = 2.0; //length of input debounce period in seconds
dummy <- null;
server.log(“StarTimer v3 device code starting”);
input <- off;
class timedOutput {
static on=1;
static off=0;
_pin = null;
_gsStream = null;
timer = null;
constructor (pin,gsStream) {
_pin = pin; //hardware pin
_gsStream = gsStream; //GroveStreams stream name
_pin.configure(DIGITAL_OUT);
}
function start(period) {
server.log("starting output="+_gsStream);
_pin.write(on);
// if (timer!=null) {
// server.log("Canceling existing timer");
// imp.cancelwakeup(timer);
// }
timer = imp.wakeup(period,stop.bindenv(this));
agent.send("gsPut","&"+_gsStream+"=1");
}
function stop() {
server.log("stopping ouput="+_gsStream);
if (timer!=null) {
imp.cancelwakeup(timer);
}
if (_pin.read()==on){
_pin.write(off);
agent.send("gsPut","&"+_gsStream+"=0");
}
}
}
function startup() {
//This function is called at startup and runs only once
server.log("The MAC address of this device is: "+imp.getmacaddress());
server.log("The unique id of this device is: "+hardware.getdeviceid());
server.log("The SSID of the wireless network is: "+imp.getssid());
server.log("The signal strength is: "+ imp.rssi());
//The output is sent to pin9 of the imp
timer1 <- timedOutput(outputPort1,“fanOutput1”);
timer1.stop(); //make sure the output is off
//timer2 <- timedOutput(outputPort2,“fanOutput2”);
//timer2.stop(); //make sure the output is off
//The input being watched
inputPin <- inputPort;
inputPin.configure(DIGITAL_IN,debounceInput);
}
function inputHandler(){
input = inputPin.read(); //this should be a debounced input
server.log(“input=”+input);
if (input==off){
timer1.stop();
//timer2.stop();
}
else { //input is on
timer1.start(timer1Period);
//timer2.start(timer2Period);
}
inputPin.configure(DIGITAL_IN,debounceInput); //re-enable debounce callback
}
function debounceInput(){
inputPin.configure(DIGITAL_IN); //disable callback
imp.wakeup(debouncePeriod,inputHandler) //let input debounce
}
function requestParameters(dummy){
//called by the agent if it needs configuration information that requires the device ID
agent.send(“getDeviceParams”,hardware.getdeviceid());
}
startup();
agent.on(“requestParameters”,requestParameters)
`
I’m basically seeing double inputs about a second apart. I’m now wondering if there are input state changes happening so quickly, they get queued before debounceInput() gets called to remove the callback. Thoughts?