Reading data at 10 Khz rate

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. :frowning: :frowning:

const int pin2 = 2;
const int pin4 = 4;
int clock = 0;
int clock1 = 0;



void setup()
{
  pinMode(pin2, INPUT);
   
  pinMode(pin4, INPUT);
  
  Serial.begin(2400);
}
void loop()
{
     static byte num_bits  = 0;
     static byte value = 0;

     clock = digitalRead(pin2);
     if ((clock == HIGH) && (clock1 == LOW))
        {   

            
            value = (value << 1) | digitalRead (pin4);
            if (++num_bits >= 8)                          // have we done 8 iterations?  If so print it out
                {
                     Serial.print(value, HEX);
                     Serial.println();
                     num_bits = 0;
                    value = 0;
                }
        }

    clock1= clock;
}