I have an MCP23017 switch that requires a value 0-255 to turn on the 8x outputs.
I have 8x inputs that can be configured in a setup menu to allow certain combinations of those outputs to be operated.
For example:
Switch 1 configuration: 00110001 This would be outputs 3,4, and 8 on.
Switch 2 configuration: 11000000
Switch 3 configuration: 11000000
Switch 4 configuration: 10000001
Switch 5 configuration: 00000100
Switch 6 configuration: 11000000 This would be outputs 1 and 2 on.
Switch 7 configuration: 00001000
Switch 8 configuration: 11000000
These are stored in EEPROM as as 8x variables with a value 0-255.
How do I add these 8x switches up, so that I end up with a single binary result of all the switches positions?
You might have more than one switch on, and therefore that particular output will be flagged as on more than once.
Because that BIN as a second "parameter"* is only valid for a function call, more specific, a print() call. You can't just use it randomly somewhere else.
SteveRC2017:
I don't understand how an OR function combines the binary numbers. To my simple mind, that would be an AND function?
If I tell you to turn to eat an apple if you're at home OR on vacation, will you eat an apple if you're at home but not on vacation?
And when I tell you to turn to eat an apple if you're at home AND on vacation, will you eat an apple if you're at home but not on vacation?
Same here, you want a 1 in the final variable if one of the others has a 1 there, not if they all have a 1 there.
SteveRC2017:
I assume this is 'comparing' then, not adding?
No, it's OR'ing them And in words I think (bitwise) "adding" is the most fitting but a bit ambiguous.
It's a bit more complex then that. Brackets here don't indicate a function call but just a group which is evaluated before the rest is done, same as in math. The comma is a bit of a weird operator in normal lines of code (aka, NOT in a function call). It tells the compiler to take the first operand, evaluate it, throw away the result, and go on with the second operand. Aka, here the whole 'Inputx_assign' is just thrown out.