-
Concatenation
Hi,
I have a trivial problem, that i have been wracking my brains over for the last week. Getting dumber as i get older :frown:
The idea is that a sequence of infra red LED's gets turned on in relation to the binary count on PortA. This also turns on an associated detector.
Whatever the output of the detector, it gets written to the variable DataIn2.
So, after a full 8 bits the data in DataIn2 should look like 10010110, in this case the beam has been interrupted at positions 1, 4, 6, & 7.
The code below does this, but obviously knocks of the first bit, due to it moving the bit to the left to concatenate the variable - so the example now looks like 00010110. How do i get the complete 8 bits into the variable?
Code:
BinaryCount: 'Advances LED and detector 1 place
PortA = BinCount 'Pushes Binary value of BinCount onto PortA
Pause 10 'Pause to let detectors start up
If PortB.0 = 1 Then 'monitor PortE.2 for beam break
DataIn2 = DataIn2 + 1 'something blocking beam, write a 1 to DataIn2 variable
DataIn2 = DataIn2<<1 'Move bit one place to left to concatinate variable
Else
DataIn2 = DataIn2 + 0 'Beam clear, write a 0 to DataIn2 variable
DataIn2 = DataIn2<<1
EndIf
If BinCount = 7 Then
BinCount = 0 'Reset when we get to end of LED's
Gosub Display
Else
BinCount = BinCount + 1
EndIf
Return
-
Re: Concatenation
Code:
If PortB.0 = 1 Then 'monitor PortE.2 for beam break
DataIn2 = DataIn2<<1 'Move bit one place to left to concatinate variable
DataIn2 = DataIn2 + 1 'something blocking beam, write a 1 to DataIn2 variable
Else
DataIn2 = DataIn2<<1
DataIn2 = DataIn2 + 0 'Beam clear, write a 0 to DataIn2 variable
EndIf
How about doing it this way?
-
Re: Concatenation
Hi,
Adding to (or removing from actually...) Jersons example, this should let you get rid of the IF/THEN construct as well
Code:
DataIn2 = (DataIn2 << 1) + PortB.0 ' Move bits one step to the left and add state of PortB.0 to the least significant bit.
/Henrik.
-
Re: Concatenation
Many thanks Jerson, just swapping the lines over did the trick. I really sacrificed at the alter of stupidity this week!
HenrikOlsson, i did not know you could do this. Should save some space and speed things up.
Many thanks to both of you for your time, really saved my sanity.
-
Re: Concatenation
You're welcome. We have all been there(the altar of stupidity) at some time in our lives, haven't we?
-
Re: Concatenation
Hi Jerson / HenrikOlsson,
Not sure what happened to my spelling this week either. 'alter' - what is wrong with me! Maybe i should get checked for early onset Alzheimers - ah, maybe not, i can spell Alzheimers!
Having looked through my code, I've realised (English spelling), that concatenation is spelled incorrectly as well, argghhh! Maybe it's the wine - surely not - it can't be the wine - oh god , not the wine!!
P.S. HenrikOlsson, thanks again, that one line of code is beautiful - did i spell that correctly, phew!
-
Re: Concatenation
I thought it was spelled Oldtimer's.... Everyone needs a laugh
-
Re: Concatenation
Hey, steady on, I'm not that old. Oh, actually I might be.
Nurse, quick, the screens! :tongue: