I am having a very strange issue with the realtime clock in Arduino that I cannot seem to solve. Basically, I have a program that logs data from a terrarium kit and saves each session to an SD card as a file named after the time the program started. 0
The problem with the program is that the code hangs on the statement before the setup section: “DateTime now = RTC.now().” The realtime clock appears to be functioning correctly with the example program which makes this issue even more strange. I am unsure if this is purely a software or hardware issue. From scouring the internet, I have found people with a similar problem to mine where the RTC will freeze up the i2c bus on the Arduino. However, I am unsure whether this is the issue plaguing me. If this is indeed the issue plaguing my program, unsure of how to work around it. I found an online solution here, however my program freezes upon launch so this would not work for me.
So far, I have tried: -Replacing the coin-cell battery in the realtime clock (I am using 3 volt batteries) -Replacing the datalogger shield with a new one -Replacing the Arduino with a new one -Checking to make sure that the jumpers were correctly soldered to the datalogger shield (Adafruit support checked mine through images and said they looked good)
I am running this program on IDE Version 1.6.9 with a program with the newer Adafruit datalogger shield on the Arduino Mega.
Any help would be greatly appreciated
//The libraries we need for our terrarium kit
//Download and install them all at this link: https://drive.google.com/drive/folders/10XAFdYmnyz0dg9I6YYj80vJlHebFIZhN
#include <OneWire.h> //Import OneWire library
#include <DallasTemperature.h> //Import DallasTemperature library
#include <Adafruit_Sensor.h> //Import Adafruit Sensor library
#include <DHT.h> //Import first part of the DHT library
#include <TimerOne.h> //Import TimerOne library
#include <SPI.h>
#include <SD.h> //this refers to an old SD library needed specifically for
#include "RTClib.h" //this is from the rtc example folder code
//SD Card & Realtime Clock: Initialization
File logfile;
RTC_DS1307 RTC;
//Humidity Sensor: Constants
//This #define defines the pin that the DHT is plugged into
#define DHT_PIN 2
//This #define defines the type of the DHT
#define DHT_TYPE DHT22
//This tells the code that a DHT is plugged into port DHT_PIN and the DHT is of type DHT_TYPE
DHT dht(DHT_PIN, DHT_TYPE);
//Moisture Sensor: Variables
int MS_sigPin = A5; //Set the signal pin as analog pin 5
int MS_digPin = 7; //Set the digital pin as 2
float MS_upper = 0; //Declare highest value variable
//Other variables
int measurementNumber = 1; //Number of measurements taken
//Filename:
DateTime now = RTC.now(); //Initialize the RTC
// String seconds = (String)(now.second()); //Stores the seconds from the RTC upon setup as a string
const String minutes = (String)(now.minute()); //Stores the seconds from the RTC upon setup as a string
const String hours = (String)(now.hour()); //Stores the seconds from the RTC upon setup as a string
const String days = (String)(now.day()); //Stores the seconds from the RTC upon setup as a string
const String months = (String)(now.month()); //Stores the seconds from the RTC upon setup as a string
const String years = (String)(now.year()); //Stores the seconds from the RTC upon setup as a string
const String filename = "D_" + minutes + hours + days + months + years; //Create the file name as a timestamp
void setup() {
int chipSelect = 10; //chipSelect pin for the SD card
// put your setup code here, to run once:
Serial.begin(115200); //Set the baud rate at 56700
//If RTC is already updated, the below is commented out
RTC.begin();
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));//this is to force rtc update
// if (!RTC.isrunning()) {
//
// Serial.println("RTC is NOT running! Starting it now.");
// RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
//
// }// end rtc if
//{
//The below is copied from wired, contains a second try, hoping for sd success herein.
pinMode(chipSelect, OUTPUT);
digitalWrite(chipSelect, HIGH);
SD.begin(chipSelect);
// if (!SD.begin(10, 11, 12, 13)) {
// Serial.println("Card failed, or not present");
// delay(50);
// Serial.println("Trying again...");
// if (!SD.begin(50, 51, 52, 53)) {
// Serial.println("Card failed, or not present");
// }
// }
/******************************** CSV FORMAT ******************************/
// Serial.print("Current Position: ");
// Serial.println(logfile.position());
if(logfile.position() == 0)//Below print header line for datafile
logfile.println("Unix Time, Date, Time, Light, Humidity, Temperature");
//logfile.println("uni,date,time,Temperature");
/**************************************************************************/
// sensors.begin();
// sensors.setResolution(12);
//Humidity Sensor:
dht.begin();
//Moisture Sensor:
pinMode(MS_sigPin, OUTPUT); //Set the signal pin as an output
pinMode(MS_digPin, OUTPUT); //Set the digital pin as an output
digitalWrite(MS_digPin, HIGH); //Turn the digital pin on
Serial.println("DIP THE MOISTURE SENSOR IN A CUP OF WATER MAKING SURE NOT TO SUBMERGE THE TOP OF IT INTO THE WATER, ENTER IN ANY KEY WHEN YOU ARE READY TO CALIBRATE"); //Print out useful information
while (Serial.available() == 0) //Hang the program until the user inputs a key to continue
{
}
Serial.println("Starting Calibration"); //Print out useful information
Timer1.initialize(1000000); //Initialize an interrupt every 1 second
Timer1.attachInterrupt(Calibrate); //When the interrupt is called run the “Calibrate” function
}
//Calibrate the moisture sensor
void Calibrate() //Function to calibrate the sensor
{
if (analogRead(MS_sigPin) > MS_upper) //If the value read by the sensor is higher than the past highest value
{
MS_upper = analogRead(MS_sigPin); //Set the current value as the new highest value
}
}
void loop() {
//Photocell: Variables
int photocellPin = A0; // the cell and 10K pulldown are connected to a0
char fileCharStorage[(filename.length()+5)]; //+4 Characters to include ".csv"
filename.toCharArray(fileCharStorage, filename.length()+5);
fileCharStorage[filename.length()] = 'V'; //Set the file name
fileCharStorage[filename.length()-1] = 'S'; //Set the file name
fileCharStorage[filename.length()-2] = 'C'; //Set the file name
fileCharStorage[filename.length()-3] = '.'; //Set the file name
Serial.println(filename); //Print out the filename
Serial.println(sizeof(fileCharStorage)); //Print the length of the array storing the file name
logfile = SD.open(fileCharStorage, FILE_WRITE); //Creates (or opens) a file names "MMHHDDMMYYYY.CSV"
if (logfile)
{
Serial.println("The SD Card is functioning properly");
}
else
{
Serial.println("A Critical Error occured while trying to initialize the SD Card!");
}
//Realtime Clock
DateTime now = RTC.now();
//Photocell:
int photocellReading = analogRead(photocellPin); //Variable that stores the amount of light detected by the photocell as an integer
// This code gets the humidity from the DHT
float dhtHumidity = dht.readHumidity();
/*
This code gets the temperature from the DHT
*/
float dhtTemperature = dht.readTemperature();
Serial.println(""); //Text Formatting
Serial.println("Starting New Measurement (" + (String)measurementNumber + ")");
Serial.println(""); //Text Formatting
delay(1000);
//Print out data from the number of measurements taken
Serial.println("The Arduino has been active for " + (String)((measurementNumber*10)-10) + " seconds");
measurementNumber++;
//Print out data from the RTC
Serial.print("Unix Time (Seconds since 1/1/1970): ");
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.println(", ");
Serial.print("RTC time: ");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(",");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println("");
Serial.println("");
//Photocell:
Serial.println("Data from the photocell:");
Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading
if (photocellReading < 10) {
Serial.println(" - Dark"); //says that if any number is lower than 10 then it prints out dark.
} else if (photocellReading < 200) {
Serial.println(" - Dim"); //says that if any number is lower than 200 then it prints out dim.
} else if (photocellReading < 500) {
Serial.println(" - Light"); //says that if any number is lower than 500 then it prints out light.
} else if (photocellReading < 800) {
Serial.println(" - Bright"); //says that if any number is lower than 800 then it prints out bright
} else {
Serial.println(" - Very bright"); //says that if any number higher than 800 then it prints out very bright
}
Serial.println("");
// Humidity Sensor:
Serial.println("Data from the humidity sensor:");
Serial.print("Humidity: ");
Serial.print(dhtHumidity);
Serial.print(" %, Temp: ");
Serial.print(dhtTemperature);
Serial.println(" Celsius");
Serial.println("");
//Moisture Sensor:
Serial.println("Data from the moisture sensor:");
float percent = (analogRead(MS_sigPin)/MS_upper)*100; //Calculate moisture percentage
Serial.println("Moisture = " + (String)percent + "%"); //Print out the moisture percentage
Serial.println("");
Serial.println("--------------------------------------------------");
//Log everything onto the SD card
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(", ");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print("/");
logfile.print(now.year(), DEC);
logfile.print(",");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print(", ");
logfile.print(photocellReading);
logfile.print(", ");
logfile.print(dhtHumidity);
logfile.print(", ");
logfile.print(dhtTemperature);
logfile.println();
logfile.close();
// Delay by 1+9 (10) seconds between measurements
delay(9000);
}