B4R Question Read pins into a byte

jimseng

Active Member
Licensed User
Longtime User
hi
This should be really easy but i can't work it out.
How do I read 4 pins into a byte?
Something along these lines maybe?
B4X:
dim s as byte
For i = 0 To 3
    Dim f As Boolean
    f = rly(i).DigitalRead
    If f = True Then
        'some part of  byte s = 1
        Else
        'some part of  byte s = 0
            End If
next
 

JordiCP

Expert
Licensed User
Longtime User
Try it. Both s or s2 should give the same result

B4X:
dim s as byte =0
Dim s2 as byte=0
dim mask as int=1
For i = 0 To 3
    Dim f As Boolean
    f = rly(i).DigitalRead
    If f = True Then  'some part of  byte s = 1
         'This will work
         s=Bit.Or(s, Bit.ShifLeft(1,i))    ' pins 0 to 3 will be in bits 0 (weight=1) to 3 (weight=8)

         'This is basically the same
         s2 = s2 + mask
     Else
        'some part of  byte s = 0 --> just do nothing
     End If
     mask=mask*2
next
Log("Result is "&s)
Log("Result is "&s2)
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
thankyou. I realised I should have posted this in the B4R forum, but it is working for me now.
 
Upvote 0
Top