 void copyFromBuffer(int cyclicBufferPosition);
 void dumpCardToSerial();
 
+
+unsigned int toneDisableTime;
+int toneEnabled = 0;
+
+int lastMs = 0;
+
 void loop() {
+
+		if(eventReceived and (millis() - lastSerialEventTime >= 20) ){
+			eventReceived = false;
+			cyclicBufferPosition = 0;
+			if(isDoorLocked)
+{
+				copyFromBuffer(cyclicBufferPosition);
+				dumpCardToSerial();
+				processCardNumber();
+			}
+		}
+
+if (millis() != lastMs)
+{
+  lastMs = millis();
+  if (toneDisableTime)
+  {
+    toneDisableTime--;
+  }
+  else
+  {
+    noTone(pinPiezo);
+  }
+}
+
+
 		bool button = digitalRead(pinButtonSwitch);
 		if(button != previousButtonState){
 			buttonEvent = millis();

	}
#endif

void skipSerialBuffer(){
	while(Serial.available()){
		Serial.read();
	}

		Serial.print("VALID\n");
	#endif
	if(isCardAuthorized()){
		tone(pinPiezo, toneAccepted);
                toneDisableTime = toneDuration;
toneEnabled = 1;		
unlockDoor();
		//cleanBuffer();
		reportOpened();
		return;
	}else{
		tone(pinPiezo, toneRejected);
toneDisableTime = toneDuration;
toneEnabled = 1;
	}
}

