     Serial.println();
     Serial.println();
     
-    byteCount = 0;
+    atomicSet(&byteCount, 0);
   }
+}
+
+int atomicGet(volatile int* ptr) {
+  noInterrupts();
+  int i = *ptr;
+  interrupts();
+  return i;
+}
+
+void atomicSet(volatile int* ptr, int val) {
+  noInterrupts();
+  *ptr = val;
+  interrupts();
 }
 
 void receive() {

}

void loop() {
  if (byteCount > 0) {
    // Print data length
    Serial.print("Packet received: ");
    Serial.print(byteCount);
    Serial.println(" bytes");
    
    // Print data in hexadecimal format
    for (int i = 0; i < byteCount; i++) {
      char hex[4];
      sprintf(hex, "%02X ", buf[i]);
      Serial.print(hex);

/**
 * Receive raw data via interrupt using the MRF24J40 module.
 *
 * The INT pin from MRF24J40 must be connected to the INT1 pin in the ATmega328 (Arduino D3 pin).
 *
 * Copyright (c) 2015 Circuitar
 * All rights reserved.
