Hello all i am new to the forum but have been coding for about 3 years so i know my way around stuff a little just can't get past this problem I'm facing at all. been scrolling through the internet and cannot find the problem.
The project is a kind of home automation system that logs the temperature using a TMP36, light with an LDR, and distance with a HC-SR04. i am also using a DS3231 to create a timestamp and using a microSD card module to save the data. I'm also using an LCD screen to show some findings.
when using the circuit and just programming it so that the ds3231 findings print to the serial and the temperature and light reads to the sd card aswel as the serial, it works. but when it works it says sd card failed but still writes to the file.
When i programme the code to print the ds3231 findings and the sensor readings to the sd card it will say card initialised at the start but will not open the file. its although it acts the complete wrong way around?
//HOME AUTOMATION ARDUINO CODE
// Including of libraries
#include <SD.h> //SD card library
#include <SPI.h> //SD card library
#include "Wire.h" // RTC library
#include "RTClib.h" // RTC library
#include <LiquidCrystal.h> // LCD library
#define DS3231_I2C_ADDRESS 0x68 // defining real time clock module
// Arduino component pins
int LDRValue = 0; // Light Sensor Value
int tmp36Pin = A0; // TMP36 Pin
int LDR = A1; // LDR pin
int buzzerPin = A2; // Buzzer for Intrusion detection system
int redLED = A3; // Red LED pin
// Real time clock module - A4, A5
int echoFrontDoorPing = 7; // Front door echo pin
const int trigFrontDoorPing = 8; // Front door trig pin
const int sdCardCS = 10; // SD card module Pins
//SD MOSI = 11
//SD MISO = 12
//SD SCK = 13
LiquidCrystal lcdscreen(9, 6, 5, 4, 3, 2); // LCD screen pins
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup()
{
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
pinMode(redLED, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Wire.begin();
lcdscreen.begin(16, 2);
delay(1000); // wait for console opening
Serial.print("SD CARD STARTUP.....");
pinMode(10, OUTPUT);
delay(2000);
if (SD.begin(sdCardCS)) {
Serial.println("SD CARD FAILED!");
return;
}
Serial.println("SD CARD INITIALIZED!");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2016, 7, 17, 12, 19, 0));
delay(1000);
}
}
void loop()
{
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
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();
// delay(1000);
// Getting temperature reading from TMP36
int reading = analogRead(tmp36Pin);
float voltage = reading *5.0;
voltage /= 1024.0;
float tempC = (voltage - 0.5)*100;
// print temperature reading to seriel
//Serial.println(tempC);
Serial.println("");
Serial.print("Temperature C - ");
Serial.print( tempC );
// IF STATEMENT used to define when the heating will be coming on. eg. 21C or below
if (tempC<21)
{
// Print on LCD
lcdscreen.setCursor(0,1);
lcdscreen.print(tempC);
lcdscreen.print("C - HEAT ON");
Serial.println(" - HEATING ON ");
}
// ELSE anything above 21C will not trigger the heating
else {
Serial.println(" - HEATING OFF ");
lcdscreen.setCursor(0,1);
lcdscreen.print(tempC);
lcdscreen.print("C -HEAT OFF");
}
// Getting light readings from LDR
LDRValue = analogRead(LDR);
Serial.println ("");
Serial.print ("The Light Reading is - ");
Serial.print (LDRValue);
// IF STATEMENT USED to define when the lights will come on e.g anyhting below 900
if (LDRValue <900)
{
Serial.println(" - LIGHTS ON ");
lcdscreen.setCursor(0, 0);
lcdscreen.print(LDRValue);
lcdscreen.print(" - LIGHTS ON");
}
// ELSE anything above the 900 reading will not trigger the lights to come on
else {
Serial.println(" - LIGHTS OFF ");
lcdscreen.setCursor(0, 0);
lcdscreen.print(LDRValue);
lcdscreen.print(" - LIGHTS OFF");
}
long duration, cm ;
delay(1000);
//Initializing the pin states
pinMode(trigFrontDoorPing, OUTPUT);
// FRONT DOOR PING
//Sending PING signal, starting with LOW for a clean signal
digitalWrite(trigFrontDoorPing, LOW);
delayMicroseconds(2);
digitalWrite(trigFrontDoorPing, HIGH);
delayMicroseconds(5);
digitalWrite(trigFrontDoorPing, LOW);
pinMode(echoFrontDoorPing, INPUT);
duration = pulseIn(echoFrontDoorPing, HIGH);
// convert the time into a distance - Microseconds to centimeters
cm = MsToCm(duration);
Serial.println("");
Serial.print("Front Door - ");
Serial.print(cm);
Serial.print("cm - ");
// IF STATEMENT used to define when alarm system will be active e.g anything detected over 50cm
if(cm > 50)
{
Serial.print("Intrusion Detection System enabled");
Serial.println("");
digitalWrite(redLED, HIGH);
delay(1000);
digitalWrite(redLED, LOW);
noTone(buzzerPin);
// ELSE STATEMENT used if anything is detected before 50cm the buzzer will go off and the LED
// will stay red constant
}
else
{
Serial.print("Alarm is going off!!");
Serial.println("");
digitalWrite(redLED, HIGH);
tone(buzzerPin, 400);
delay(1000);
tone(buzzerPin, 400);
delay(1000);
}
File dFile = SD.open("datalog.txt", FILE_WRITE);
// if available, write the file:
if (dFile) {
dFile.println("");
dFile.println("Temperature is - ");
dFile.print(tempC);
dFile.print(",");
dFile.print("The Light Reading is -");
dFile.print(LDRValue);
dFile.println("");
dFile.close();
// print to the serial port too:
Serial.println(tempC);
Serial.println(LDRValue);
delay(1000);
}
// if the file isn't open, show error:
else {
Serial.println("error opening datalog.txt");
}
}
// microseconds to centimeters conversion
long MsToCm( long microseconds) {
return microseconds / 29 / 2;
}
the code above is the code that allows the sensor readings to write to the SD card but says at the start SD card failed?
The code below is where i have added the date and time to be added to the sd card and it says at the start SD card initialized but won't allow the data to read to the file.
File dFile = SD.open("datalog.txt", FILE_WRITE);
// if available, write the file:
if (dFile) {
dFile.println("");
dFile.print(now.year(), DEC);
dFile.print('/');
dFile.print(now.month(), DEC);
dFile.print('/');
dFile.print(now.day(), DEC);
dFile.print(" (");
dFile.print(daysOfTheWeek[now.dayOfTheWeek()]);
dFile.print(") ");
dFile.print(now.hour(), DEC);
dFile.print(':');
dFile.print(now.minute(), DEC);
dFile.print(':');
dFile.print(now.second(), DEC);
dFile.println("Temperature is - ");
dFile.print(tempC);
dFile.print(",");
dFile.print("The Light Reading is -");
dFile.print(LDRValue);
dFile.println("");
dFile.close();
// print to the serial port too:
Serial.println(tempC);
Serial.println(LDRValue);
delay(1000);
}
// if the file isn't open, show error:
else {
Serial.println("error opening datalog.txt");
}
}
// microseconds to centimeters conversion
long MsToCm( long microseconds) {
return microseconds / 29 / 2;
}
this is my result from the 2nd code when trying to read time on sd card aswel -
really am stuck and don't know what else to try. I have tried putting the date and time in a string to be converted to a csv instead but still will not work. I seem to think that it is something to do with the librarys and they won't work together?
Any help would be much appreciated.
Regards oldskoolbray!