//B.Vandeportaele 2018 #define PIN_RX_IR 8 //////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////// Function to print in hexadecimal with a known number of digits void printHex(int num, int precision) { char tmp[16]; char format[128]; sprintf(format, "%%.%dX", precision); sprintf(tmp, format, num); Serial.print(tmp); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// inline bool DetecteBas() { return (digitalRead(PIN_RX_IR) == 0); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// inline bool DetecteHaut() { return (digitalRead(PIN_RX_IR) != 0); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { pinMode(PIN_RX_IR, INPUT); digitalWrite(PIN_RX_IR, LOW); Serial.begin(9600); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// unsigned int temps = 0; unsigned int temps2 = 0; unsigned int deltat = 0; unsigned char etat = 0; unsigned char cptbit = 0; unsigned char bytes[4]; //////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { switch (etat) { case 0: if (DetecteBas()) { temps = micros(); etat = 1; } break; case 1: if (DetecteHaut()) { temps2 = micros(); deltat = temps2 - temps; temps = temps2; if ( (deltat > 8000) && (deltat < 9000)) { etat = 2; } else etat = 0; } break; case 2: if (DetecteBas()) { temps2 = micros(); deltat = temps2 - temps; temps = temps2; if ( (deltat > 4000) && (deltat < 5000)) { etat = 3; cptbit = 0; bytes[0] = 0; bytes[1] = 0; bytes[2] = 0; bytes[3] = 0; } else if ( (deltat > 2000) && (deltat < 2500)) { etat = 0; Serial.print("+"); } break; case 3: if (DetecteHaut()) { temps2 = micros(); deltat = temps2 - temps; temps = temps2; if (cptbit < 32) etat = 4; else { etat = 0; if (bytes[2] == ~bytes[3]) { Serial.print("="); printHex(bytes[0],2); Serial.print(" "); printHex(bytes[1],2); Serial.print(" "); printHex(bytes[2],2); Serial.print(" "); } } } break; case 4: if (DetecteBas() ) { temps2 = micros(); deltat = temps2 - temps; temps = temps2; if (deltat > 800) bytes[cptbit >> 3] |= (1 << (cptbit & 7)); etat = 3; cptbit++; } break; } } }