#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h> //Library or DS1820 temperature sensor
#include <DS3231.h> //Library for DS3231 RTC time, and temperature
#include <LiquidCrystal.h>
//LCD def
LiquidCrystal LCD(9,8,7,5,4,3);
// Data wire (of DS1820) is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//SetUpClock
DS3231 myRTC;
File myFile;
double Temperature;
double TemperatureII;
#define button1Pin A1 // where the set button is connected
#define button2Pin A2 // where the Options button is connected
#define button3Pin A3 // where the Options button is connected
bool celsius=true;
char CF;
//Time measurment values
byte hrNow;
byte minNow;
byte interval=1;
byte hrRecord=0;
byte minRecord=0;
unsigned long previousTime=0;
unsigned long timeOutInterval=1000;
//RTC requiered valuues
bool century = false;
bool h12Flag;
bool pmFlag;
bool SpecialEvent=false;
//=================================
void setup() {
//LCD intro
LCD.begin (16,2);
LCD.setCursor (3,0);
LCD.print ("Nav Light:");
LCD.setCursor (3,1);
LCD.print ("TempGraber");
delay(4000);
LCD.clear();
// Start up the DallasTemperature library
sensors.begin();
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
// Start the I2C interface
Wire.begin();
Serial.begin(9600);
//Card start
LCD.setCursor (0,0);
LCD.print ("SD card check...");
delay(2000);
LCD.clear();
if(!SD.begin(10)) {
LCD.setCursor (0,0);
LCD.print ("SD card failed!");
return;
}
LCD.setCursor (0,0);
LCD.print ("SD card OK.");
delay(2000);
LCD.clear();
//Open file
myFile=SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
LCD.setCursor (0,0);
LCD.print ("File OK.");
delay(2000);
LCD.clear();
}
myFile.close();
}
//=================================
void loop() {
// put your main code here, to run repeatedly:
//Celsius/Farenhaind switch*/
if (digitalRead(button1Pin) == HIGH) {
celFarSwitch();
}
delay(100);
//ShowTime();
if (digitalRead(button3Pin)== HIGH) {
Serial.println ("B3");
}
if (celsius == true){
cTemp();
}
else {
fTemp();
}
Display();
fileWrite();
}
//=================================
void cTemp(){
sensors.requestTemperatures();
Temperature = sensors.getTempCByIndex(0);
TemperatureII = sensors.getTempCByIndex(1);
CF='C';
}
void fTemp() {
sensors.requestTemperatures();
Temperature = (sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0;
TemperatureII = (sensors.getTempCByIndex(1) * 9.0) / 5.0 + 32.0;
CF='F';
}
void fileWrite(){
//check current hour and minute
hrNow=myRTC.getHour(h12Flag, pmFlag);
minNow=myRTC.getMinute();
//check if record time came
if (hrNow==hrRecord && minNow==minRecord) {
myFile=SD.open("DATA.txt", FILE_WRITE);
myFile.print("Date: ");
myFile.print(myRTC.getDate(), DEC);
myFile.print(".");
myFile.print(myRTC.getMonth(century), DEC);
myFile.print(".");
myFile.print(myRTC.getYear(), DEC);
myFile.print(" ");
myFile.print("Record time: ");
myFile.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
myFile.print(":");
myFile.print(myRTC.getMinute(), DEC);
myFile.print(":");
myFile.print(myRTC.getSecond(), DEC);
myFile.print(" ");
myFile.print("Temperature Sensor (0): ");
myFile.print(" ");
myFile.print(Temperature);
myFile.print(" ");
if (celsius==true) {myFile.print("C");}
else { myFile.print("F");}
myFile.print(" ");
myFile.print("Temperature Sensor (1): ");
myFile.print(" ");
myFile.print(TemperatureII);
if (celsius==true) { myFile.print("C");}
else { myFile.print("F");}
myFile.print(" ");
myFile.print("Room temperature: ");
myFile.print (myRTC.getTemperature());
myFile.print("C");
myFile.println(" ");
myFile.close();
//set records time to 0;
minRecord=0;
hrRecord=0;
//Print Record message. If special event is true, print nothing.
if (SpecialEvent==false) {
LCD.clear();
LCD.setCursor (0,0);
LCD.print ("Record added.");
LCD.setCursor (0,1);
LCD.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
LCD.print(":");
LCD.print(myRTC.getMinute(), DEC);
LCD.print(":");
LCD.print(myRTC.getSecond(), DEC);
delay(2000);
LCD.clear();
}
}
//set record time
minRecord = minNow + interval;
Serial.println(minNow);
Serial.println(interval);
Serial.println(minRecord);
if (minRecord < 60) {
hrRecord=myRTC.getHour(h12Flag, pmFlag);
}
else {
minRecord = minRecord - 60;
hrRecord=hrRecord=myRTC.getHour(h12Flag, pmFlag)+1;
}
}
void Display(){
LCD.setCursor (0,0);
LCD.print ("Snsr0:");
LCD.setCursor (7,0);
LCD.print (Temperature);
LCD.print((char)223);
LCD.print (CF);
LCD.setCursor (0,1);
LCD.print ("Snsr1:");
LCD.setCursor (7,1);
LCD.print (TemperatureII);
LCD.print((char)223);
LCD.print (CF);
}
void ShowTime(){
SpecialEvent=true;
LCD.clear();
LCD.setCursor (0,0);
LCD.print ("Date: ");
LCD.print(myRTC.getDate(), DEC);
LCD.print(".");
LCD.print(myRTC.getMonth(century), DEC);
LCD.print(".");
LCD.print(myRTC.getYear(), DEC);
LCD.setCursor (0,1);
LCD.print ("Time: ");
LCD.print(myRTC.getHour(h12Flag, pmFlag), DEC); //24-hr
LCD.print(":");
LCD.print(myRTC.getMinute(), DEC);
LCD.print(":");
LCD.print(myRTC.getSecond(), DEC);
delay(3000);
LCD.clear();
}
void SetInterval(){
SpecialEvent=true;
LCD.clear();
LCD.setCursor (0,0);
LCD.print ("Set save time: ");
LCD.setCursor (0,1);
LCD.print(interval);
LCD.print (" min.");
}
void celFarSwitch() {
SpecialEvent=true;
LCD.setCursor (0,0);
LCD.print ("Cel./F. switch ");
LCD.setCursor (0,1);
if (celsius==true) {
LCD.print ("Celsius < > ");
}
else {
LCD.print ("Farenhaind < > ");
}
if (digitalRead(button2Pin)== HIGH) {
delay(300);
celsius=!celsius;
}
delay(200);
if (digitalRead(button1Pin) == HIGH) {
SpecialEvent = false;
LCD.clear();
}
}