Trouble with SD card and other sensors

help please so here is my code down below I’m not trained on programming and I basically took a bunch of codes and put them together. I’m not getting any errors on the ide but only the sd card is communicating with me. I had the co2 sensor, temp sensor , dust sensors, all communicating before I tried to put the rtc clock and the sd card breakout board in the system. Please let me know your thoughts.
#include “SoftwareSerial.h”
#include “SD.h”
#include “Wire.h”
SoftwareSerial K_30_Serial(4,5); //Sets up a virtual serial port
//Using pin 5 for Rx and pin 4 for Tx
byte readCO2={0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25};
byte response={0,0,0,0,0,0,0};
int valMultiplier = 1;
int dust pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms =refresh_rate;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
int temperatureCommand = 3;
int humidityCommand = 5;
int clockPin = 2;
int dataPin = 3;
int val;
int ack;
float temperature;
float humidity;
const int chipSelect=10:
float refresh_rate=0.0;
long Num=1;

void setup()
{
Wire.begin();
Serial.begin(19200);
Serial.print(“Initializing SD card…”);
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x0E); // select register
Wire.write(0b00011100); // w
pinMode(10, OUTPUT);
pinMode(8,INPUT);
starttime = millis();//get the current time;
pinMode (2, INPUT);
pinMode ( 3, INPUT);
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
return;
}
Serial.println(“card initialized.”);

File commandFile= SD.open(“COMMANDS.txt”);
if (commandFile)
{
Serial.println(“Reading Command File”);
float decade=pow(10,(commandFile.available()-1));
while
(commandFile.available())
{
float temp= (commandFile.read()-‘0’);
refresh_rate= temp* decade + refresh_rate;
decade=decade/10;
}
Serial.print(“Refresh Rate=”);
Serial.print( refresh_rate);
Serial.println(“ms”);
commandFile.close();
}
else
{
Serial.println(“Could not readd command File”);
return;
}
File logFile=SD.open(“LOG6.csv”,FILE_WRITE);
if (logFile)
{
logFile.println(",");
String header= “Num,Temperature,Humidity,CO2,Dust concentration, hours, minutes,seconds”;
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println (“Couldn’t Open log file”);
}
}

void loop()
{sendRequest(readCO2);
unsigned long valCO2 = getValue(response);
Serial.print(“Co2 ppm = “);
Serial.println(valCO2);
delay(refresh_rate);
duration = pulseIn(dust pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
{
ratio = lowpulseoccupancy/(sampletime_ms10.0); // Integer percentage 0=>100
concentration = 1.1
pow(ratio,3)-3.8pow(ratio,2)+520ratio+0.62; // using spec sheet curve
Serial.print(lowpulseoccupancy);
Serial.print(”,”);
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
lowpulseoccupancy = 0;
starttime = millis();
}
sendCommandSHT(temperatureCommand, dataPin, clockPin);
waitForResultSHT(dataPin);
val = getData16SHT(dataPin, clockPin);
skipCrcSHT(dataPin, clockPin);
temperature = (float)val * 0.01 - 40;
temperature = (float)temperature;

sendCommandSHT(humidityCommand, dataPin, clockPin);
waitForResultSHT(dataPin);
val = getData16SHT(dataPin, clockPin);
skipCrcSHT(dataPin, clockPin);
humidity = -4.0 + 0.0405 * val + -0.0000028 * val * val;
Serial.print(temperature100);
Serial.println(humidity
100);
delay(refresh_rate);
// send request to receive data starting at register 0
{
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write((byte)0); // start at register 0
Wire.endTransmission();
Wire.requestFrom(0x68, 3); // request three bytes (seconds, minutes, hours)
while(Wire.available())
{
int seconds = Wire.read(); // get seconds
int minutes = Wire.read(); // get minutes
int hours = Wire.read(); // get hours
seconds = (((seconds & 0b11110000)>>4)*10 + (seconds & 0b00001111)); // convert BCD to decimal
minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert BCD to decimal
hours = (((hours & 0b00100000)>>5)*20 + ((hours & 0b00010000)>>4)*10 + (hours & 0b00001111)); // convert BCD to decimal (assume 24 hour mode)
Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.println(seconds);
String dataString= String(Num)+","+String(temperature)+","+String(humidity)+","+String(valCO2)+","+String(concentration)+","+String (hours)+","+String(minutes)+","+String (seconds);
delay (refresh_rate);
File logFile= SD.open(“LOG.csv”,FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else
{
Serial.println(“couldn’t open log File”);
}
Num++;

delay(refresh_rate);
}
}
}

void sendRequest(byte packet)
{
while(!K_30_Serial.available()) //keep sending request until we start to get a response
{
K_30_Serial.write(readCO2,7);
delay(refresh_rate);
}

int timeout=0; //set a timeoute counter
while(K_30_Serial.available() < 7 ) //Wait to get a 7 byte response
{
timeout++;
if(timeout > 10) //if it takes to long there was probably an error
{
while(K_30_Serial.available()) //flush whatever we have
K_30_Serial.read();

break; //exit and try again
}
delay(refresh_rate);
}

for (int i=0; i < 7; i++)
{
response = K_30_Serial.read();
}
}
unsigned long getValue(byte packet[])
{
int high = packet[3]; //high byte for value is 4th byte in packet in the packet
int low = packet[4]; //low byte for value is 5th byte in the packet
unsigned long val = high*256 + low; //Combine high byte and low byte with this formula to get value
return val* valMultiplier;
}
int shiftIn(int dataPin, int clockPin, int numBits) {

  • int ret = 0;*

  • for (int i=0; i<numBits; ++i) {*

  • digitalWrite(clockPin, HIGH);*

  • //delay(10); not needed :)*
    _ ret = ret*2 + digitalRead(dataPin);_

  • digitalWrite(clockPin, LOW);*

  • }*

  • return(ret);*
    }
    // send a command to the SHTx sensor
    void sendCommandSHT(int command, int dataPin, int clockPin) {

  • int ack;*

  • // transmission start*

  • pinMode(dataPin, OUTPUT);*

  • pinMode(clockPin, OUTPUT);*

  • digitalWrite(dataPin, HIGH);*

  • digitalWrite(clockPin, HIGH);*

  • digitalWrite(dataPin, LOW);*

  • digitalWrite(clockPin, LOW);*

  • digitalWrite(clockPin, HIGH);*

  • digitalWrite(dataPin, HIGH);*

  • digitalWrite(clockPin, LOW);*

  • // shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)*

  • shiftOut(dataPin, clockPin, MSBFIRST, command);*

  • // verify we get the right ACK*

  • digitalWrite(clockPin, HIGH);*

  • pinMode(dataPin, INPUT);*

  • ack = digitalRead(dataPin);*

  • if (ack != LOW)*

  • Serial.println(“ACK error 0”);*

  • digitalWrite(clockPin, LOW);*

  • ack = digitalRead(dataPin);*

  • if (ack != HIGH)*

  • Serial.println(“ACK error 1”);*
    }
    // wait for the SHTx answer
    void waitForResultSHT(int dataPin) {

  • int ack;*

  • pinMode(dataPin, INPUT);*

  • for(int i=0; i<100; ++i) {*

  • delay(10);*

  • ack = digitalRead(dataPin);*

  • if (ack == LOW)*

  • break;*

  • }*

  • if (ack == HIGH)*

  • Serial.println(“ACK error 2”);*
    }
    // get data from the SHTx sensor
    int getData16SHT(int dataPin, int clockPin) {

  • int val;*

  • // get the MSB (most significant bits)*

  • pinMode(dataPin, INPUT);*

  • pinMode(clockPin, OUTPUT);*

  • val = shiftIn(dataPin, clockPin, 8);*
    _ val *= 256; // this is equivalent to val << 8;_

  • // send the required ACK*

  • pinMode(dataPin, OUTPUT);*

  • digitalWrite(dataPin, HIGH);*

  • digitalWrite(dataPin, LOW);*

  • digitalWrite(clockPin, HIGH);*

  • digitalWrite(clockPin, LOW);*

  • // get the LSB (less significant bits)*

  • pinMode(dataPin, INPUT);*

  • val |= shiftIn(dataPin, clockPin, 8);*

  • return val;*
    }
    // skip CRC data from the SHTx sensor
    void skipCrcSHT(int dataPin, int clockPin){

  • pinMode(dataPin, OUTPUT);*

  • pinMode(clockPin, OUTPUT);*

  • digitalWrite(dataPin, HIGH);*

  • digitalWrite(clockPin, HIGH);*

  • digitalWrite(clockPin, LOW);*
    }

Code tags for your code please

Where do you suspect your point of failure is?

 String dataString= String(Num)+","+String(temperature)+","+String(humidity)+","+String(valCO2)+","+String(concentration)+","+String (hours)+","+String(minutes)+","+String (seconds);

There is just no excuse for pissing away memory like this. Writing to an SD card is already buffered. Writing one character at a time to the buffer or (trying to) write 100 characters at a time makes NO difference.

Pissing away memory allocating and de-allocating all these Strings is an atrocity.

How much memory do you have available when you do this?

http://playground.arduino.cc/Code/AvailableMemory

PaulS:
Pissing away memory allocating and de-allocating all these Strings is an atrocity.

How much memory do you have available when you do this?

Come on, most of the kids today are used to Gigs of RAM, how could 512 bytes be too much :wink:

Chuck.

Hello...
I am intarfacing Arduino GPS shield v1.1 with mega 2560 with. my gps is working fine but I didnt find how to log this data on Sd card??
please help.....waiting for your valuable feedback!
anybody can tell me what pin connection I need to do so???
...Sj

but I didnt find how to log this data on Sd card??

"this data"? WHAT data?

Why try adding MORE code to that mess that you have already? Write a SIMPLE sketch that logs anything. Does that write data to a file on the SD card? If so, then you know how to log useful data. If not, then you have a different problem AND you have simple code you can show us.