 }
 
 
-float getTempF(int pin) {
-    // get the temperature
+int getTempF(int pin) {
+    /* get the temperature
+    Args:
+        pin (int):  the Arduino analog input pin to read from
+    Returns:
+        temp (int):  scaled Farenheit temperature (10x float)
+    */
+    float voltage, tempC, tempF;
+
     // 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

    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
    // 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);
    }

    // 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:"));


    // method 2 use slower floats but simpler
    // multiplier value taken from sparkfun
    voltage = analogRead(pin)*0.004882814;

    tempC = (voltage - 0.5) * 100.0;
    tempF = (tempC * (9.0/5.0) + 32.0);

    Serial.print(F("voltage: "));
    Serial.print(voltage);
    Serial.print(F("   deg C: "));
    Serial.print(tempC);

    return int(tempF * 10);
}


void driveUnit(uint8_t unit) {
    /*
    this function controls outputs that control relays
    heat and cool spin up the fan 1 minute before engaging
    call this recursively to do so
    shutdown leaves the fan on 1 minute after

    Args:
        unit (uint8_t): which HVAC function to activate, or off for all off
    Returns:
        none

    wiring notes:
    red wire - 24V hot
    blue wire - 24V common
    green wire - fan
    yellow wire - cooling
    white wire - heating
    */
    static unsigned long timeFan = 0;
    unsigned long timeCurrent = millis();

    switch(unit) {
        case cool:
            if ((timeCurrent - timeFan) < fanDelay) {
                driveUnit(fan);
            } else {
                if (digitalRead(relayAC) != HIGH) {
                    digitalWrite(relayAC, HIGH);
                }
            }
            break;
        case heat:
            if ((timeCurrent - timeFan) < fanDelay) {
                driveUnit(fan);
            } else {
                if (digitalRead(relayHeat) != HIGH) {
                    digitalWrite(relayHeat, HIGH);
                }
            }
            break;
        case fan:
            if (digitalRead(relayFan) != HIGH) {
                timeFan = timeCurrent;
                digitalWrite(relayFan, HIGH);
            }
            break;
        case shutdown:

        default:
            digitalWrite(relayFan, LOW);
            digitalWrite(relayAC, LOW);
            digitalWrite(relayHeat, LOW);
            break;
    }
}


int getEEPROMint(int address) {
    uint8_t hiByte, loByte;

    hiByte = EEPROM.read(address);
    loByte = EEPROM.read(address + 1);

    return word(hiByte, loByte);
}


void setEEPROMint(int address, int value) {
    uint8_t hiByte, loByte;

    hiByte = highByte(value);
    loByte = lowByte(value);

    if (EEPROM.read(address) != hiByte) {
        EEPROM.write(address, hiByte);
    }
    if (EEPROM.read(address + 1) != loByte) {
        EEPROM.write(address + 1, loByte);
    }
}


void setEEPROMbyte(int address, uint8_t value) {
    if (EEPROM.read(address) != value) {
        EEPROM.write(address, value);
    }
}