//import libraries #include #include //#include #include #include #include #define ONE_WIRE_BUS 9 // Thermometer pin const int chipSelect = 10; // SD card is always on 10 // Set up a oneWire instance to communicate with any OneWire device\par OneWire ourWire(ONE_WIRE_BUS); DallasTemperature sensors(&ourWire); /************************RTC Items**********************************************/ #define RTC_I2C_ADDRESS 0x68 // This is the I2C address of the Real Time Clock byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; byte function = 0x00; // Function to be performed. //Send Time as T, sec(00-07), min, hr, dayOfWeek, dayOfMonth, month, year //example: T0045073300915 //sec=00, min=45, hr=07, dayofweek=3, dayofmonth=30, month=09, year=15 //RTC_DS1307 rtc; /*******************************************************************************/ File dataFile; void setup() { Serial.begin(9600); Wire.begin(); //start up I2C comms //setupRTC(); setupSD(); setupTemp(); } void loop(void) { if (Serial.available() > 0) { function = Serial.read(); if (function == 'T') { setDateTime(); getDateTime(); } function = 0; } loopTemp(); loopRTC(); delay(1000); } void setupSD() { Serial.print("Initializing SD card..."); // make sure that the default chip select pin is set to // output, even if you don't use it: pinMode(SS, OUTPUT); // see if the card is present and can be initialized: if (!SD.begin(10, 11, 12, 13)) { Serial.println("Card failed, or not present"); while (1) ; // Wait forever since we cant write data } Serial.println("card initialized."); // Open up the file we're going to log to!\par // Pick first unused file starting with "0".\par char name[13]; String namestr; for (int i = 0; i < 1000; i++) { namestr = String(i); namestr.toCharArray(name, namestr.length() + 1); if (SD.exists(name)) continue; break; } Serial.println(name); dataFile = SD.open(name, FILE_WRITE); if (! dataFile) { Serial.println("error opening file"); while (1) ; // Wait forever since we cant write data\par } } void setupTemp(void) { sensors.begin(); } void loopTemp() { sensors.requestTemperatures(); // Send the command to get temperatures\par Serial.println(sensors.getTempCByIndex(0)); dataFile.print(sensors.getTempCByIndex(0)); dataFile.print(", "); } void loopRTC() { getDateTime(); dataFile.print(year); dataFile.print(", "); dataFile.print(month); dataFile.print(", "); dataFile.print(dayOfMonth); dataFile.print(", "); dataFile.print(hour); dataFile.print(", ") dataFile.print(minute); dataFile.print(", "); dataFile.print(second); dataFile.println(""); dataFile.flush(); } void setDateTime() // Sets the date and time from the RTC { second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result. minute = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); hour = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); dayOfWeek = (byte) (Serial.read() - 48); dayOfMonth = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); month = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); year = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); Serial.println(second, DEC); Serial.println(minute, DEC); Serial.println(hour, DEC); Serial.println(dayOfWeek, DEC); Serial.println(dayOfMonth, DEC); Serial.println(month, DEC); Serial.println(year, DEC); Wire.beginTransmission(RTC_I2C_ADDRESS); Wire.write((byte)0x00); Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set // bit 6 (also need to change readDateDs1307) Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); delay(5); getDateTime(); } void getDateTime()// Gets the date and time from the RTC { Wire.beginTransmission(RTC_I2C_ADDRESS); Wire.write((byte)0x00); Wire.endTransmission(); Wire.requestFrom(RTC_I2C_ADDRESS, 7); // A few of these need masks because certain bits are control bits second = bcdToDec(Wire.read() & 0x7f); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm dayOfWeek = bcdToDec(Wire.read()); dayOfMonth = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); } 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) ); }