Sorry this is the present code
const int CLOCK_PIN = 2;
const int DATA_PIN = 4;
const int PACKET_BITS = 128;
const int PREAMBLE_LEN = 6;
const int DATA_LEN = 26;
enum {
PRE_0, PRE_1, PRE_2, PRE_3, PRE_4, PRE_5, DATA
} states;
byte preamble [] = {0x0C,0x0A,0x0A,0x0C,0x01,0x0F};
char hexAsciiVals[] = "0123456789ABCDEF";
byte state = PRE_0;
byte nibble;
int nibble_count = 0;
void setup() {
pinMode(CLOCK_PIN, INPUT);
pinMode(DATA_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
nibble = getNibble();
nibble_count++;
// Serial.print(digitalRead(CLOCK_PIN));
switch (state) {
case PRE_0:
if (nibble_count > 32) {
// we've read 32 bytes and still not found a match
// for the preamble so skip a clock pulse
while (digitalRead(CLOCK_PIN) == LOW);
while (digitalRead(CLOCK_PIN) == HIGH);
nibble_count = 0;
}
else
{
if(nibble == preamble[0])
state = PRE_1;
else
state = PRE_0;
}
break;
case PRE_1:
if(nibble == preamble[1])
state = PRE_2;
else
state = PRE_0;
break;
case PRE_2:
if(nibble == preamble[2])
state = PRE_3;
else
state = PRE_0;
break;
case PRE_3:
if(nibble == preamble[3])
state = PRE_4;
else
state = PRE_0;
break;
case PRE_4:
if(nibble == preamble[4])
state = PRE_5;
else
state = PRE_0;
break;
case PRE_5:
if(nibble == preamble[5])
state = DATA;
else
state = PRE_0;
break;
case DATA:
Serial.print (hexAsciiVals[nibble]);
if (nibble_count == DATA_LEN) {
// all done, start again
state = PRE_0;
nibble_count = 0;
Serial.println();
}
break;
}
}
byte getNibble() {
byte val = 0;
for (byte bit_count = 0; bit_count < 4; bit_count++) {
while (digitalRead(CLOCK_PIN) == HIGH);
while (digitalRead(CLOCK_PIN) == LOW);
val <<= 1;
val |= digitalRead(DATA_PIN);
}
return (val &= 0x0F);
}