Page 1 of 1

input port bit slect using variable name

Posted: Tue Aug 02, 2022 11:04 am
by Maverick
Hi
Is there a way you can read the value of a port bit using a variable name to select the port bit used.

Re: input port bit slect using variable name

Posted: Tue Aug 02, 2022 7:41 pm
by medelec35
Hello.
The easiest way would be to use the switch icon.
Then you can add any input on any branch.
If for example, the value of the switch variable is 2 the branch which has 2 will be accessed.
That could have an input A2 for example

Re: input port bit slect using variable name

Posted: Tue Aug 02, 2022 8:17 pm
by mnfisher
An alternative technique...

Read the entire port and then shift to get the required bit..
Have something like:
Where .n is the variable holding the bit you want to read and .p is a variable as 'wide' as the port (so byte for 8 bits or long for esp32 (32 bits port)).

Code: Select all

.p = Input() // Read the whole port - click on Advanced options to allow this.
.bit = (.p >> .n)  & 1   // Extract the required bit.
Martin