//      Temperature senor and control for thenewdays
//
//      Uno R3
//      20x4 I2C LCD display
//      relay
//      MAX6675 K tpe sensor
//      Potentiometer
//

//
//      Use true while debugging and all Serial.print.. will be executed
//      Set to false for production code and Serial will be disabled
//
#define     DEBUG       true
//
//      For readings in degrees Celsius, set this true, otherwise false
//
#define     CELSIUS     false
//      Pin definitions for MAX6675
#define     sclkPin     3
#define     csPin       4
#define     misoPin     5
//      Pin definition for potentiometer
#define     potPin      A0
//      Pin definitions for I2C
#define     sdaPin      A4
#define     sclPin      A5
#define     relay    6

//
//      Define the temperature range covered by the potentiomener
//
#define     potLOW      75
#define     potHIGH     150
//
//
//      Include required libraries
//
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <max6675.h>
//
//      Objects and variables
//
MAX6675             sensor(sclkPin, csPin, misoPin);       // Make temp sensor object
LiquidCrystal_I2C   lcd(0x27,20,4);                     // Create the LCD 
//
float   temp,wasTemp,wasPot=0,pot;                      // Temp and pot readings
bool    heaterOn = false;                               // Is heater on ?
//int relay = 6;

void setup() {
  pinMode (relay, OUTPUT);
  
#if DEBUG
    Serial.begin(9600);
#endif
    lcd.init();                                         // Initialize LCD
    lcd.backlight();                                    // Turn on backlight
    lcd.clear();                                        // Clear the screen
    digitalWrite(relay,LOW);                         // 
}

void loop() {
  
//
//      Get current readings for temperature and for potentiometer
//
    getTemperature();                                   // Read current temperature
    getPotentiometer();                                 // Read current potentiometer setting
//
//      If either one changed, then update the display
//
    if (temp!=wasTemp || pot!=wasPot)
        LCDDisplay();                                   // Show new value(s)
//
//      Adjust heating state according to temp settings
//
    if (temp<=pot) {                                    // If too cold
        turnHeatOn();                                   // Star heating
    } else {
        turnHeatOff();
    }
//
//      Save current readings for next pass comparison
//
    wasTemp = temp;                                     // Update values
    wasPot = pot;
  
}
void LCDDisplay() {

    lcd.setCursor(0,0); lcd.print("Moebius");
    lcd.setCursor(0,1); lcd.print("UT Temp Ctrl");
  
    lcd.setCursor(0,2); lcd.print("Temperature ");
    lcd.setCursor(12,2); lcd.print(temp);
    lcd.setCursor(0,3); lcd.print("Set at   ");
    lcd.setCursor(12,3); lcd.print(pot);  
    }
//
//      Read the potentiometer and morph into range required
//
void getPotentiometer() {
    int     reading = analogRead(potPin);               // Read the pot
    pot = map(reading,0,1023,potLOW,potHIGH);           // Wrap to proper range
#if DEBUG
    if (pot!=wasPot) {
        Serial.print("Read pot value ");
        Serial.print(reading);
        Serial.print(" corresponding to temp ");
        Serial.println( pot );
        }
#endif
    }
//
//
//      Read the temperature from the sensor
//
float getTemperature() {
#if CELSIUS
    temp = sensor.readCelsius();
#else
    temp =  sensor.readFarenheit();
#endif
#if DEBUG
    if (temp!=wasTemp) {                                // If temperature changed
        Serial.print("Read temperature ");
        Serial.print(temp);
        Serial.println( (CELSIUS) ? " degrees C" : " degrees F");
        }
#endif
      }
//
//      Turn the heat on
//
void turnHeatOn() {
    if (heaterOn) {                                    // If not already heating
        heaterOn = true;                                // Start heating now
        digitalWrite (relay,HIGH);
#if DEBUG
        Serial.print("Started heating ");
#endif
      }
    }
//
//      Turn the heat off
//
void turnHeatOff() {
     if (heaterOn) {                                    // If heating
        heaterOn = false;                                // Stopt heating now
        digitalWrite (relay,LOW);
#if DEBUG
        Serial.print("Stopped heating ");
#endif
    }
  }