I'm receiving a signal in an arduino and I want to measure the period with which said signal goes HIGH to LOW and vicecersa. I know what this message will be (I wrote it in manchesterEncoded) so I wrote this code
// find carrier wave
unsigned long carrierDuration = pulseIn(RX, HIGH);
if (carrierDuration > 40000) {
// wait for LOW
if (digitalRead(RX) == LOW) {
delayMicroseconds(1000);
//Verifica serial Code
for (int i = 0; i < 18; i++) {
if (manchesterEncoded[i] == 0) {
periods[i] = pulseIn(RX, LOW);
state[i]=0;
} else {
periods[i] = pulseIn(RX, HIGH);
state[i]=1;
}
}
The problem is that when i sand the code 010101010101... I get random times (0.01ms to 10 ms) and not the ones I know are the actual periods (1ms).