Bring together several sketches

Hi A bit of help please? I am trying to obtain a reading from an IR light and then add a date from a REAL Time Clock and log it to a SD card. I will replace the Temperature pin with the Real Time Clock as this is on 4 and 5. I have been able to do this ok with each individual component, but no success when I try and change the sketch below to bring them all together. –

#include <SD.h> //SD Card Library
#include <Wire.h> //I2C Library

//SPI SD Card Pins
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
int CS_pin = 8;
int pow_pin = 8;

//I2C Temperature Pins
//SDA = Analog Pin 4
//SCL = Analog Pin 5

//IR Distance Sensor Pins
int IR1_pin = 0;
int IR2_pin = 1;

//Light Sensor Pins
int light_pin = 2;

float refresh_rate = 0.0; //Dataloger Refresh Rate
int temp_address = 72; //Address of the I2C Temp Sensor
long id = 1; //Use this to store the id # of our reading.

void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println(“Initializing Card”);
//CS Pin is an output
pinMode(CS_pin, OUTPUT);

//SD Card will Draw Power from Pin 8, so set it high
pinMode(pow_pin, OUTPUT);
digitalWrite(pow_pin, HIGH);

//Initialize Card
if (!SD.begin(CS_pin))
{
Serial.println(“Card Failure”);
return;
}
Serial.println(“Card Ready”);

//Read the Configuration information (COMMANDS.txt)
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 read command file.”);
return;
}

//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 = “ID, Light, IR1, IR2”;
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println(“Couldn’t open log file”);
}

}

void loop()
{
//Check Light Level
int light_level = analogRead(light_pin);

//Read Distances
int IR1_val = analogRead(IR1_pin);
int IR2_val = analogRead(IR2_pin);

//Create Data string for storing to SD card
//We will use CSV Format
String dataString = String(id) + ", " + String(light_level) + ", " + String(IR1_val) + ", " + String(IR2_val);

//Open a file to write to
//Only one file can be open at a time
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”);
}

//Increment ID number
id++;

delay(refresh_rate);[/color]

The Serial com responds with -

Initializing Card
Card Ready
Reading Command File
Refresh Rate = 1000.00ms
ID, Light, IR1, IR2
1, 476, 332, 320

3
3
3
3

Any advice appreciated.

Any advice appreciated.

Keep in mind that you did say this.

Post your code properly, using the # icon. I do not read red code, and plenty of other people get annoyed at improperly posted code, too.

I hope this is more acceptable ?

#include <SD.h>         //SD Card Library
#include <Wire.h>       //I2C Library

//SPI SD Card Pins
  //MOSI = Pin 11
  //MISO = Pin 12
  //SCLK = PIN 13
  int CS_pin = 8;
  int pow_pin = 8;
  
//I2C Temperature Pins
  //SDA = Analog Pin 4
  //SCL = Analog Pin 5
  
//IR Distance Sensor Pins
  int IR1_pin = 0;
  int IR2_pin = 1;
  
//Light Sensor Pins
  int light_pin = 2;

float refresh_rate = 0.0;  //Dataloger Refresh Rate
int temp_address = 72;     //Address of the I2C Temp Sensor
long id = 1;                //Use this to store the id # of our reading.

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);
  
  //SD Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT);  
  digitalWrite(pow_pin, HIGH);
  
  //Initialize Card
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
  
  //Read the Configuration information (COMMANDS.txt)
  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 read command file.");
    return;
  }
  
  //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 = "ID, Light, IR1, IR2";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
}

void loop()
{
  //Check Light Level
  int light_level = analogRead(light_pin);
  
    //Read Distances
  int IR1_val = analogRead(IR1_pin);
  int IR2_val = analogRead(IR2_pin);
  
  //Create Data string for storing to SD card
  //We will use CSV Format  
  String dataString = String(id) + ", " + String(light_level) + ", " + String(IR1_val) + ", " + String(IR2_val); 
  
  //Open a file to write to
  //Only one file can be open at a time
  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");
  }
  
  //Increment ID number
  id++;
  
  delay(refresh_rate);[/font][/font]

and it returned this via the serial Com

Initializing Card
Card Ready
Reading Command File
Refresh Rate = 1000.00ms
ID, Light, IR1, IR2
1, 476, 332, 320

3
3
3
3
    float decade = pow(10, (commandFile.available() - 1));

Why is decade a float?

      float temp = (commandFile.read() - '0');

Why is temp a float? Why temp? Couldn't you find a more meaningful name?

I don't see how that output is generated from that code. I also don't see why you can't/don't/won't print identifying information before the output:

Serial.print("dataString = [");
Serial.print(dataString);
Serial.println("]");

that produces output like: dataString = [1, 476, 332, 320] would be much more meaningful.

If that code then printed dataString = [3] dataString = [3] dataString = [3] dataString = [3] then we'd have something to go on.

By the way, there is nothing in this code that needs the huge overhead of the String class. I'd strongly recommend that you ditch it.

Thanks for the response. I fear I may have to start from the begining with this to try and link everything back again.

This is the latest effort -

//SD Card Demonstration
//Some code from public domain work by Tom Igoe

#include <SD.h>         //SD Card Library
#include <Wire.h>       //I2C Library

//SPI SD Card Pins
  //MOSI = Pin 11
  //MISO = Pin 12
  //SCLK = PIN 13
  int CS_pin = 8;
  int pow_pin = 8;
  
//I2C Temperature Pins
  //SDA = Analog Pin 4
  //SCL = Analog Pin 5
  
//IR Distance Sensor Pins
  int IR1_pin = 0;
  int IR2_pin = 1;
  
//Light Sensor Pins
  int light_pin = 2;

int temp_address = 72;     //Address of the I2C Temp Sensor
long id = 1;                //Use this to store the id # of our reading.

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);
  
  //SD Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT);  
  digitalWrite(pow_pin, HIGH);
  
  //Initialize Card
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
  
  //Read the Configuration information (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");
    
       Serial.print("dataString = [");
Serial.println("]");
    commandFile.close();
  }
  else
  {
    Serial.println("Could not read command file.");
    return;
  }
  
  //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 = "ID, Light, IR1, IR2";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
}

void loop()
{
  //Check Light Level
  int light_level = analogRead(light_pin);
  
    //Read Distances
  int IR1_val = analogRead(IR1_pin);
  int IR2_val = analogRead(IR2_pin);
  
  //Create Data string for storing to SD card
  //We will use CSV Format  
  String dataString = String(id) + ", " + String(light_level) + ", " + String(IR1_val) + ", " + String(IR2_val); 
  
  //Open a file to write to
  //Only one file can be open at a time
  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");
  }
  
  //Increment ID number
  id++;
  
  delay(50);
}

with this response

Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 466, 344, 301
Initializing Card
Card Ready
Re°R+?¥?±¥é¥¹Card
Card Ready
ReR+?¥?±¥é¥¹Card
Card Ready
Reading Command File
dataString = []
ID,Initializing Card
Card Ready
Reading Command FilInitializing Card
Card ReadyInitializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 285, 339, 317
2, 339, 328, 328
Initializing Card
Card Ready
Initializing Card
Card Read½R+?¥?±¥é¥¹Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 328, 330, 327
2, 312, 328, 320
Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 331, 333, 327
2, 280, 326, 312
Initializing Card
Card ReadùCR+?¥?±¥é¥¹Card
Card Read¹R+?¥?±¥é¥¹Card
Card Read^R+?¥?±¥é¥¹Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, IJ¹¥Ñ¥?±¥é¥¹Card
Card Ready
ReXR+?¥?±¥é¥¹Card
Card Reaä^R+?¥?±¥é¥¹Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 720, 332, 366
2, 461, 332, 321
Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 376, 331, 317
2, 379, 320, 345
Initializing Card
Card Read¹CR+?¥?±¥é¥¹Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 314, 330, 322
2, 312, 358, 327
Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 317, 331, 323
2, 291, 328, 312
Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 296, 330, 318
2, 330, 328, 320
Initializing Card
Card ReadyInitializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
Initializing Card
Card Ready
Reading Command File
dataString = []
ID, Light, IR1, IR2
1, 281, 355, 321

So, something is causing your sketch to reset.

  if (commandFile)
  {
    Serial.println("Reading Command File");
    
       Serial.print("dataString = [");
Serial.println("]");
    commandFile.close();
  }

Please, please, use the Tools + Auto format menu option before posting code.

I know that the SD library uses a lot of SRAM memory. The overuse of the String class is not helping. I suspect that you are running out of memory.

Dumping the String class, and simply using 6 print() calls and 1 println() call will save a bunch of memory.