B4R Question Write binary to four digital output pins?

jimseng

Active Member
Licensed User
Longtime User
Hello
Just starting to try to get my head around b4R
If I have a byte array that contains 1011 (received from asyncstreams via serial port)
What would be the best way of representing that on pins digital output pins 4,5,6 and 7? There must be a more elegant way of setting the pins without a bundle of if statements.

Thanks
 

jimseng

Active Member
Licensed User
Longtime User
ok so I declared the pins
Dim rly(4) As Pin
initialize them:
For i = 0 To rly.Length -1
rly(i).Initialize(i+4,rly(i).MODE_OUTPUT)
Next
then I tried ass per your example:

Dim data(4) As Byte
data ="1010"
For i = 0 To 3
rly(i).DigitalWrite(data(i)=1)
Next

I am a bit confused as the digitalwrite seems to be a boolean value?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

There is a very big difference between:
B4X:
data = Array As Byte (1, 0, 1, 0)
And:
B4X:
data = "1010"
'equivalent to:
data = Array As Byte(48, 49, 48, 49)
'or
data = Array As Byte(Asc("1"), Asc("0"), ...

The best solution is to encode the data as bytes instead of string. If you do want to use string then change the line to:
B4X:
rly(i).DigitalWrite(data(i) = Asc("1"))

I am a bit confused as the digitalwrite seems to be a boolean value?
That's true.

data(i) = Asc("1") returns a boolean value.
 
Upvote 0
Top