# Toggle GPIO Input

**URL:** https://discourse.nodered.org/t/toggle-gpio-input/60643
**Category:** General
**Created:** [1 April 2022 10:23 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643 "2022-04-01T10:23:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![BWTH](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@BWTH](https://discourse.nodered.org/u/BWTH)
#### Post date: [1 April 2022 10:23 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/1 "2022-04-01T10:23:41Z")

</div>

Hi  
i am looking for an funktion that does the following:  
on my Raspberry when i push an button (GPIO in 7), than i want to send out udp1, if i push the same button a second time i want to send out udp2, and than if i push the button another time i want to sent out udp1....so now i have tried it with the toggle note, but i have no result which is working. any ideas for an beginner to solve the problem  
thanks

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [1 April 2022 11:40 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/2 "2022-04-01T11:40:18Z")

</div>

You need to save the last state in a context which can be remembered when the next msg arrives. Either you take a flow variable or a context variable in a function node.

---

<div class="post-metadata">

### Author: ![BWTH](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@BWTH](https://discourse.nodered.org/u/BWTH)
#### Post date: [1 April 2022 18:00 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/3 "2022-04-01T18:00:23Z")

</div>

thanks, but for me as an beginner this is too compicated, is there no other way. over a node witch i can install?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [1 April 2022 20:26 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/4 "2022-04-01T20:26:43Z")

</div>

Did you try searching [flows.nodered.org](http://flows.nodered.org) for Toggle. I found this very quickly there. I haven't tried it but it looks close to what you want. [Toggle Switch Subflow (flow) - Node-RED](https://flows.nodered.org/flow/15c17f3445639d90139f4b05113f4a8d)

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [1 April 2022 20:27 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/5 "2022-04-01T20:27:57Z")

</div>

Import this function node:

```auto
[
    {
        "id": "db823e37fd839077",
        "type": "function",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "func": "var output = !(context.get(\"toogle\")||false);\ncontext.set(\"toogle\",output);\nmsg.payload = output ? \"udp1\" : \"udp2\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 1040,
        "wires": [
            [
                "6d935a3dcad3e665"
            ]
        ]
    }
]

```

toogles your output.

---

<div class="post-metadata">

### Author: ![BWTH](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@BWTH](https://discourse.nodered.org/u/BWTH)
#### Post date: [2 April 2022 06:00 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/6 "2022-04-02T06:00:24Z")

</div>

thank you, that works perfect

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [2 April 2022 06:32 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/7 "2022-04-02T06:32:58Z")

</div>

No code version

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/762a6fe171cb191b9acf25ac02e0de1c0cb1d91a.png)

```auto
[
    {
        "id": "db823e37fd839077",
        "type": "function",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "func": "var output = !(context.get(\"toogle\")||false);\ncontext.set(\"toogle\",output);\nmsg.payload = output ? \"udp1\" : \"udp2\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 1040,
        "wires": [
            [
                "6d935a3dcad3e665"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 June 2022 06:33 UTC](https://discourse.nodered.org/t/toggle-gpio-input/60643/8 "2022-06-01T06:33:49Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
