Reading data from low to high, high to low etc

Hi All

I have a pattern from low to high. When it spikes high i need to record the time it is high and that will tell me if its a "0" or a "1" then it will dip to low and i need to write this so that it will detect and time the time in ms.

When it dips low it will then go high for an x amount of ms and that will be a "0" or "1".

Thanks

When it spikes high i need to record the time it is high

I interpret that as when it the input becomes HIGH then you need to record the time then later, when it becomes LOW you need to record the time again so that you can use the difference to determine whether the pulse represented a 0 or a 1. Is that right ?

Note my use of becomes rather than is. If I am correct then you need to detect the state change times. Have a look at the StateChangeDetection example in the IDE

If you mean something different then please explain in more detail

I tried this quickly to show you one possibility, it compiles but I didn't test it:

#define PIN 2
bool prevState = LOW;
bool currState = LOW;
unsigned long chrono = 0;
unsigned long duration = 0;

void setup() {
  Serial.begin(115200);
  prevState = digitalRead(PIN);
}

void loop() {
  currState = digitalRead(PIN);
  if (currState) {
    if (!prevState) chrono = millis();
  } else {
    if (prevState) {
      duration = millis() - chrono;
      Serial.print ("Duration HIGH = ");
      Serial.print (duration);
      Serial.println(" ms");
    }
  }
  prevState = currState;
}