# How to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1

**URL:** <https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406>\
**Category:** General\
**Created:** [15 January 2020 20:53 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406 "2020-01-15T20:53:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [15 January 2020 20:53 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/1 "2020-01-15T20:53:15Z")

</div>

how to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1 or if gpio1 == 0 && gpio2 ==1 then gpio3=0  
im very new to node red. Thanks.  
Sorry for bad english.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 January 2020 23:27 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/2 "2020-01-15T23:27:54Z")

</div>

You might want to look at this [https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams)

---

<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:** [16 January 2020 10:25 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/3 "2020-01-16T10:25:57Z")

</div>

Doing simple logic operations kike this, is one of things that isn't simple to do in Node-RED for a beginner.

Please complete this table so we can give you suggestions  
[edit] This table is wrong based on the original post - sorry everyone ☹ [/edit]

```auto
gpio1 gpio2 gpio3
 0 0 ?
 0 1 0
 1 0 1
 1 1 ?

```

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [27 January 2020 20:45 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/4 "2020-01-27T20:45:14Z")

</div>

> [@cymplecy](#):
>
> gpio1 gpio2 gpio3 0 0 ? 0 1 0 1 0 1 1 1 ?

gpio1 gpio2 gpio3  
0 0 0  
0 1 0  
1 0 1  
1 1 1

---

<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:** [27 January 2020 21:03 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/5 "2020-01-27T21:03:20Z")

</div>

Did you look at the link @zenofmud posted? That technique will enable you to get the two input pin states into a function node.

---

<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:** [27 January 2020 22:03 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/6 "2020-01-27T22:03:43Z")

</div>

> [@devifast](#):
>
> gpio1 gpio2 gpio3  
> 0 0 0  
> 0 1 0  
> 1 0 1  
> 1 1 1

That means gpio3 = gpio1

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [27 January 2020 22:15 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/7 "2020-01-27T22:15:40Z")

</div>

And GPIO-2 is redundant.

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [29 January 2020 18:40 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/8 "2020-01-29T18:40:23Z")

</div>

> [@dynamicdave](#):
>
> redundant.

I want to do something like that, but I'm still new. Also, my English is not very high level.  
I don't know how to describe the pins in the function, what I'm showing is an example of logic, but it's not spelled correctly.  
My question is how to write it correctly.  
thanks

[{"id":"3f2862a8.7a553e","type":"function","z":"d4094956.4419b8","name":"","func":"if (gpio1 == 1 && gpio2 == 1) {\ngpio = 1;\n}\nelse{\n (gpio1 == 0 && gpio2 == 0){\ngpio = 0;\nreturn msg;\n}\n\n ","outputs":1,"noerr":8,"x":290,"y":140,"wires":}]

I did small projects with arduino and did fine. But things are different here.  
It looks like this:

int led=13;  
int high\_pin=3;  
int low\_pin=4;

void setup() {  
// put your setup code here, to run once:  
pinMode(led, OUTPUT);  
pinMode(high\_pin, INPUT);  
pinMode(low\_pin, INPUT);  
}

void loop() {  
// put your main code here, to run repeatedly:  
if (high\_pin == HIGH && low\_pin == HIGH) {  
digitalWrite(led, HIGH);  
}  
else {  
digitalWrite(led, LOW);  
}  
}  
Sorry about bad english.

---

<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:** [29 January 2020 20:00 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/10 "2020-01-29T20:00:01Z")

</div>

Did you see the link earlier about joining streams? That will allow you to get both the input gpio pins in one message.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 January 2020 20:09 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/11 "2020-01-29T20:09:02Z")

</div>

> [@cymplecy](#):
>
> Doing simple logic operations kike this, is one of things that isn't simple to do in Node-RED for a beginner.
> 
> Please complete this table so we can give you suggestions
> 
> ```auto
> gpio1 gpio2 gpio3
> 0 0 ?
> 0 1 0
> 1 0 1
> 1 1 ?
> 
> ```

I know this is late in the game, but I am just reading this thread.

Isn't that a misake @cymplecy? He wasn't GPIO 3 to be active if GPIO 1 and 2 are == 1.

Your second line has GPIO 3 active if GPIO 1 (ONLY) is == 1

---

<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:** [29 January 2020 20:35 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/12 "2020-01-29T20:35:56Z")

</div>

@Trying_to_learn - ignore my private message (I've deleted it) - your right - I got my table wrong ☹

No wonder this thread is confusing 🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 January 2020 20:39 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/13 "2020-01-29T20:39:11Z")

</div>

Don't worry.

I am the consummate at making mistakes.

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [29 January 2020 20:55 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/14 "2020-01-29T20:55:35Z")

</div>

how to describe gpio in function?  
I gave an example with an arduino.

---

<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:** [29 January 2020 21:02 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/15 "2020-01-29T21:02:27Z")

</div>

It is not easy to explain to a beginner who doesn't speak English as first language.

Using logic on two separate sources is not simple to do using Node-RED

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [29 January 2020 21:12 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/16 "2020-01-29T21:12:50Z")

</div>

Maths and programming languages do not need knowledge of English, Chinese, Swedish, etc. The logic is clear here. This is an elementary description of the inscription of two pins, whether up or down, and a series of consequences. Plus, I'm not that new to getting to this forum.  
Greetings.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 January 2020 22:51 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/17 "2020-01-29T22:51:32Z")

</div>

@devifast - you don't describe a gpio pin in a `function` node. You need two `gpio` nodes connected to a 'join' node. That will result in having both both values from the `gpio` nodes in one message. You can then use a `function` node to test the two values and output the result to a `gpio` node pointing to pin 3

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 January 2020 05:06 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/18 "2020-01-30T05:06:23Z")

</div>

> [@devifast](#):
>
> how to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1 or if gpio1 == 0 && gpio2 ==1 then gpio3=0

Hi again.

I am quickly looking at the RasPI GPIO node.

I can't find a `GPIO 1`

This is what I see when I open the `GPIO` node:

 ![Screenshot from 2020-01-30 16-06-39](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d92fe679ffeeb8de7bc1a63ff9338abd7c8dfdf.png)

`GPIO 2` and `GPIO3` are there, but are maybe not the best choice to use. They look like they are used but the SPI bus.  
Though if that isn't important, no problems.

For the sake of the exercise I will use GPIO 2, 3, and 4.  
2 and 3 are the inputs and 4 is the output.

**NOTE**  
the GPIO pins default to sending a `1` when not connected.  
The `topic` nodes: they can be anything you like. I called them the GPIO_x_ where `x` is the GPIO number.  
Do not confuse that with the PIN number.

This flow does NOT work.  
I am not smart enough to know how to do the _trick_ where it checks if 'one' and 'two' are both set.

But I am sure you can work that bit out.

```auto
[{"id":"9a517fc5.ab935","type":"rpi-gpio in","z":"d29f28a7.551f88","name":"","pin":"3","intype":"tri","debounce":"25","read":false,"x":220,"y":2730,"wires":[["b054583c.f22a88"]]},{"id":"c9661668.04fc2","type":"rpi-gpio in","z":"d29f28a7.551f88","name":"","pin":"5","intype":"tri","debounce":"25","read":false,"x":220,"y":2770,"wires":[["180c6ef0.fa94f9"]]},{"id":"b054583c.f22a88","type":"change","z":"d29f28a7.551f88","name":"GPIO 2","rules":[{"t":"set","p":"topic","pt":"msg","to":"GPIO2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":2730,"wires":[["3b51d656.85415a"]]},{"id":"180c6ef0.fa94f9","type":"change","z":"d29f28a7.551f88","name":"GPIO 3","rules":[{"t":"set","p":"topic","pt":"msg","to":"GPIO3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":2770,"wires":[["3b51d656.85415a"]]},{"id":"58cde312.c6653c","type":"inject","z":"d29f28a7.551f88","name":"Test input 1 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2690,"wires":[["b054583c.f22a88"]]},{"id":"5cb46292.7c2e4c","type":"inject","z":"d29f28a7.551f88","name":"Test input 2 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2810,"wires":[["180c6ef0.fa94f9"]]},{"id":"1c4e2277.ee397e","type":"inject","z":"d29f28a7.551f88","name":"Test input 1 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2650,"wires":[["b054583c.f22a88"]]},{"id":"5eefa522.aae874","type":"inject","z":"d29f28a7.551f88","name":"Test input 2 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2850,"wires":[["180c6ef0.fa94f9"]]},{"id":"3b51d656.85415a","type":"function","z":"d29f28a7.551f88","name":"Magic happens here","func":"// Process incoming messages and store in context.\nif (msg.topic == \"GPIO2\")\n{\n context.set('one',msg.payload);\n}\nif (msg.topic == \"GPIO3\")\n{\n context.set('two',msg.payload);\n}\n\n// Set up context variables to store values.\nlet one = context.get('one') || 0;\nlet two = context.get('two') || 0;\n\nif ('one' == 1 && 'two' == 1) // You need to work on this line. As is it doesn't work.\n{\n // Create message to be on-sent.\n msg.payload = \"DO THINGS\";\n} else\n{\n return;\n //msg.payload = null;\n \n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":540,"y":2750,"wires":[["808b5691.f144e8"]]},{"id":"808b5691.f144e8","type":"debug","z":"d29f28a7.551f88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":2750,"wires":[]}]

```

I am **not** a good programmer.  
Please forgive my mistakes.  
But I hope you see what I am trying to explain to you.

Good luck.

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [30 January 2020 12:29 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/19 "2020-01-30T12:29:01Z")

</div>

> [@Trying\_to\_learn](#):
>
> how to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1 or if gpio1 == 0 && gpio2 ==1 then gpio3=0

Look at pins 1,2,3 rather like pins x, y, z or a, b, c 1,2, and 3 are just an example.  
I noticed that people take it verbatim.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 January 2020 13:12 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/20 "2020-01-30T13:12:56Z")

</div>

> [@devifast](#):
>
> I noticed that people take it verbatim.

Well when you discribe a situation and give an example and don't say that your example it theoretical, what do you expect?

If you want to work with two GPIO pins, then you will have two `GPIO` nodes and two seperate msg's. To evaluate the contents of the two messages you have to join the msg's together or you could constantly put then into two flow variables.

in eithor case you need to define what happens in all four cases - a simple truth table. The truth table you gave:

> gpio1 gpio2 gpio3  
> 0 0 0  
> 0 1 0  
> 1 0 1  
> 1 1 1  
> says  
> if gpio 1 is 0 the result is 0 - gpio 2 has no effect on the result  
> if gpio 1 is 1 the result is 1 - gpio 2 has no effect on the result

If this is the case then you don't need to worry about gpio2. If it's not the case, then you need to correct the truth table to display the actual results you need to see.

So you eithor have a situation where  
`if (gpio1 == 1) {gpio3 = 1} else {gpio3 = 0}`  
looking at your arduino code I think your truth table should be:

```auto
gpio1 gpio2 gpio3
  0 0 0
  0 1 0
  1 0 0
  1 1 1

```

which would result in an if statement like:  
`if (gpio1 == 1 && gpio2 == 1 ) {gpio3 = 1} else {gpio3 = 0}`

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [30 January 2020 14:22 UTC](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/21 "2020-01-30T14:22:51Z")

</div>

> [@zenofmud](#):
>
> if (gpio1 == 1 && gpio2 == 1 ) {gpio3 = 1} else {gpio3 = 0}

It's supposed to happen, but how do I write into function. Thanks  
if (gpio1 == 1 && gpio2 == 1 ) {gpio3 = 1} else {gpio3 = 0}

[Next page](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406.md?page=2)
