Hi,
I have three Arduinos (mega) with me now. Following code is working smoothly on 2 of them but one stops responding after printing header.
Problem here is, this faulty Arduino runs the other examples codes (like LED blink and other small codes) but not this.
What possibly went wrong? I also have checked SD card with other ones, there isn't any problem.
Any idea?
thanks.
Z
#include <SD.h>
#include <Wire.h>
#include <SPI.h>
int CS_pin = 4;
int SD_led = 8;
#include "floatToString.h"
#include "RTClib.h"
RTC_DS1307 RTC;
float COValue = 0;
float NO2Value = 0;
float O3Value = 0;
float e_NO2Value = 0;
float e_NOValue = 0;
float e_COValue = 0;
float celsius = 0, kelvin=0;
#include <math.h>
int dustPin=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
float ZeroPercentVoltage = 0.8;
float val = 0;
float RH = 0;
float max_voltage = 3.27;
void setup()
{
Wire.begin();
Serial.begin(9600);
SPI.begin();
Serial.println("Initializing Card");
pinMode(53, OUTPUT);
//digitalWrite(53, HIGH);
RTC.begin();
pinMode(ledPower,OUTPUT);
pinMode(5, OUTPUT);
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Write Log File Header
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(", , , , , , , , ,"); //Just a leading blank line, incase there was previous data
String header = "Date_Time, Temp, Humidity(%), e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, Dust_Vout";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
}
void loop()
{
char* date_time = (char*) calloc(80,sizeof(char));
char* CO_Vout = (char*) calloc(80,sizeof(char));
char* NO2_Vout = (char*) calloc(80,sizeof(char));
char* O3_Vout = (char*) calloc(80,sizeof(char));
char* cel = (char*) calloc(80,sizeof(char));
char* hum = (char*) calloc(80,sizeof(char));
char* e_NO2_Vout = (char*) calloc(80,sizeof(char));
char* e_NO_Vout = (char*) calloc(80,sizeof(char));
char* e_CO_Vout = (char*) calloc(80,sizeof(char));
NO2Value = analogRead(1) * 0.004882812;
COValue = analogRead(2) * 0.004882812;
O3Value = analogRead(3) * 0.004882812;
kelvin = analogRead(4) * 0.004882812 * 100;
celsius = (kelvin - 273.15) - 15;
val = analogRead(5);
max_voltage = (3.27-(0.006706*celsius));
RH = ((((val/1023)*5)-ZeroPercentVoltage)/max_voltage)*100;
digitalWrite(ledPower,LOW);
delayMicroseconds(delayTime);
int dust_Vout = analogRead(dustPin);
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH);
delayMicroseconds(offTime);
e_NO2Value = analogRead(8) * 0.004882812;
e_NOValue = analogRead(9) * 0.004882812;
e_COValue = analogRead(10) * 0.004882812;
floatToString(CO_Vout, COValue, 8);
floatToString(NO2_Vout, NO2Value, 8);
floatToString(O3_Vout, O3Value, 8);
floatToString(cel, celsius, 1);
floatToString(hum, RH, 1);
floatToString(e_NO2_Vout, e_NO2Value, 8);
floatToString(e_NO_Vout, e_NOValue, 8);
floatToString(e_CO_Vout, e_COValue, 8);
DateTime now = RTC.now();
sprintf(date_time,"%d/%d/%d %d:%d:%d",now.day(),now.month(),now.year(),now.hour(),now.minute(),now.second());
String dataString = String(date_time) + ", " + String(cel) + ", " + String(hum) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
// String dataString = String(date_time) + ", " + String(cel) + ", " + String(hum) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
pinMode(SD_led, OUTPUT);
digitalWrite(SD_led, HIGH);
}
else
{
Serial.println("Couldn't open log file");
}
delay(60000);
digitalWrite(SD_led, LOW);
free(date_time);
date_time = NULL;
free(CO_Vout);
CO_Vout = NULL;
free(NO2_Vout);
NO2_Vout = NULL;
free(O3_Vout);
O3_Vout = NULL;
free(cel);
cel = NULL;
free(hum);
hum = NULL;
free(e_NO2_Vout);
e_NO2_Vout = NULL;
free(e_NO_Vout);
e_NO_Vout = NULL;
free(e_CO_Vout);
e_CO_Vout = NULL;
}