Reposting Code (somewhat fixed) that needs some help

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);
}

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 You think So?
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 I think you just killed off your "Saved" variable
}

Doc

@ Docedison: I hadn't even thought about that part. It was working fine so I hadn't even thought about looking it over. Thank you for pointing it out.

My main problem is with the ReadFrequency function and adding it to the dataString.

wwdd:
In Red is the addition that is giving me errors

Unfortunately, code tags don't support coloring.

What errors are you getting? Does it compile? What is the problematic code intended to do? What does it actually do?

wwdd:
Can anyone give me some pointers as to what I have done wrong? It mostly has to do with the dataString at the moment.

  String dataString = "";

[…]
 dataString += ReadFrequency();
[…]

void ReadFrequency()  {
[…]
}

Your first problem is you are adding the return value of the ReadFrequency() call to a String object when ReadFrequency() has no return value (since it’s type void).

EDIT: Sorry I was an idiot, but where does your Frequency Counter library come from? I noticed that if I comment out the line

#include <FreqCounter.h>

I get the same compile errors, and I don’t really understand that. But as I stated before, the other compile problems are around the “FreqCounter::” references which don’t appear to make any sense. I’d expect your code should declare an object of type FreqCounter and then use that object’s reference (maybe because you didn’t declare an FreqCounter object there was no compile error when the include was omitted?)

Regards,

Brad
KF7FER

Docedison:

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 You think So?
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 I think you just killed off your "Saved" variable
}

Doc

Doc-

If you look at the line below your comment "You think So?", you'll see he uses Temp 4 times.
So isn't he saving at least 3 more calculations? Sorry if your point is 3 vs 4.

But isn't the bigger problem him trying to return the value of Temp after the function ends? I didn't think it was ok to do that. Not to mention the function's return type is double, but it actually returns (a bad reference to) a float?

Regards,

Brad.

  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later  You think So?
  Temp = 1 / (0.00032939375 + (0.00044329916 * Temp) + (-0.00000077648923 * Temp * Temp * Temp));

Yup, he uses the 1st Temp 4 times in the next line.

However 0.00032939375, 0.00044329916, and -0.00000077648923 all need 8 significant digits and for the first you can have 7 while for the other two you can have 6 since the 1st digit is more than 3.

Something tells me this part does not exactly run fast…

Not the best coding practice to use a global variable as both a temp 'register' and for saving a value for another function... Not something I'd do

Something tells me this part does not exactly run fast.....

Or well. However how much different his mistakes than ours? When We were Young...

Doc

He used the same variable to develop the value that went into it.
As to the speed of the calculation, isn't there some more time-critical part to do with measuring frequency?

I have old 4-bit, 100k clock (or is it 10k? TI-56 & TI-59) calculators that take noticeable time to do a divide....