Thanks for the replies all, but I haven't managed to concatenate the numbers using any of the code above.
Basically, this is my situation. I have a rotary encoder with the following outputs (all LVTTL on / off):
Bit1
Bit2
Bit3
Bit4
Strobe
The encoder is in ONE of 8 positions and depending on its current position, it outputs a combination of the above. Eg. Position 2 would equal high / low on the following:
Bit1 = 0
Bit2 = 1
Bit3 = 0
Bit4 = 0
Strobe = 1
The encoder is attached to a motor and gearbox in which I'd like to control the position. For example if I wanted to move to position 8 I'd have the variables set at the following values:
RequiredBit1 = 0
RequiredBit2 = 0
RequiredBit3 = 0
RequiredBit4 = 1
RequiredStrobe = 1
I'm then using the following while statement:
while(Bit1 != RequiredBit1 && Bit2 != RequiredBit2 && Bit3 != RequiredBit3 && Bit4 != RequiredBit4 && Strobe != RequiredStrobe) {
SetBit(motordrivepin);
printf("running");
CurEncoderValues();
}
printf("done");
ClearBit(motordrivepin);
}
The problem is with the while statement. I was expecting the motor to kick in, winding the shaft round slowly until position 8 was met (basically until the 5 conditions all equal TRUE). Instead the code jumps over the while and prints done every time.
Exploring the issue further I believe the logical && operator is causing 'short-circuiting'. Basically if condition1 = False then the remaining 4 conditions are not evaluated. Even by using the bitwise & so all conditions are evaluated I'm still having the same problem. I expect in the above while statement ALL conditions have to be TRUE between the & for the code to execute? This would never be the case until the required position is met as a result of turning through all positions between 2 and 8 for example.
Anyway, that leads me to this concatenation post. I thought if I concatenate all 5 variable values into one variable I wouldn't have this issue. For example:
intCurrentConcat = 01001 //position 2
intRequiredConcat = 00011 //position 8
while(intCurrentConcat != intRequiredConcat) {
SetBit(motordrivepin);
printf("running");
CurEncoderValues();
}
printf("done");
ClearBit(motordrivepin);
}
I hope this helps you understand my requirements and issue a little better. All I need is basically to put 5 integer variables (always either 0 or 1) into a single integer variable.
Could I concatenate the integers as a string and then convert the result back to a single int?
Thanks,