What does it do and why is this necessary,or is it necessary ?
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
// degree fahrenheit
byte newChar[8]= {
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000,
B00000
};
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"
/*
A simple data logger for the Arduino analog pins. This one is simplifed by taking away the
calculation that determined file flush interval. Since the readings are far enought apart,
the file is flushed for every reading.
*/
#define ECHO_TO_SERIAL 1 // echo data to serial port
// The analog pins that connect to the sensors
#define PHOTOCELLPIN A0 // analog 0
#define chipSelect 10 // for the data logging shield, we use digital pin 10 for the SD cs line
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 2 // The DHT output is connected to digital pin 7.
DHT dht(DHTPIN, DHTTYPE); //define the DHT object
RTC_DS1307 RTC; // define the Real Time Clock object
File fileData; // the logging file on the SD card
unsigned long ulintMillis = 0; // milliseconds since start
int Relay = 3;
int intPhotocellreading = 0;
int light_sensitivity = 850;
float fHumidity = 0;
float fTemp = 0;
const unsigned long ulintLoginterval = 5000; // mills between entries (reduce to take more/faster data)
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
}
void setup()//------------------------------------------------------------------------------------------------------------------------SETUP
{
pinMode(Relay, OUTPUT); //Relay
Serial.begin(9600);
//Serial.println();
Serial.println("Millis, Date , Time , Light , Humidity , Temp");
lcd.createChar(1, newChar);
lcd.begin(16,2);
lcd.clear();
lcd.print("TEMP LDR");
lcd.setCursor(4,1);
lcd.print("HUMIDITY");
pinMode(DHTPIN, INPUT);
pinMode(PHOTOCELLPIN, INPUT);
pinMode(chipSelect, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present, or datalog.csv file not present");
}
//Serial.println("card initialized.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
fileData = SD.open("datalog.csv", FILE_WRITE);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
fileData.println("RTC failed");
#if ECHO_TO_SERIAL
error ("RTC failed");
#endif //ECHO_TO_SERIAL
}
dht.begin(); // Start reading from the temperature and humidity sensor
if (fileData){
fileData.println("Millis , Date , Time, Light, Humidity , Temp");
}
else{
error("Could not write to file.");
}
}
void loop(void)//---------------------------------------------------------------------------------------------------------LOOP
{
DateTime now;
// delay for the amount of time we want between readings
delay((ulintLoginterval -1) - (millis() % ulintLoginterval));
// log milliseconds since starting
ulintMillis = millis();
fileData.print(ulintMillis); // milliseconds since start
fileData.print(", ");
#if ECHO_TO_SERIAL
Serial.print(ulintMillis); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
// log time
fileData.print(now.year(), DEC);
fileData.print("/");
fileData.print(now.month(), DEC);
fileData.print("/");
fileData.print(now.day(), DEC);
fileData.print(",");
fileData.print(now.hour(), DEC);
fileData.print(":");
fileData.print(now.minute(), DEC);
fileData.print(":");
fileData.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(",");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL
intPhotocellreading = analogRead(PHOTOCELLPIN);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
fHumidity = dht.readHumidity();
fTemp = dht.readTemperature(true);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(fHumidity) || isnan(fTemp)) {
Serial.println("Failed to read from DHT");
}
fileData.print(", ");
fileData.print(intPhotocellreading);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(intPhotocellreading); //
#endif //ECHO_TO_SERIAL
fileData.print(", ");
fileData.print(fHumidity);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(fHumidity);
#endif //ECHO_TO_SERIAL
fileData.print(", ");
fileData.print(fTemp);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(fTemp);
#endif //ECHO_TO_SERIAL
fileData.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
lcd.clear();
lcd.setCursor(0,0);
lcd.print(fTemp);
lcd.write(1);
lcd.setCursor(0,1);
lcd.print(fHumidity);
lcd.print("%");
lcd.setCursor(12,0);
lcd.print("LDR");
lcd.setCursor(12,1);
lcd.print(intPhotocellreading);
fileData.flush();
intPhotocellreading = analogRead(intPhotocellreading); //reads the ldr’s value through LDR which we have set to Analog input 0 “A0?
if (intPhotocellreading > light_sensitivity)
{
digitalWrite(Relay, HIGH);
delay(180000);
digitalWrite(Relay, LOW);
delay(1620000);
}
if (intPhotocellreading < light_sensitivity)
{
digitalWrite(Relay, LOW);
delay(900000);
}
}