Code: Select all
// Work in Progress 27 Aug 2018
// Developing first data logging sketch
// read temperature sensor and log readings of deg C and deg F to SD card
// 28 August-- sucessfully write data to SD card, next add switch to control data logging
// 29 August-- switch added. Jumper Pin 3 to 3.3 volts to logg data, jumper to GRN to not log data
// note: need to "re-set" Arduino after re-inserting SD card
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celcius=0;
float Fahrenheit=0;
int inputPin=3; // jumper Pin 3 to 3.3 volts to write data to SD card, to GRN to not write to SD
int chipSelect = 4; // chipSlect pin for the SD card reader
File myFirstData; //Data object you will write your sensor data to
RTC_PCF8523 rtc;
int x; //value of Mudulo divide
int dlSwitch; // jumper to control logging of data
int dlSwitch2;
int counter = 0;
void setup(void)
{
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This following code would set the RTC with an explicit date & time, for example
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
Serial.begin(9600);
sensors.begin();
rtc.begin();
pinMode(inputPin, INPUT);
pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
SD.begin(4); //Initialize the SD card reader
//Add for LCD
lcd.begin(20,4);
lcd.backlight();
lcd.setCursor(0,0);
lcd.clear();
}
void loop(void)
{
myFirstData=SD.open("My_Data.txt", FILE_WRITE);
sensors.requestTemperatures();
Celcius=sensors.getTempCByIndex(0);
Fahrenheit=sensors.toFahrenheit(Celcius);
DateTime now = rtc.now(); //get current date&time from rtc
//print time with leading zeros
char printLineA[10]; // This will hold the Date
char printLineB[8]; // This will hold the Time
sprintf(printLineA,"%02d/%02d/%4d", now.month(), now.day(), now.year());
sprintf(printLineB,"%02d:%02d:%02d", now.hour(), now.minute(), now.second());
// Display on LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.setCursor(0,1);
lcd.print(printLineA);
lcd.print(" ");
lcd.print(printLineB);
lcd.setCursor(13,2);
lcd.print(Celcius);
lcd.print(" C");
lcd.setCursor(13,3);
lcd.print(Fahrenheit);
lcd.print(" F");
//create "mechcanical switch": possble way to control logging
dlSwitch = digitalRead(inputPin); // readcPin3 Hi or LOW
//create a counter: another possible way to control logging
counter = counter + 1;
//Use IF statement to control when to write to SD card
if ((now.second() == 00 || now.second() == 01))
{
//Print to Serial Port/screen TimeStamp and data
//print timestamp
Serial.print(printLineA);
Serial.print(" ");
Serial.print(printLineB);
Serial.print(" ");
// print data
Serial.print(Celcius);
Serial.print(", ");
Serial.print(Fahrenheit);
//Mudulo Divide -- base it off Unix Time
// if resulant of Mudulo divide of Unix time by 60 is zero (or 1 accoumting for imprecision of timing)
// one minute has elapsed -- write to SD card
x = now.unixtime()%60;
Serial.print(", ");
Serial.print(x);
Serial.print(", ");
Serial.print(dlSwitch);
Serial.print(", ");
Serial.print(dlSwitch2);
Serial.print(", ");
Serial.print(counter);
// end Mudulo divide
Serial.println();
// Write to SD card
Serial.print(" Log Record");
myFirstData.print(printLineA);
myFirstData.print(" ");
myFirstData.print(printLineB);
myFirstData.print(" ");
myFirstData.print(Celcius);
myFirstData.print(" ");
myFirstData.println(Fahrenheit);
myFirstData.close();
counter = 0;
}
// delay 304 for one second execution interval
delay(1304);
}