newbee_2:
Yes you are absolutely right though its reads corrects value some times but once its looses its sync it does take lot of value to again com its actual position. So what could i do to make to once it looses its sync on next or in 2-3 iteration its sync again.![]()
Reading through Rob's code, if it loses its way, it will/should pick up the next preamble characters so long as the 4-bit boundary is maintained. So if you've got out of sync by 1, 2 or 3 bits one way to potentially make it pick up the trail sooner is to construct a state machine looking for the pattern of bits of the preamble rather than the sequence of nibbles (which wouldn't be found if you are no longer sampling nibbles at the correct bit boundary). Also, presuming there's a bit on every high clock pulse I'm wondering if the marked line below, will mean the first ever bit in the stream is always lost which would throw it out of sync from the start?
byte getNibble() {
byte val = 0;
for (byte bit_count = 0; bit_count < 4; bit_count++) {
while (digitalRead(CLOCK_PIN) == HIGH); // <<< THIS LINE
while (digitalRead(CLOCK_PIN) == LOW);
val <<= 1;
val |= digitalRead(DATA_PIN);
}
return (val &= 0x0F);
}
You do say it works, so I presume I'm wrong on that point.
Geoff