 #include <Wire.h>
 #include "rgb_lcd.h"
 
+// parameters indicated with a * should be tuned to fit the door.
+
 // ULTRASONIC
-#define TRIGGER_PIN_1  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
-#define ECHO_PIN_1     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
-#define TRIGGER_PIN_2  10  // Arduino pin tied to trigger pin on the ultrasonic sensor.
-#define ECHO_PIN_2     9  // Arduino pin tied to echo pin on the ultrasonic sensor.
-
-#define DETECTION_THRESH 50 // Distance cutoff for passing
-#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
+#define TRIGGER_PIN_1  12  // Arduino pin tied to trigger pin on the first ultrasonic sensor.
+#define ECHO_PIN_1     11  // Arduino pin tied to echo pin on the first ultrasonic sensor.
+#define TRIGGER_PIN_2  10  // Arduino pin tied to trigger pin on the second ultrasonic sensor.
+#define ECHO_PIN_2     9  // Arduino pin tied to echo pin on the second ultrasonic sensor.
+
+#define DETECTION_THRESH 25 // Distance cutoff for passing (< DETECTION_THRESH is considered to be a person in proximity)
+#define OPEN_ANGLE 20       // Angle where door is considered open
+#define MAX_DISTANCE 200    // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
+boolean doorOpen = false;   // is the door open?
 
 // USE THIS INSTEAD TO REVERSE THE DOOR DIRECTION
-// NewPing sonar_1(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE); // NewPing setup of closer sensor.
-// NewPing sonar_2(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE); // NewPing setup of farther sensor.
-
-NewPing sonar_1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE); // NewPing setup of closer sensor.
-NewPing sonar_2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE); // NewPing setup of farther sensor.
+// NewPing sonar_1(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);  // NewPing setup of closer sensor.
+// NewPing sonar_2(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);  // NewPing setup of farther sensor.
+NewPing sonar_1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);     // NewPing setup of closer sensor.
+NewPing sonar_2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);     // NewPing setup of farther sensor.
 
 // RGB
 rgb_lcd lcd;
 
 // MADGEWICK
+// see https://www.arduino.cc/en/Tutorial/Genuino101CurieIMUOrientationVisualiser
 Madgwick filter;
 unsigned long microsPerReading, microsPrevious, microsBetweenReads, microsLastRead;
 float accelScale, gyroScale;
 
-// THRESHOLDS
-const int thresh = 25;       // < 60 cm to sensor is considered passing through.
-const float openAngle = 20;  // door is considered open at 20 degrees.
-boolean doorOpen = false;
-
 // INDICATOR
-const int ledPin =  13;      // the number of the LED pin
+const int ledPin =  13;      // the number of the on-board LED in the Arduino 101
 
 // POPULATION
-int population = 0;
-unsigned long closeTrigger = 0, farTrigger = 0;
+int population = 0;          // current population of the room
+unsigned long closeLastTime = 0, farLastTime = 0; // The last times a person was detected at the close and far sensors, respectively.  Note that 0 means the last detection never happened or happened too long ago.
 
 // SETUP
 void setup() {

  CurieIMU.setGyroRange(250);

  // initialize variables to pace updates to correct rate
  microsPerReading = 1000000 / 25;
  microsPrevious = micros();
  microsBetweenReads = 1000000 / 2; // must wait half a second between consecutive reads
  microsLastRead = micros();

  // get initial angle of the door
  // startAngle = measureYaw();
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);  // Indicate that setup is complete
}

void loop() {

  return a;
}

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