I am new to the forum and a novice with Arduino.
There's a problem in my sketch.
[code]
#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include "DHT.h"
#include <ThreeWire.h>
#include <RtcDS1302.h>
#define DHTTYPE DHT11 // DHT 11
uint8_t DHTPin = 6;
DHT dht(DHTPin, DHTTYPE);
float Temperature;
float Humidity;
ThreeWire myWire(8,5,2); // IO, SCLK, CE
RtcDS1302 Rtc(myWire);
const int chipSelect = 10; //SD card CS pin connected to pin 4 of Arduino
void setup()
{
// Setup Serial connection
Serial.begin(9600);
pinMode(DHTPin, INPUT);
dht.begin();
Serial.print("compiled: ");
Serial.print(DATE);
Serial.println(TIME);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
// printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
Serial.println(" ");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
Initialize_SDcard();
}
void loop()
{
Write_SDcard();
Read_TempHum();
RtcDateTime now = Rtc.GetDateTime();
delay(1000); //Wait for 5 seconds before writing the next data
}
void Read_TempHum()
{
Temperature = dht.readTemperature();
Humidity = dht.readHumidity();
Serial.print("Temperature = ");
Serial.println(Temperature);
Serial.print("Humidity = ");
Serial.println(Humidity);
// delay(1000);
}
void Initialize_SDcard()
{
// 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;
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println("Temperature,Humidity"); //Write the first row of the excel file
dataFile.close();
}
}
void Write_SDcard()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
printDateTime;
dataFile.print(",");
dataFile.print(Temperature); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(Humidity); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.println(); //End of Row move to next row
dataFile.close(); //Close the file
}
else
Serial.println("SD card writing failed");
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
[code]
No matter what I try, every solution creates a new problem.
Please HELP!