# \[Resolved\] Start/Stop motor with limit switch

**URL:** <https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332>\
**Category:** General\
**Created:** [21 September 2018 12:09 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332 "2018-09-21T12:09:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [21 September 2018 12:09 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/1 "2018-09-21T12:09:02Z")

</div>

Hello,

I wish I could drive (start and stop) a motor with the switch on my smartphone and if the movement is detected by the limit switch stop the movement. No worries with the use of GPIO.  
I tested several functions but with the switch of the ui I can not operate.

Could you help me.

Thank you

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 September 2018 12:17 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/2 "2018-09-21T12:17:01Z")

</div>

If you have written a function in a flow, even if they don't work, it is useful to share them here as often a member of the community can see where you are going wrong and suggest how you can improve your flow.

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [21 September 2018 12:54 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/3 "2018-09-21T12:54:01Z")

</div>

This is the first time I posted on this forum and I start in Node red.

I hope I have taken the right data.

[{"id":"11e0a82.22036d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"e1735106.aafe68","type":"ui\_switch","z":"11e0a82.22036d8","name":"Moteurs Start","label":"Moteur start","group":"6781044e.5564a4","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"str","onicon":"","oncolor":"","offvalue":"false","offvalueType":"str","officon":"","offcolor":"","x":280,"y":80,"wires":[["ec211cec.30bbf"]]},{"id":"8892fb3d.fda038","type":"rpi-gpio out","z":"11e0a82.22036d8","name":"Motor","pin":"3","set":true,"level":"0","freq":"","out":"out","x":730,"y":120,"wires":[]},{"id":"780f27b4.65a4d","type":"rpi-gpio in","z":"11e0a82.22036d8","name":"Switch stop","pin":"7","intype":"down","debounce":"100","read":false,"x":270,"y":160,"wires":[["ec211cec.30bbf"]]},{"id":"ec211cec.30bbf","type":"BooleanLogic","z":"11e0a82.22036d8","name":"Test","operation":"XOR","inputCount":2,"topic":"result","x":520,"y":120,"wires":[["8892fb3d.fda038"]]},{"id":"6781044e.5564a4","type":"ui\_group","z":"","name":"Moteurs","tab":"6d3213cb.a45f4c","order":1,"disp":true,"width":"6","collapse":false},{"id":"6d3213cb.a45f4c","type":"ui\_tab","z":"","name":"Chirocam","icon":"dashboard","order":1}]

![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c8bfe2e3279af0873b34c5b2a181b558a2d8cdd4.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 September 2018 13:15 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/4 "2018-09-21T13:15:02Z")

</div>

I can't see a function node i there...

So the labels underneath the nodes might help explain some of what is happening  
Your dashboard switch is sending "on" but your RPi Switch is expecting a 0 or 1

Take a look at the change node to change on or off into 1 or 0

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [21 September 2018 13:26 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/5 "2018-09-21T13:26:57Z")

</div>

If I use boolean or number it's the same thing. The XOR remains in "Unknow".

 ![Capture1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2a7d6c0832faf654e5e778ee2f7ad1d4444d27f8.png)

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [21 September 2018 13:29 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/6 "2018-09-21T13:29:33Z")

</div>

If i use Inject it is ok.

 ![Capture2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/412860be5c8e15993c1ccde49e685eaa6e0f44e2.png)

I still have to initialize "switch stop" for the XOR to work

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 September 2018 15:15 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/7 "2018-09-21T15:15:04Z")

</div>

Have you read the ReadMe page of the XOR node? I assume it is [https://flows.nodered.org/node/node-red-contrib-boolean-logic](https://flows.nodered.org/node/node-red-contrib-boolean-logic) ? The part entitled **####BooleanLogic** might be or interest...

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [21 September 2018 17:01 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/8 "2018-09-21T17:01:34Z")

</div>

I had looked. What is problematic is that I can make it work if I replace the switch with an Inject.  
I guess what I want to do with the XOR needs to be done with an If function but I do not know how to do it.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [21 September 2018 17:07 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/9 "2018-09-21T17:07:43Z")

</div>

You started this thread by saying you had tried several functions what have you tried?

---

<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:** [22 September 2018 12:14 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/10 "2018-09-22T12:14:51Z")

</div>

If it works with inject but not with an alternative technique then you are not sending the same message content. Use a debug node to see what you are actually sending it.

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [23 September 2018 13:15 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/11 "2018-09-23T13:15:27Z")

</div>

I did what you propose.

First image with switch I true in debug

 ![Capture3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/20ab8c08112f2cd66f0acbb6a3bf0f84c32ef6c5.png)

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [23 September 2018 13:15 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/12 "2018-09-23T13:15:59Z")

</div>

Second image with Inject i have true too.

 ![Capture4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d59492085cff4a872b50a4344098f6d76f412f0c.png)

I do not understand what I'm not doing well

---

<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:** [23 September 2018 14:33 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/13 "2018-09-23T14:33:08Z")

</div>

> [@Pittex](#):
>
> I do not understand what I'm not doing well

Configure the debug nodes to show the complete message.

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [23 September 2018 15:13 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/14 "2018-09-23T15:13:40Z")

</div>

I'm not sure I did the right thing. I have this.

With Inject  
msg : Object  
object  
\_msgid: "6bb413af.db164c"  
topic: ""  
payload: true

Witch switch  
msg : Object  
object  
payload: true  
socketid: "EZnvBbW-qcTwrxVFAAAB"  
\_msgid: "85fcfbbc.fe51a8"

I double click on the debug and I have selected complete msg object and selected the 2 items

---

<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:** [23 September 2018 17:19 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/15 "2018-09-23T17:19:44Z")

</div>

Stick a debug on the PIN 3 node also to show what it is sending to the boolean node

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [23 September 2018 17:31 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/16 "2018-09-23T17:31:52Z")

</div>

I test another approach with an if function.  
But I start, I learn to do  
if Switch = 1 and Pin = 0 -\> Motor = 1.  
if Switch = 1 -\> Motor = 0.  
It takes me time since I can not understand the structure of the function module.

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [23 September 2018 18:09 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/17 "2018-09-23T18:09:10Z")

</div>

I test

if (msg.payload=="SwitchOn" && msg.payload=="PinOff")  
{  
msg.payload=1;  
}  
else if (msg.payload=="SwitchOff")  
{  
msg.payload=0;  
}  
return msg;

But write msg.payload=="SwitchOn" && msg.payload=="PinOff", how to write this correctly?

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [23 September 2018 18:40 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/18 "2018-09-23T18:40:04Z")

</div>

I test another possibility

[{"id":"64a069ff.e5db","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"c289498d.545d98","type":"rpi-gpio in","z":"64a069ff.e5db","name":"","pin":"38","intype":"down","debounce":"100","read":false,"x":176,"y":130,"wires":[["75db7356.b10fc4"]]},{"id":"75db7356.b10fc4","type":"function","z":"64a069ff.e5db","name":"PinState","func":"if (msg.payload==1)\n{\n global.set("Pin38","PinOn");\n}\nelse if (msg.payload===0)\n{\n global.set("Pin38","PinOff");\n}","outputs":1,"noerr":0,"x":409,"y":131,"wires":[[]]},{"id":"23c3d3f.3a2742c","type":"ui\_switch","z":"64a069ff.e5db","name":"","label":"Switch1","group":"6781044e.5564a4","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":177,"y":202,"wires":[["843460f1.180848","505a2341.c4768c"]]},{"id":"843460f1.180848","type":"function","z":"64a069ff.e5db","name":"SwitchState","func":"global.set("Switch",msg.payload)","outputs":1,"noerr":0,"x":414,"y":201,"wires":[[]]},{"id":"ec74ae6b.9b39a","type":"function","z":"64a069ff.e5db","name":"","func":"var PinStatus=global.get("Pin38");\nvar SwitchStatus=global.get("Switch");\n\nif (SwitchStatus===true)\n{\n if (PinStatus==="PinOff")\n {\n msg.payload=1; \n }\n}\n\nif (SwitchStatus===false)\n{\n msg.payload=0; \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":207,"y":293,"wires":[["bf34bf19.87fba8"]]},{"id":"bf34bf19.87fba8","type":"rpi-gpio out","z":"64a069ff.e5db","name":"","pin":"37","set":true,"level":"0","freq":"","out":"out","x":416,"y":294,"wires":[]},{"id":"505a2341.c4768c","type":"debug","z":"64a069ff.e5db","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":434,"y":244,"wires":[]},{"id":"6781044e.5564a4","type":"ui\_group","z":"","name":"Moteurs","tab":"6d3213cb.a45f4c","order":1,"disp":true,"width":"6","collapse":false},{"id":"6d3213cb.a45f4c","type":"ui\_tab","z":"","name":"Chirocam","icon":"dashboard","order":1}]

But does not work

---

<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:** [23 September 2018 18:51 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/19 "2018-09-23T18:51:01Z")

</div>

> [@Pittex](#):
>
> Witch switch  
> msg : Object  
> object  
> payload: true  
> socketid: "EZnvBbW-qcTwrxVFAAAB"  
> \_msgid: "85fcfbbc.fe51a8"

Are you sure you are not setting a topic in the switch? I thought the XOR node needs topics to tell the two inputs apart.

---

<div class="post-metadata">

**Author:** ![Pittex](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pittex](https://discourse.nodered.org/u/Pittex)\
**Post date:** [24 September 2018 08:03 UTC](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332/20 "2018-09-24T08:03:17Z")

</div>

Thank you everyone. Colin found, he was missing the topic on the switch.

If anyone can look at my other two solutions, I'd like to move on.  
A big thank-you.

[Next page](https://discourse.nodered.org/t/resolved-start-stop-motor-with-limit-switch/3332.md?page=2)
