Problems with optional callback function when pin state changes

Hi,

I am using a pin of the imp002 for switching purpose. Whenever the switch state changes, a particular function should be called.

//Configure Manual Switch Manual <- hardware.pinA; Manual.configure(DIGITAL_IN, ManualAction);

However, the ManualAction function keeps getting called even when the pinA value remains constant. It is not being called from anywhere else in the program. Suggestions are welcome!

Try with this instead

//Configure Manual Switch Manual <- hardware.pinA; Manual.configure(DIGITAL_IN_PULLUP, ManualAction);

If you don’t have a pull-up resistor on, you can enable one inside the Impee instead. If no pull-up or pull-down resistor is present, the pin can float and trigger randomly.

…plus, per your email to us, you will likely need to implement switch debouncing.

Thanks for the prompt responses! The issue is still present; so as a quick and lousy fix, I have started polling the pin instead of having a callback function!

The issue is not with bouncing of the switch as far as I can see. If I keep a callback function (ManualAction), it keeps getting called mostly seconds after there is a TurnOn() and TurnOff() call in the device code; even when the switch has not been touched for a few seconds. Sometimes, ManualAction is called for no reason at all.

The TurnOn/TurnOff call can be due to motion, from an mobile app etc. Pulling down the pin is also not helping.

I am not able to figure out the connection between a write on the relay pin and call of the ManualAction function.

`

Relay <- hardware.pin5;
Relay.configure(DIGITAL_OUT);

function TurnOn()
{
server.log(“Turn On”);
Relay.write(1);
imp.sleep(0.02);
status=1;
sendsensordata();
}

function TurnOff()
{
server.log(“Turn Off”);
Relay.write(0);
imp.sleep(0.02);
status=0;
sendsensordata();
lockLow = true;

}

//Configure Manual Switch
Manual <- hardware.pinA;
Manual.configure(DIGITAL_IN, ManualAction);

`

Suggestions are welcome!

This is how I did it on my rain gauge

function rainPulse() { local state = hardware.pin2.read(); if (state == 1) { agent.send("sensorRainfallPulse", 0); } } hardware.pin2.configure(DIGITAL_IN, rainPulse);

Works without any problems for me, and all the rain gauge does, is to close a reed switch which pulls the pin down to GND.

How is the relay controlled? Sounds like you have some coupling to the high impedance input from the relay.

How long are the wires to the switch? You may need to add some capacitance to filter spurious noise.