How to add 2 inputs every second

I have 2 variables that are constantly incoming from a ADC and I rate limited them to 1/sec each, I want to combine them (one is positive, one negative) and have the output be the difference between them. I think this can be done but the problem I am having is I will get 2 of one input sometimes and then my output is thrown off, how can I make sure my calculation takes one reading of each and outputs the result?

Is this two ADC or one ? Messages never arrive together , you will need a join node.

uhm something like this?
for every input, you always get a msg with the latest values, then substract them

[{"id":"602aab9435fee132","type":"inject","z":"1160bb977c192b32","name":"adc1","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"parts.index","v":"0","vt":"num"},{"p":"parts.count","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc1","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":370,"y":240,"wires":[["eb26a3852d0dc2e9"]]},{"id":"eb26a3852d0dc2e9","type":"join","z":"1160bb977c192b32","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":550,"y":260,"wires":[["34721846c797337f","7afd33ab1c1b369e","4a1d2095d2df1902"]]},{"id":"c4539199ae85d2ef","type":"inject","z":"1160bb977c192b32","name":"adc2","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"parts.index","v":"1","vt":"num"},{"p":"parts.count","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc2","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":370,"y":280,"wires":[["eb26a3852d0dc2e9"]]},{"id":"34721846c797337f","type":"function","z":"1160bb977c192b32","name":"substract","func":"const x = msg.payload.adc1 - msg.payload.adc2\nmsg.raw_data = msg.payload\nmsg.payload = x\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":260,"wires":[["0e07a7c884f3a839"]]},{"id":"0e07a7c884f3a839","type":"debug","z":"1160bb977c192b32","name":"debug 77","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":260,"wires":[]}]

If you set, in the Join node, After a Number of Message Parts to 2 then you don't need to setup msg.parts in the inject nodes.

[{"id":"602aab9435fee132","type":"inject","z":"bdd7be38.d3b55","name":"adc1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc1","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":310,"y":2920,"wires":[["eb26a3852d0dc2e9"]]},{"id":"eb26a3852d0dc2e9","type":"join","z":"bdd7be38.d3b55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":490,"y":2940,"wires":[["34721846c797337f"]]},{"id":"c4539199ae85d2ef","type":"inject","z":"bdd7be38.d3b55","name":"adc2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"adc2","payload":"$floor($random() * 10)\t\t","payloadType":"jsonata","x":310,"y":2960,"wires":[["eb26a3852d0dc2e9"]]},{"id":"34721846c797337f","type":"function","z":"bdd7be38.d3b55","name":"substract","func":"const x = msg.payload.adc1 - msg.payload.adc2\nmsg.raw_data = msg.payload\nmsg.payload = x\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":2940,"wires":[["0e07a7c884f3a839"]]},{"id":"0e07a7c884f3a839","type":"debug","z":"bdd7be38.d3b55","name":"debug 77","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":820,"y":2940,"wires":[]}]

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