     prevDwn = stateDwn;
 
     // calculate the desired and current temperatures
-    // TODO: use floating point math until i can get a handle on integer scaling
-    // speed is not really a big deal here
-    float coolpoint = centerpoint + offset;
-    float heatpoint = centerpoint - offset;
-    float temperature = getTempF(tempsens);
-
-    // we used a 1 degree swing around our desired temperature
+    // we used a 1 degree swing around our desired temperature (hysteresis)
+    coolOn = centerpoint + offset + hysteresis;
+    coolOff = centerpoint + offset - hysteresis;
+    heatOn = centerpoint - offset + hysteresis;
+    heatOff = centerpoint - offset - hysteresis;
+    temperature = getTempF(analogTempSens);
+
+    // determine what mode we should be in
     // firing the relays is done in driveUnit function
-    if (temperature > (coolpoint + hysteresis)) {
-        driveUnit(cool);
-    } else if (temperature < (heatpoint - hysteresis)) {
-        driveUnit(heat);
-    } else if (temperature < (coolpoint - hysteresis)) {
-        driveUnit(off);
-    } else if (temperature < (heatpoint + hysteresis)) {
-        driveUnit(off);
-    }
+    if (mode != off) {
+        if ((temperature > coolOn) && (mode != cool)) {
+            mode = cool;
+        } else if ((temperature < heatOn) && (mode != heat)) {
+            mode = heat;
+        } else if ((temperature > heatOff) && (mode == heat)) {
+            mode = shutdown;
+        } else if ((temperature < coolOff) && (mode == cool)) {
+            mode = shutdown;
+        } else {
+            mode = cycle;
+        }
+    }
+    driveUnit(mode);
 
     // print the current state for debugging
-    //Serial.print(F("adc0: "));
-    //Serial.print(adc0);
     Serial.print(F("   deg F: "));
     Serial.print(temperature);
     Serial.print(F("   centerpoint:"));

}


float getTempF(int pin) {
    // get the temperature
    // method 1 integer math, loses precision in the typical house range?
    // take in the ADC value, int scale voltage to 5V=500
    // using a TMP36 which is 10mV/C with a +0.5V offset

These shift the center point up and down
*/

#include <EEPROM.h>

// Arduino convention is usually byte, but even arduino.h uses uint8_t
// pins
const uint8_t analogTempSens = 0;
const uint8_t buttonUp = 2;
const uint8_t buttonDwn = 3;
const uint8_t relayFan = 11;
const uint8_t relayAC = 12;
const uint8_t relayHeat = 13;

// numeric to text shortcuts
const uint8_t off = 0;
const uint8_t on = 1;
const uint8_t cool = 2;
const uint8_t heat = 3;
const uint8_t fan = 4;
const uint8_t shutdown = 5;
const uint8_t cycle = 6;

// eeprom storage locations
const uint8_t eepCheckVal = 0xAA;
const int eepCheckAddr = 0;
const int eepModeAddr = 1;
const int eepCenterpointAddr = 2;

// temperature parameters scaled as degree F x 10
const int defaultCenterpoint = 690; // 69.0
const int hysteresis = 5; // 0.5
const int offset = 50; // 5

// HVAC system parameters
const int fanDelay = 6000; // ms

// global variables used in the loops
int centerpoint;
uint8_t mode;

void setup(void) {
    // variable declaration
    uint8_t check;

    // setup digital pins
    pinMode(buttonUp, INPUT); // is default
    pinMode(buttonDwn, INPUT); // is default
    pinMode(relayFan, OUTPUT);
    pinMode(relayAC, OUTPUT);
    pinMode(relayHeat, OUTPUT);

    // start serial monitor
    Serial.begin(9600);

    // get mode and temperature settings from eeprom or default values
    // we store a basic check pattern in first bit
    // if it's there, load the previous settings
    // otherwise use the defaults and write them to EEPROM
    // note that mode only stores either "on" or "off"
    check = EEPROM.read(eepCheckAddr);
    if (check == eepCheckVal) {
        Serial.println(F("Using mode and temperature from EEPROM"));
        mode = EEPROM.read(eepModeAddr);
        centerpoint = getEEPROMint(eepCenterpointAddr);
    } else {
        Serial.println(F("Using mode and temperature from defaults"));
        mode = on;
        centerpoint = defaultCenterpoint;

        // EEPROM.clear();
        setEEPROMbyte(eepCheckAddr, eepCheckVal);
        setEEPROMbyte(eepModeAddr, mode);
        setEEPROMint(eepCenterpointAddr, centerpoint);
    }
}


void loop(void) {
    // variable declaration
    static uint8_t prevUp = HIGH, prevDwn = HIGH;
    uint8_t stateUp, stateDwn;
    int coolOn, coolOff, heatOn, heatOff, temperature;

    // check if we are pushing any buttons when the loop begins
    stateUp  = digitalRead(buttonUp);
    stateDwn = digitalRead(buttonDwn);

    // if this is first time button is pressed, move the setpoint
    // if both buttons are pressed, toggle system on and off and save to eeprom
    // TODO: write this as an interrupt?
    if ((stateUp == LOW) && (stateDwn == LOW) && (prevUp != LOW) && (prevDwn != LOW)) {
        if (mode == off) {
            mode = on;
        } else {
            mode = off;
        }
        setEEPROMbyte(eepModeAddr, mode);
    } else if ((stateUp == LOW) && (prevUp != LOW)) {
        centerpoint += 10; // +1.0 F
        setEEPROMint(eepCenterpointAddr, centerpoint);
    } else if ((stateDwn == LOW) && (prevDwn != LOW)) {
        centerpoint -= 10; // -1.0 F
        setEEPROMint(eepCenterpointAddr, centerpoint);
    }

    // store current button states in global vars for next loop through
