Push button 0/1 GPIO

Hello, can anyone help me? I need a function that allows me to change the state.

what I want is to press the button and have the output to 1 and press again and output = 0

Captura de pantalla 2020-01-23 a las 23.45.38

thanks

Try this and see if it it what you want.

[{"id":"7347b78.8b23848","type":"switch","z":"6c4e53bf.97148c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":180,"wires":[["82ff5a1a.efefc8"],["26637948.29d256"]]},{"id":"82ff5a1a.efefc8","type":"change","z":"6c4e53bf.97148c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"next","pt":"flow","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":140,"wires":[[]]},{"id":"26637948.29d256","type":"change","z":"6c4e53bf.97148c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"},{"t":"set","p":"next","pt":"flow","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":200,"wires":[[]]},{"id":"5047f5ea.66c1ec","type":"function","z":"6c4e53bf.97148c","name":"get next value","func":"msg.payload = flow.get(\"next\")||0;\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":180,"wires":[["7347b78.8b23848"]]}]

That might not have been the correct flow. I'll try and check back later if it doesn't work for you.

sorry but where do I have to put this? in node function?

That is a flow that you will import into your existing flow.

If you put a function node between your existing ones and then paste this code into it then it should do what you want

if (msg.payload == 1) {
    context.set("previousState",!context.get("previousState"))
    msg.payload = 1 * context.get("previousState");
    return msg;
}

If you need an explanation - please ask for more information :slight_smile:

2 Likes

thank you very much it is perfect!!!

Just got back to this forum. Glad it worked for you.

Thanks It Worked

1 Like