Convert switch state to binary and add seperate array

HI all,
ive been working on this for a while now, im trying to control a can bus module. pulling data out fine and the rest is working , im not stuck on getting the control state out.
i have 6 switches, each is initally set to zero, then as i switch them on i need to create a value from 0-64, this will be the enable byte that im going to send,
attached to each channel switch is a dropdown menu with set pwm values 100% ,75% etc to -100%
all needs to end up in a string or json data as follows

PWM_1,PWM_2,PWM_3,PWM_4, PWM_5,PWM_6,SwitchState

[{"id":"6c583e35.133e38","type":"ui_switch","z":"79f6369c.f3dc3","name":"","label":"Ch 1","tooltip":"Channel 1","group":"f1a707e.73993f8","order":1,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"ch1","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":490,"y":220,"wires":[["947928cf.3b083"]]},{"id":"8d27ca2a.2b642","type":"ui_switch","z":"79f6369c.f3dc3","name":"","label":"Ch 2","tooltip":"Channel 2","group":"f1a707e.73993f8","order":2,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"ch2","style":"","onvalue":"2","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":490,"y":280,"wires":[["947928cf.3b083"]]},{"id":"48a54048.b8034","type":"ui_switch","z":"79f6369c.f3dc3","name":"","label":"Ch 3","tooltip":"Channel 3","group":"f1a707e.73993f8","order":3,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"ch3","style":"","onvalue":"4","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":490,"y":340,"wires":[["947928cf.3b083"]]},{"id":"8e23c034.d169","type":"ui_switch","z":"79f6369c.f3dc3","name":"","label":"Ch 4","tooltip":"Channel 4","group":"f1a707e.73993f8","order":4,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"ch4","style":"","onvalue":"8","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":490,"y":400,"wires":[["947928cf.3b083"]]},{"id":"1363605.1bb712","type":"ui_switch","z":"79f6369c.f3dc3","name":"","label":"Ch 5","tooltip":"Channel 5","group":"f1a707e.73993f8","order":5,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"ch5","style":"","onvalue":"16","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":490,"y":460,"wires":[["947928cf.3b083"]]},{"id":"5ad99436.70ec64","type":"ui_switch","z":"79f6369c.f3dc3","name":"","label":"Ch 6","tooltip":"Channel 6","group":"f1a707e.73993f8","order":6,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"ch6","style":"","onvalue":"32","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":490,"y":520,"wires":[["947928cf.3b083"]]},{"id":"24fbcc45.762d5c","type":"debug","z":"79f6369c.f3dc3","name":"ch1-6","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":340,"wires":[]},{"id":"9a16e82d.6f2bb","type":"inject","z":"79f6369c.f3dc3","name":"Intialise off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":".5","topic":"","payload":"0","payloadType":"num","x":210,"y":400,"wires":[["6c583e35.133e38","8d27ca2a.2b642","48a54048.b8034","8e23c034.d169","1363605.1bb712","5ad99436.70ec64","5938c637.0f69c8"]]},{"id":"947928cf.3b083","type":"join","z":"79f6369c.f3dc3","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"0","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":710,"y":360,"wires":[["ede7d9d4.61c3e","24fbcc45.762d5c"]]},{"id":"302d856a.d754aa","type":"debug","z":"79f6369c.f3dc3","name":"PWm","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":480,"wires":[]},{"id":"ede7d9d4.61c3e","type":"function","z":"79f6369c.f3dc3","name":"Ch1-6 Byte 8 ","func":"var number_msg = {payload :msg.payload.ch1 + msg.payload.ch2 + msg.payload.ch3 + msg.payload.ch4 + msg.payload.ch5 + msg.payload.ch6};\nmsg.payload = number_msg;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":280,"wires":[["3713c4f4.6597e4","e989178e.2843f"]]},{"id":"2a7283ae.b37354","type":"ui_dropdown","z":"79f6369c.f3dc3","name":"","label":"Ch 1 PWM","tooltip":"","place":"PWM","group":"1ad02d1e.bf7033","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"100 %","value":127,"type":"num"},{"label":"75%","value":95,"type":"num"},{"label":"50%","value":64,"type":"num"},{"label":"25%","value":32,"type":"num"},{"label":"0%","value":0,"type":"num"},{"label":"-25%","value":224,"type":"num"},{"label":"-50%","value":192,"type":"num"},{"label":"-75%","value":160,"type":"num"},{"label":"-100%","value":128,"type":"num"}],"payload":"","topic":"pwm_1","x":1050,"y":400,"wires":[["80b2b188.e7458"]]},{"id":"afd62687.b9aa68","type":"ui_dropdown","z":"79f6369c.f3dc3","name":"","label":"Ch 2 PWM","tooltip":"","place":"PWM","group":"1ad02d1e.bf7033","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"100 %","value":127,"type":"num"},{"label":"75%","value":95,"type":"num"},{"label":"50%","value":64,"type":"num"},{"label":"25%","value":32,"type":"num"},{"label":"0%","value":0,"type":"num"},{"label":"-25%","value":224,"type":"num"},{"label":"-50%","value":192,"type":"num"},{"label":"-75%","value":160,"type":"num"},{"label":"-100%","value":128,"type":"num"}],"payload":"","topic":"pwm_2","x":1050,"y":440,"wires":[["80b2b188.e7458"]]},{"id":"cfa8710d.7e29d","type":"ui_dropdown","z":"79f6369c.f3dc3","name":"","label":"Ch 3 PWM","tooltip":"","place":"PWM","group":"1ad02d1e.bf7033","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"100 %","value":127,"type":"num"},{"label":"75%","value":95,"type":"num"},{"label":"50%","value":64,"type":"num"},{"label":"25%","value":32,"type":"num"},{"label":"0%","value":0,"type":"num"},{"label":"-25%","value":224,"type":"num"},{"label":"-50%","value":192,"type":"num"},{"label":"-75%","value":160,"type":"num"},{"label":"-100%","value":128,"type":"num"}],"payload":"","topic":"pwm_3","x":1050,"y":480,"wires":[["80b2b188.e7458"]]},{"id":"5af47f11.8a5df8","type":"ui_dropdown","z":"79f6369c.f3dc3","name":"","label":"Ch 4 PWM","tooltip":"","place":"PWM","group":"1ad02d1e.bf7033","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"100 %","value":127,"type":"num"},{"label":"75%","value":95,"type":"num"},{"label":"50%","value":64,"type":"num"},{"label":"25%","value":32,"type":"num"},{"label":"0%","value":0,"type":"num"},{"label":"-25%","value":224,"type":"num"},{"label":"-50%","value":192,"type":"num"},{"label":"-75%","value":160,"type":"num"},{"label":"-100%","value":128,"type":"num"}],"payload":"","topic":"pwm_4","x":1050,"y":520,"wires":[["80b2b188.e7458"]]},{"id":"41542926.6576e","type":"ui_dropdown","z":"79f6369c.f3dc3","name":"","label":"Ch 5 PWM","tooltip":"","place":"PWM","group":"1ad02d1e.bf7033","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"100 %","value":127,"type":"num"},{"label":"75%","value":95,"type":"num"},{"label":"50%","value":64,"type":"num"},{"label":"25%","value":32,"type":"num"},{"label":"0%","value":0,"type":"num"},{"label":"-25%","value":224,"type":"num"},{"label":"-50%","value":192,"type":"num"},{"label":"-75%","value":160,"type":"num"},{"label":"-100%","value":128,"type":"num"}],"payload":"","topic":"pwm_5","x":1050,"y":560,"wires":[["80b2b188.e7458"]]},{"id":"4ddc9a90.a54f94","type":"ui_dropdown","z":"79f6369c.f3dc3","name":"","label":"Ch 6 PWM","tooltip":"","place":"PWM","group":"1ad02d1e.bf7033","order":6,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"100 %","value":127,"type":"num"},{"label":"75%","value":95,"type":"num"},{"label":"50%","value":64,"type":"num"},{"label":"25%","value":32,"type":"num"},{"label":"0%","value":0,"type":"num"},{"label":"-25%","value":224,"type":"num"},{"label":"-50%","value":192,"type":"num"},{"label":"-75%","value":160,"type":"num"},{"label":"-100%","value":128,"type":"num"}],"payload":"","topic":"pwm_6","x":1050,"y":600,"wires":[["80b2b188.e7458"]]},{"id":"5938c637.0f69c8","type":"link out","z":"79f6369c.f3dc3","name":"","links":["d0dec74f.022308","9a77ef0c.ed8f1"],"x":355,"y":140,"wires":[]},{"id":"d0dec74f.022308","type":"link in","z":"79f6369c.f3dc3","name":"Reset","links":["5938c637.0f69c8"],"x":865,"y":460,"wires":[["2a7283ae.b37354","afd62687.b9aa68","cfa8710d.7e29d","5af47f11.8a5df8","41542926.6576e","4ddc9a90.a54f94"]]},{"id":"80b2b188.e7458","type":"join","z":"79f6369c.f3dc3","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"1","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1250,"y":480,"wires":[["302d856a.d754aa","5abd88d.25f92f8"]]},{"id":"5abd88d.25f92f8","type":"function","z":"79f6369c.f3dc3","name":"","func":"msg={ payload:[msg.payload.pwm_1,msg.payload.pwm_2,msg.payload.pwm_3,\n        msg.payload.pwm_3,msg.payload.pwm_4,msg.payload.pwm_5,msg.payload.pwm_6,    \n        msg.payload.payload]};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1340,"y":280,"wires":[["53d2abd9.f296d4"]]},{"id":"cfc3b842.d8cff8","type":"debug","z":"79f6369c.f3dc3","name":"nuber","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1160,"y":160,"wires":[]},{"id":"e0560c.569d31f8","type":"comment","z":"79f6369c.f3dc3","name":"Please read","info":"here i need the value of ch1-6 summed,\nand the pwm values in an array \ne.g.\n[ch1 PWM, ch2 PWM, ch3 PWM, ch4 PWM, ch5 PWM , ch6 PWM, summed ch1-6]\nim then goting to trasmit this over serial to an arduino\npayload.payload","x":1580,"y":260,"wires":[]},{"id":"53d2abd9.f296d4","type":"debug","z":"79f6369c.f3dc3","name":"Test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1510,"y":300,"wires":[]},{"id":"3713c4f4.6597e4","type":"change","z":"79f6369c.f3dc3","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":280,"wires":[["5abd88d.25f92f8","cfc3b842.d8cff8"]]},{"id":"e989178e.2843f","type":"function","z":"79f6369c.f3dc3","name":"","func":"msg = msg.payload.topic\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":970,"y":120,"wires":[[]]},{"id":"f1a707e.73993f8","type":"ui_group","name":"Channel Switch","tab":"4eea1a15.096744","order":1,"disp":true,"width":"3","collapse":false},{"id":"1ad02d1e.bf7033","type":"ui_group","name":"PWM setting","tab":"4eea1a15.096744","order":2,"disp":true,"width":"6","collapse":false},{"id":"4eea1a15.096744","type":"ui_tab","name":"Operate Unit","icon":"forward","order":5,"disabled":false,"hidden":false}]

ground to a halt, searched the froum and found bits but cant get it to work, any help would be gratfully recieved.
Mark

try this function...


//Update Bit
function updateBit(number, bitPosition, bitValue) {
  const bitValueNormalized = bitValue ? 1 : 0;
  const clearMask = ~(1 << bitPosition);
  return (number & clearMask) | (bitValueNormalized << bitPosition);
}

var result = 0;

result = updateBit(result, 0, msg.payload.pwm_1 );
result = updateBit(result, 1, msg.payload.pwm_2 );
result = updateBit(result, 2, msg.payload.pwm_3 );
result = updateBit(result, 3, msg.payload.pwm_4 );
result = updateBit(result, 4, msg.payload.pwm_5 );
result = updateBit(result, 5, msg.payload.pwm_6 );

msg.payload = result;
return msg;        

NOTE: this assumes msg.payload.pwm_x is a bool(ish) value. e,g, true/false 1/0 type value. If not, then you might need to parse the bitValue function parameter differently

2 Likes

@Steve-Mcl
Thanks, just tried that a lot neater than what i was trying to do.
Just got to sort the final array out now, still plodding if i solve it ill post it.
thanks again

ok sussed it out, so i thought id post what i did so anyone else who needs it can.
Used the code from @Steve-Mcl , ( i did change the msg.payload.pwm_1 etc to msg.payload.ch1 etc as its the switches i need the value from) and added in

msg.payload = result;
msg.topic = "switch";
return msg; 

then i used a join node with the 6 dropdown boxes each with a topic of the PWM channel, set to a key/value Object with msg.topic as the key and got the data i was after

{"pwm_1":0,"pwm_2":0,"pwm_3":0,"pwm_4":0,"pwm_5":0,"pwm_6":0,"switch":0}
so now i can send that to my arduino and pull the info out.

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