Toggle gpio with external trigger on/off/on
-
Hi - I'm trying to create an external trigger that toggles (flip flop) a relay module on and off, or off and on. I want to use a single momentary button to do this so I can save space. The relay will operate a dust extractor.
if sensors.gpIn[2].value=0 M42 P2 S1 else sensors.gpIn[2].value=1 M42 P2 S0
I though it would be as simple as the above but it's only performing the first if part. Any suggestions welcomed.
-
echo
out the state of the pin before the if statement might give you some insight(Edit: Also you need
else if
or just strike the condition. Pin is 1 if it's not 0, can't be anything else) -
@bearer Still no luck - I'm a bit confused now.
If I only write in the external trigger
if sensors.gpIn[2].value=0 M42 P2 S1
and the pin value is 0, running the external trigger turns it on
but if I write
if sensors.gpIn[2].value=1 M42 P2 S0
while the pin is 1, it doesn't change.
-
@EducatingSavvas said in Toggle gpio with external trigger on/off/on:
if sensors.gpIn[2].value=0
I believe @bearer meant you could try
if sensors.gpIn[2].value=0 M42 P2 S1 else M42 P2 S0
i.e no need for second if conditional
-
@OwenD said in Toggle gpio with external trigger on/off/on:
if sensors.gpIn[2].value=0 M42 P2 S1 else M42 P2 S0
Just tried that and that doesn't work either.
-
Then you either found a bug, of your pin is always determined to be 0.
Writing out the value before the if statement with
echo
is probably a good idea to see whats going on.If it always writes out 0 then you either wired something wrong, or it could be the pin has been damaged.
-
@bearer said in Toggle gpio with external trigger on/off/on:
ined to be 0.
If I write M42 P2 S1 or M42 P2 S0 into the console it changes.
echo sensors.gpIn[2].value=0
results in a false, regardless if the M42 P2 is S1 or S0I don't really know what to do next...
-
sensors.gpIn[2] is the same pin as you're setting with M42 P2?
maybe poke around with m409 to see if that should be gpOut or something?
https://duet3d.dozuki.com/Wiki/Gcode#Section_M409_Query_object_model -
@bearer Yes - I'm checking the state of the same pin I want to change depending on the value.
-
Then i suspect you should use sensors.gpOut[2] instead og sensors.gpIn[2] but you can test with m409.
-
@bearer said in Toggle gpio with external trigger on/off/on:
Then i suspect you should use sensors.gpOut[2] instead og sensors.gpIn[2] but you can test with m409.
Thanks - I've ran out of time today but will have a go next time I am in. I actually need to change from the e1 and e0temp pins I'm currently using as I've noticed they toggle on briefly on start up - although not enough current to turn the relay module on but that could change. I can see the GPIO pins on the duex5 but not on the Duet 2. Are those what you were referring too?
-
Whatever pin you define in M950 as output Pn will need to be addressed as sensors.gpOut[n]
(or maybe one is index'ed from 1 and the other 0, but still need to address it as an output i think)
-
@bearer I think my head is a bit clearer today. I don’t know what I was doing last weekend… I wrote
M42 P3 S0
to turn the pin low / off and
M409 K"state.gpOut[3]"
to check the pin state getting this back.
{"key":"state.gpOut[3]","flags":"","result":{"pwm":0}}
I then wrote M42 P3 S1 so the pin was high / on and queried the state again:
M409 K"state.gpOut[3]"
{"key":"state.gpOut[3]","flags":"","result":{"pwm":1.00}}
I then changed my trigger to
If state.gpOut[3].pwm==0 M42 P3 S1 else state.gpOut[3].pwm==1 M42 P3 S0
Thanks again for pointing me in the right direction. I’m going to place the relevant M42 commands into M10.g and M11.g and simplify the trigger file to refer to those.
-
If state.gpOut[3].pwm==0 M42 P3 S1 else state.gpOut[3].pwm==1 M42 P3 S0
condition in else is obsolete, and more "elegant" way is
If state.gpOut[3].pwm != 0 M42 P3 S0 else M42 P3 S1