void myISR( void );
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(2), myISR, FALLING);
}
volatile uint8_t messageReady = 0;
volatile unsigned long elapsedTime;
volatile uint8_t message[4];
void loop() {
// put your main code here, to run repeatedly:
if ( messageReady == 1 ){
messageReady = 0;
for ( int i = 0; i < 4; i++){
Serial.print(message[i], HEX);
Serial.print(" ");
}
Serial.println(" ");
}
}void myISR( void ){
static unsigned long previousMicros = micros();
static uint8_t byteIndex = 0;
static uint8_t bitIndex = 0;
elapsedTime = micros() - previousMicros;
previousMicros = micros();
if ( elapsedTime > 10000 && elapsedTime < 15000){
// Start of message
byteIndex = 0;
bitIndex = 0;
}
if ( elapsedTime > 1000 && elapsedTime < 1500 ){
// We have a zero
bitWrite( message[byteIndex], bitIndex, 0);
bitIndex++;
if (bitIndex > 7){
byteIndex++;
bitIndex = 0;
}
}
if ( elapsedTime > 2000 && elapsedTime < 2500 ){
// We have a one
bitWrite( message[byteIndex], bitIndex, 1);
bitIndex++;
if (bitIndex > 7){
byteIndex++;
bitIndex = 0;
}
}
if ( byteIndex > 3 ){
// We have the entire message
messageReady = 1;
byteIndex = 0;
}
}