/* SD card datalogger
This example shows how to log data from TEMPRATURE AND HUMIDITY SENSOR,
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10 (for MKRZero SD: SDCARD_SS_PIN)
by PIYUSH KUMAR SINGH
*/
#include <SPI.h>
#include <SD.h>
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
int x;
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
Wire.begin(12);
Wire.onReceive(receiveEvent);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void writeTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
String TIMEString="";
File TIMEFile = SD.open("IR.txt", FILE_WRITE);
TIMEString += "TIME: ";
TIMEString += String(hour, DEC);
TIMEString += ":";
if (minute<10)
{
TIMEString += "0";
}
TIMEString += String(minute, DEC);
TIMEString += ":";
if (second<10)
{
TIMEString += "0";
}
TIMEString += String(second, DEC);
TIMEString += " ";
TIMEString += String(dayOfMonth, DEC);
TIMEString += "/";
TIMEString += String(month, DEC);
TIMEString += "/";
TIMEString += String(year, DEC);
TIMEString +=" Day of week: ";
switch(dayOfWeek){
case 1:
TIMEString +="Sunday";
break;
case 2:
TIMEString +="Monday";
break;
case 3:
TIMEString +="Tuesday";
break;
case 4:
TIMEString +="Wednesday";
break;
case 5:
TIMEString +="Thursday";
break;
case 6:
TIMEString +="Friday";
break;
case 7:
TIMEString +="Saturday";
break;
}
// if the file is available, write to it:
if (TIMEFile) {
TIMEFile.println(TIMEString);
TIMEFile.close();
// print to the serial port too:
Serial.println(TIMEString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening IR.txt");
}
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
}
void loop() {
String TIME = "";
switch(x)
{
case 1: { writeTime();
TIME += "SAA";
break;
}
case 2: { writeTime();
TIME += "RE";
break;
}
case 3: { writeTime();
TIME += "GAA";
break;
}
case 4: { writeTime();
TIME += "MAA";
break;
}
case 5: { writeTime();
TIME += "PAA";
}
case 6: { writeTime();
TIME += "DHA";
break;
}
case 7: { writeTime();
TIME += "NI";
break;
}
default : { writeTime();
TIME += "empty";
break;
}
}
File mainFile = SD.open("song.txt", FILE_WRITE);
// if the file is available, write to it:
if (mainFile) {
mainFile.println(TIME);
mainFile.close();
// print to the serial port too:
Serial.println(TIME);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening IR.txt");
}
}