 
   // check if it's time to read data and update the filter
   microsNow = micros();
-  if (microsNow - microsPrevious >= microsPerReading) {
-    float difference = measureYaw() - 180;
-    doorAngle = abs(difference);
-
-    if (doorAngle < openAngle) {
+  if (microsNow - microsPrevious >= microsPerReading) { // This condition is taken from the tutorial code -- it ensures updates happen at the right time
+    float difference = measureYaw() - 180; // Normalize the measurement so that it starts from 0, since the initial angle is 180
+    doorAngle = abs(difference); // Note that the abs function cannot have other operations inside of it.  This is apparently a problem with Arduino.  This line and the previous one have to be two separate operations, or this will bug up.
+
+    if (doorAngle < OPEN_ANGLE) { // Door is closed
       closeDoor();
       Serial.println("Door not open");
-    } else if (microsNow - microsLastRead < microsBetweenReads) {
+    } else if (microsNow - microsLastRead < microsBetweenReads) { // Door is open, a movement was recently detected, and we are waiting between movements; do not double count or re-record the last passing time because the person may still be passing through.
       Serial.println("Movement Detected");
-    } else {
+    } else { // Door is open, but a complete movement has not been discovered recently.
       openDoor();
       ping_1 = sonar_1.ping_cm();
       ping_2 = sonar_2.ping_cm();
-  
+
+      // This block and the next have similar logic.  When we detect a ping, we adjust population and reset the latest detection times if the other sensor's last detection time is recent.  Otherwise, we update the current sensor's last detection.
       if (detect(ping_2)) {
-        if (closeTrigger > 0) {
+        if (closeLastTime > 0) {
           incrementPopulation();
         } else if (!detect(ping_1)) {
-          farTrigger = microsNow;
+          farLastTime = microsNow;
         }
       } else if (detect(ping_1)) {
-        if (farTrigger > 0) {
+        if (farLastTime > 0) {
           decrementPopulation();
         } else if (!detect(ping_2)) {
-          closeTrigger = microsNow;
+          closeLastTime = microsNow;
         }
-      } else {
-        if (farTrigger > 0 && microsNow - farTrigger > 2 * microsBetweenReads) {
-          farTrigger = 0;
-        } else if (closeTrigger > 0 && microsNow - closeTrigger > 2 * microsBetweenReads) {
-          closeTrigger = 0;
+      } else { // if a significant amount of time has passed (defined as 2 * microsBetweenReads), then reset the last detection times.  This is to prevent detections from a long time ago priming the detector when it doesn't make sense.
+        if (farLastTime > 0 && microsNow - farLastTime > 2 * microsBetweenReads) {
+          farLastTime = 0;
+        } else if (closeLastTime > 0 && microsNow - closeLastTime > 2 * microsBetweenReads) {
+          closeLastTime = 0;
         }
       }
       printinfo(doorAngle, ping_1, ping_2);

}

void decrementPopulation() {
  if (population > 0) {
    population--;
  }
  updatePopulationAndMoveCursor();
  lcd.write("Have a nice day!");
}

void openDoor() {
  if (!doorOpen) {
    lcd.setRGB(0, 255, 0);
    doorOpen = true;
  }
}

void closeDoor() {
  if (doorOpen) {
    lcd.setRGB(255, 255, 255);
    doorOpen = false;

  return a;
}

// from tutorial
float convertRawGyro(int gRaw) {
  // since we are using 250 degrees/seconds range
  // -250 maps to a raw value of -32768
