J jimseng Active Member Licensed User Longtime User May 23, 2016 #1 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
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
Erel B4X founder Staff member Licensed User Longtime User May 23, 2016 #2 Store the pins in an array of pins. Code: If Buffer.Length <> Pins.Length Then Log("Invalid data") Return End If For i = 0 To Buffer.Length - 1 Pins(i).DigitalWrite(Data(i) = 1) Next Upvote 0
Store the pins in an array of pins. Code: If Buffer.Length <> Pins.Length Then Log("Invalid data") Return End If For i = 0 To Buffer.Length - 1 Pins(i).DigitalWrite(Data(i) = 1) Next
J jimseng Active Member Licensed User Longtime User May 23, 2016 #3 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
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?
J jimseng Active Member Licensed User Longtime User May 23, 2016 #4 Sorry. I should have said that it doesn't seem to be working. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User May 24, 2016 #5 Please use [code]code here...[/code] tags when posting code. There is a very big difference between: Code: data = Array As Byte (1, 0, 1, 0) And: Code: 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: Code: rly(i).DigitalWrite(data(i) = Asc("1")) jimseng said: I am a bit confused as the digitalwrite seems to be a boolean value? Click to expand... That's true. data(i) = Asc("1") returns a boolean value. Upvote 0
Please use [code]code here...[/code] tags when posting code. There is a very big difference between: Code: data = Array As Byte (1, 0, 1, 0) And: Code: 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: Code: rly(i).DigitalWrite(data(i) = Asc("1")) jimseng said: I am a bit confused as the digitalwrite seems to be a boolean value? Click to expand... That's true. data(i) = Asc("1") returns a boolean value.
J jimseng Active Member Licensed User Longtime User May 24, 2016 #6 Thanks Erel. That is really helpful. Upvote 0