Here is my code, corrected to have both sketches merged (from earlier post). In Red is the addition that is giving me errors. Can anyone give me some pointers as to what I have done wrong? It mostly has to do with the dataString at the moment.
#include <math.h>
#include <SD.h>
#include "Wire.h"
#include "RTClib.h"
#include <FreqCounter.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------
// A simple data logger for the Arduino analog pins
#define LOG_INTERVAL 1000 // mills between entries
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
const int chipSelect = 10;
RTC_DS1307 RTC;
// the logging file
File datafile;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
//---------------------------------------------------------------------------------------------------------------------------------------------------
void setup(void) {
Serial.begin(9600);
Serial.println();
Wire.begin();
// initialize the SD card
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
String printDouble( double val, unsigned int precision){
// prints val with number of decimal places determine by precision
String retval;
retval += String(int(val));
retval += ".";
unsigned int frac;
if(val >= 0)
frac = (val - int(val)) * precision;
else
frac = (int(val)- val ) * precision;
retval += frac;
return retval;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
float pad = 2267; // balance/pad resistor value
float thermr = 2001; // thermistor nominal resistance
double Thermister(int RawADC) {
delay(100);
long Resistance;
float Temp; // Dual-Purpose variable to save space.
Resistance=((1024 * pad / RawADC) - pad);
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later
Temp = 1 / (0.00032939375 + (0.00044329916 * Temp) + (-0.00000077648923 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celsius
return Temp; // Return the Temperature
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
void printTherm(void) {
double therm = Thermister(analogRead(0)); // Read sensor
#if ECHO_TO_SERIAL
Serial.print("Therm Temperature is: ");
Serial.print(therm,2);
Serial.print((char)176);("Date, Time, ThermT, Freq, Pressure");
Serial.println("");
#endif //end ECHO_TO_SERIAL
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
void loop(void) {
[color=red]// make a string for assembling the data to log:
String dataString = getDateDs1307();
String sensor1 = printDouble (Thermister(analogRead(0)),100);
dataString += sensor1;
dataString += ", ";
dataString += ReadFrequency();
dataString += ", ";[/color]
#if ECHO_TO_SERIAL
printTherm();
#endif
File dataFile = SD.open("logger.csv", FILE_WRITE);
if (dataFile)
{
datafile.println("\n");///just a blank line
datafile.println("Date, Time, ThermT, Freq, Pressure");
datafile.close();
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
#if ECHO_TO_SERIAL
Serial.println("\nDate, Time, ThermT, Freq, Pressure");
Serial.println(dataString);
Serial.println("");
#endif
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening test.csv");
}
delay(20000);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
void setDateDs1307()
{
// Use of (byte) type casting and ascii math to achieve result.
second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
dayOfWeek = (byte) (Serial.read() - 48);
dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(byte(0x00));
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
String getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.read() & 0x7F);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3F);
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
String dataString = "";
dataString += Print2Digit(dayOfMonth);
dataString += String("/");
dataString += Print2Digit(bcdToDec(month));
dataString += String("/"); // Y2k1 bug!
dataString += Print2Digit(bcdToDec(year));
dataString += String(", ");
dataString += Print2Digit(hour);
dataString += String(":");
dataString += Print2Digit(minute);
dataString += String(":");
dataString += Print2Digit(second);
return dataString;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
String Print2Digit(byte Val)
{
String dataString = "";
if (Val < 10)
{
dataString = "0";
}
dataString += String(Val, DEC);
return dataString;
}
[color=red]void ReadFrequency() {
long int frq;
FreqCounter::f_comp = 8;
FreqCounter::start(1000);
while (FreqCounter::f_ready == 0);
{/*Do Nothing*/}
frq=FreqCounter::f_freq;
#if ECHO_TO_SERIAL
Serial.println("");
Serial.print("Frequency: ");
Serial.print(frq);
Serial.println(" Hz");
#endif
//----------------------------------------
//------ Calculate Pressure ------
//----------------------------------------
double A = -4.170459900908042e-11;
double B = -0.04637910393309745;
double C = 1765.6201656883327;
double Press = ((A*frq*frq)+(B*frq)+C);
#if ECHO_TO_SERIAL
Serial.print("Pressure: ");
Serial.print(Press);
Serial.println(" PSI");
#endif
dataString += Print2Digit(frq);
dataString += String(", ");
dataString += Print2Digit(Press);
dataString += String(", ");
[/color]
delay(100);
}