Reading an input to later duplicate it at the output

I’m attempting to read the output of an unidentified IC and reproduce it using arduino. There’s no clock and the data looks to be encoded by the duration of pulses, (long for 0, short for 1 or vice versa). The output changes on two intervals, about 516 and 168 microseconds. The signal could be low or high for the period of a short or long pulse. I think it’s some kind of frequency shift keying. In any case, I don’t need to interpret the data, just duplicate it.

I’m not having much luck reading the signal. My programming skills are not very good, this is all I’ve got:

// Read an input and duplicate it without necessarily understanding it
// Digital pin 2 input, 6 and 7 output
// signal duration ~17ms
// Short pulse ~168us, long pulse ~516us

bool state;
bool lastState;
unsigned long changeTime = 0;
unsigned long microTime = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(112500);
  pinMode(2, INPUT);
  pinMode(6, OUTPUT);     // trigger 1
  pinMode(7, OUTPUT);     // trigger 2
}

void loop() {
  state = 0;
  changeTime = 0;
  lastState = 0;
  digitalWrite(6, HIGH);                 // trigger button 1
  lastMillis = millis();
  microTime = micros();
  while (millis() - lastMillis < 18) {   // capture sample
    state = digitalRead(2);
    changeTime = micros() - microTime;
    while (state != lastState) {    // detect change
      if (changeTime > 175) {
        Serial.print("1");
      } else {
        Serial.print("0");
      }
      changeTime = 0;
      microTime = micros();
      lastState = state;
    }
  }
  digitalWrite(6, LOW);
  Serial.println();
  delay(2500);
}

I’m triggering the signal to begin by writing 6 high, then I look for a change in the state of the signal. If that time is greater than the midpoint it’s output as 1 and vice versa. Seems fool proof to me, but I’m not getting consistent data. I checked the time it takes to run through the loop and its no more than 12 microsecond. There should be plenty of time for the input to catch even the short one. I’ve got it up on my scope and everything looks as it should, so I’m sure it’s a software issue.

Update: fixed some code issues, still not perfect data, but more consistent:

// Read an FSK input and duplicate it without necessarely undertanding it
// Digital pin 2 input, 6 and 7 output (b1 on/off)
// Pulse duration ~17ms
// Short pulse ~168us, long pulse ~516us

bool state;
bool lastState;
unsigned long changeTime = 0;
unsigned long microTime = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(112500);
  pinMode(2, INPUT);
  pinMode(6, OUTPUT);     // trigger 1
  pinMode(7, OUTPUT);     // trigger 2
}

void loop() {
  state = 0;
  changeTime = 0;
  lastState = 0;
  digitalWrite(6, HIGH);                 // trigger button 1
  delay(3);
  microTime = micros();
  while (changeTime < 800) {   // capture sample
    state = digitalRead(2);
    changeTime = micros() - microTime;
    if (state != lastState) {    // detect change
    // Serial.print(changeTime);
      if (changeTime > 175) {
        Serial.print("1,");
      } else {
        Serial.print("0,");
      }
      changeTime = 0;
      microTime = micros();  
    }
    lastState = state;
  }
  digitalWrite(6, LOW);
  Serial.println();
  delay(2500);
}

Of 50 samples, some bits are almost perfectly random, others are perfectly consistent

I dont understand this line

while (millis() - lastMillis < 18)

but here is a suggestion
make the output of the chip go to an input on the arduino and use pin change interrupts and time stamp when the interrupt is fired using an array of long unsigned ints i.e. time*=micros() i is a counter and will increment each time the interrupt is run. So at the end you have an array that tells you the time between the ic output changing.*

Figured it out, final sketch:

// Read an FSK input and duplicate it without necessarely undertanding it
// Digital pin 2 input, 6 and 7 output (b1 on/off)
// Pulse duration ~17ms
// Short pulse ~168us, long pulse ~516us

bool state;
bool lastState;
unsigned long changeTime = 0;
unsigned long microTime = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(112500);
  pinMode(2, INPUT);
  pinMode(6, OUTPUT);     // trigger 1
  pinMode(7, OUTPUT);     // trigger 2
  Serial.println("Start");
}

void loop() {
  state = 0;
  changeTime = 0;
  lastState = 0;
//  Serial.println("Data:");
  digitalWrite(6, HIGH);                 // trigger button 1
  delay(3);
  microTime = micros();
  while (changeTime < 600) {   // capture sample
    state = digitalRead(2);
    changeTime = micros() - microTime;
    if (state != lastState && changeTime > 100) {    // detect change
  //    Serial.print(changeTime);
      if (changeTime > 342) {
        Serial.print("1");
        microTime = micros();
      } else {
        Serial.print("0");
        microTime = micros();
      }
    }
    lastState = state;
  }
  digitalWrite(6, LOW);
  Serial.println();
  delay(500);
}

The middle time I used to compare the short and long changes was too close to the small one because of a math error. Few other minor errors as well

noweare:
I dont understand this line

while (millis() - lastMillis < 18)

Executes the code in the associated code block for 18 milliseconds