How to group inputs for IF statements?
Chad1
Posts: 26
Hey everyone,
Let's say I have ten switches, and any time one of them is high I want to turn on an LED. Instead of writing something like this in SPIN...
repeat
if (ina[SW1] == 1) or (ina[SW2] == 1) or (ina[SW3] == 1) or ... or (ina[SW10] == 1)
***Turn on LED***
...could I write something like this? (Pseudocode for the sake of clarity)
InputGroup = [SW1, SW2, ... , SW10]
repeat
if InputGroup == 1
***Turn on LED***
Thanks in advance for your help!
Let's say I have ten switches, and any time one of them is high I want to turn on an LED. Instead of writing something like this in SPIN...
repeat
if (ina[SW1] == 1) or (ina[SW2] == 1) or (ina[SW3] == 1) or ... or (ina[SW10] == 1)
***Turn on LED***
...could I write something like this? (Pseudocode for the sake of clarity)
InputGroup = [SW1, SW2, ... , SW10]
repeat
if InputGroup == 1
***Turn on LED***
Thanks in advance for your help!

Comments
repeat if ina[lastswitch..firstswitch] ' turn on LEDTo make code appear how I did it, retaining indentation and such, put it in [noparse] [/noparse].
EDIT:
Here's a more advanced solution that doesn't need contiguous pins and tells you which pins are on:
CON ' let's say we care about inputs on pins 1, 2, 3, 5, 8, 11, 14, and 18; those bits are all set in pinmask pinmask = %00000000_00000100_01001001_00101110 PUB switchmonitor | ins, pinno repeat ins := ina & pinmask ' capture the inputs we care about repeat while ins ' while there are more inputs to process pinno := (>| ins) ' get the id of the highest pin that's on ins &= ! |< pinno ' mark the pin as processed (clear bit pinno of ins) ' pin #pinno is on, do something about it