//Yogurtino 1.0 20/05/2016  - Celso Ferrarini Junior - cferrarini@gmail.com 
//Free For Personal Use

#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//use lcd guesser.ino if you are not sure of your lcd address and pin numbers


DS3231 clock;
RTCDateTime dt;

int red = 13;
int green = 12;
int blue = 11;

//Internal VCC voltage
#include <Vcc.h>
const float VccMin   = 0.0;           // Minimum expected Vcc level, in Volts.
const float VccMax   = 5.0;           // Maximum expected Vcc level, in Volts.
const float VccCorrection = 1.0/1.0;  // Measured Vcc by multimeter divided by reported Vcc
Vcc vcc(VccCorrection);


//Voltimeter
//int analogInput = 1;
//float vout = 0.0;
//float vin = 0.0;
//float R1 = 100000.0; // resistance of R1 (100K) -see text!
//float R2 = 10000.0; // resistance of R2 (10K) - see text!
//int value = 0;


//Thermistor:
#include <math.h>
#define ThermistorPIN 0                 // Analog Pin 0
//float vcc = 5.02;                       // only used for display purposes, if used                                     // set to the measured Vcc.
float pad = 9970;                       // balance/pad resistor value, set this to                                       // the measured resistance of your pad resistor
float thermr = 10000;                   // thermistor nominal resistance
float Thermistor(int RawADC) {
long Resistance;  
float Temp;  // Dual-Purpose variable to save space.
  Resistance=pad*((1024.0 / RawADC) - 1);
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                      
  return Temp;                                      // Return the Temperature
}

void setup()
{

  pinMode(red, OUTPUT);   // sets the pins as output
  pinMode(green, OUTPUT);   
  pinMode(blue, OUTPUT); 
  
// voltimeter
//    pinMode(analogInput, INPUT);
    
    lcd.begin(16,2);               // initialize the lcd 
//  Serial.begin(9600);

  // Initialize DS3231
//  Serial.println("Initialize DS3231");;
  clock.begin();

  // Set sketch compiling time
  // clock.setDateTime(__DATE__, __TIME__);

  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);

  // Manual (YYYY, MM, DD, HH, II, SS
  // clock.setDateTime(2014, 4, 13, 19, 21, 00);
}

void loop()
{
  dt = clock.getDateTime();

//thermistor
 float temp;
  temp=Thermistor(analogRead(ThermistorPIN));       // read ADC and  convert it to Celsius

//voltimeter
   // read the value at analog input
//   value = analogRead(analogInput);
//   vout = (value * 5.0) / 1024.0; // see text
//   vin = vout / (R2/(R1+R2)); 
//   if (vin<0.09) {
 //  vin=0.0;//statement to quash undesired reading !
//} 

//Internal VCC voltage
 float v = vcc.Read_Volts();

lcd.setCursor ( 0, 0 );
lcd.print(clock.dateFormat("d/m/y", dt));
lcd.print(clock.dateFormat("l", dt));
lcd.print(v);
lcd.print("V");
//  lcd.print(p);
//  lcd.print("%");
lcd.setCursor ( 0, 1 );
lcd.print(clock.dateFormat("H:i:s", dt));
lcd.setCursor ( 9, 1 );
//lcd.print(clock.readTemperature());
lcd.print(temp,1);
lcd.print((char)223);
lcd.print("C");
//delay(150);



 // HIGH TEMPERATURE
 if(temp >= 79 + 1) {
   digitalWrite(red,HIGH);
          tone(3,900,200);
       delay(300);
          digitalWrite(red,LOW);
                 tone(3,900,200);
       delay(300);
   digitalWrite(blue,LOW);
digitalWrite(green,LOW);
}
       
    // LOW TEMPERATURE
 else if(temp <= 46 - 1) {
       digitalWrite(blue,HIGH);
       tone(3,900,200);
       delay(300);

digitalWrite(blue,LOW);
       tone(3,900,200);
       delay(300);
                     digitalWrite(red,LOW);
                     digitalWrite(green,LOW);
  }
       
      else { //NORMAL TEMPERATURE
       digitalWrite(red,LOW);
              digitalWrite(blue,LOW);
                     digitalWrite(green,HIGH);
                     delay(500);
                     digitalWrite(green,LOW);
                     delay(500);
}
}

