 int   heldMillis        = 0;
 byte  currentState      = 0;
 
+// calibration variables
+int   calibrationIteration  = 0;
+int   avgIdlePressure       = 0;
+bool  isCalibrating         = false;
+
 RFM69 radio;
 
 void setup() {
+//  Serial.begin(9600);
   pinMode(TURNON_TX_LED_PIN,        OUTPUT);
   pinMode(TURNON_FLOWER_RED_PIN,    OUTPUT);
   pinMode(TURNON_FLOWER_GREEN_PIN,  OUTPUT);
   pinMode(TURNON_FLOWER_BLUE_PIN,   OUTPUT);
-  
+
   initRadio();
+
+  // Start in calibration mode
+  isCalibrating = true;
 }
 
 void loop() {
-  int   sensorValue       = analogRead(TURNON_SENSOR_PIN);
-  int   potLeftValue      = analogRead(TURNON_POT_LEFT_PIN);
-  int   potRightValue     = analogRead(TURNON_POT_RIGHT_PIN);
-  byte  colorBit          = potValuesToColorBit(potLeftValue, potRightValue);
-  
-  updateCurrentState(sensorValue, colorBit);
-  updateFlowerColor();
-  broadcastCurrentStateIfChanged();
+  int sensorValue = analogRead(TURNON_SENSOR_PIN);
+
+  if(isCalibrating) {
+    runCalibrationMode(sensorValue);
+  } else {
+    runSenseMode(sensorValue);
+  }
+
   delay(TURNON_CYCLE_DURATION);
 }
 

  byte  colorBit        = 0;
  byte  potLeftState    = thresholdPotValue(potLeftValue);
  byte  potRightState   = thresholdPotValue(potRightValue);
  
  bitWrite(colorBit, 0, potRightState);
  bitWrite(colorBit, 1, potLeftState);


  }
}

void runSenseMode(int sensorValue) {
  int   potLeftValue      = analogRead(TURNON_POT_LEFT_PIN);
  int   potRightValue     = analogRead(TURNON_POT_RIGHT_PIN);
  byte  colorBit          = potValuesToColorBit(potLeftValue, potRightValue);

  updateCurrentState(sensorValue, colorBit);
  updateFlowerColor();
  broadcastCurrentStateIfChanged();
}

void runCalibrationMode(int sensorValue) {
  avgIdlePressure =
    (calibrationIteration * avgIdlePressure + sensorValue) /
    (calibrationIteration + 1);

  calibrationDuration = TURNON_CYCLE_DURATION * calibrationIteration;

  if(calibrationDuration >= TURNON_CALIBRATION_DURATION) {
    isCalibrating = false;
  }

  calibrationIteration++;
}

// Given the sensor value, this function computes the current state
// of the transmitter.
void updateCurrentState(int sensorValue, byte colorBit) {
//  Serial.println(sensorValue);

  // We've met the squeeze threshold.
  if(sensorValue > avgIdlePressure + TURNON_SQUEEZE_THRESHOLD) {
    heldMillis += TURNON_CYCLE_DURATION;

    // We've squeezed for a duration of at least TURNON_HOLD_MILLIS
