Temperature logger only logs zeros

I'm trying to create a two probe temperature logger using an Uno that records data via SD shield. The Serial Monitor displays the correct temperatures but when I plug the SD card into the computer it has created a file and logged nothing but zeros. I've attached my code.

Can anyone help me figure out what I'm doing wrong?

TwoProbeLoggerCode.ino.ino (4 Bytes)

Please read the "how to use this forum " and post your code in the proper manner using </>

Sorry about that. Code is now in the proper format.

#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SD.h>
#include <string.h> 

#define TEMP_BUS_ONE 16 // first temp: A2
#define TEMP_BUS_TWO 17 // second temp: A3

int counterTemp = 0;
float dataOne;
float dataTwo;
char test[20];

const int chipSelect = 10;

String dataString;

OneWire a2OneWire(TEMP_BUS_ONE);
DallasTemperature tempOne(&a2OneWire);

OneWire a3OneWire(TEMP_BUS_TWO);
DallasTemperature tempTwo(&a3OneWire);

File myFile; 

void setup(void)
{
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  tempOne.begin();
  Serial.println(tempOne.getDeviceCount(), DEC);
  tempTwo.begin();
  Serial.println(tempTwo.getDeviceCount(), DEC);
  
   // SET UP SD CARD FOR WRITING
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
} else {
    Serial.println("SD card initialized");
    myFile = SD.open("temp.txt", FILE_WRITE);    // open file for writing
   if (myFile) {  // if file can be opened, write to it
      Serial.println("temperature.txt file opened for writing");
   
  } else {  // if not, show an error
      Serial.println("ERROR: not able to open temperature.txt");
    }
  }
   
}

void loop(void)
{
  tempOne.requestTemperatures(); // Send the command to get temperatures
  tempTwo.requestTemperatures();
  
  dataString = "";
  dataOne = 0;
  dataTwo = 0;
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
 // Serial.print(" Requesting temperatures...");
  tempOne.requestTemperatures(); // Send the command to get temperatures
  tempTwo.requestTemperatures();
//  Serial.println("DONE");

  Serial.print("Inlet Temperature is: ");
  if(tempTwo.getTempCByIndex(0) < 0){
    float dataTwo = 0;
    Serial.println(1111);
    Serial.println(floatToString(test, dataTwo, 1, 5));
    }
  else{
    float dataTwo = tempTwo.getTempCByIndex(0);
    Serial.print(floatToString(test, dataTwo, 1, 5));
    }
  
  Serial.print("Outlet Temperature is: ");
  if(tempOne.getTempCByIndex(0) < 0){
    float dataOne;
    Serial.println(floatToString(test, dataOne, 1, 5));
    }
  else{
    float dataOne = tempOne.getTempCByIndex(0);
    Serial.println(floatToString(test, dataOne, 1, 5));
  }
 delay(1000);   
// writeDataToCard(counterTemp, dataOne);
 
  delay(1000);   
// writeDataToCard(counterTemp, dataTwo);  
  delay(1000);
  counterTemp++;

// write data to SD card
  myFile = SD.open("temp.txt", FILE_WRITE);
  myFile.print(floatToString(test, dataOne, 1, 5));
  myFile.print(", ");
  myFile.print(floatToString(test, dataTwo, 1, 5));
  myFile.print("\n");
  myFile.close();
}
char * floatToString(char * outstr, double val, byte precision, byte widthp){
  char temp[16];
  byte i;
  
  // compute the rounding factor and fractional multiplier
  double roundingFactor = 0.5;
  unsigned long mult = 1;
  for (i = 0; i < precision; i++)
  { 
    roundingFactor /= 10.0;
    mult *= 10;
  }
  
  temp[0]='\0';
  outstr[0]='\0';

  if(val < 0.0){
    strcpy(outstr,"-\0");
    val = -val;
  }

  val += roundingFactor;

  strcat(outstr, itoa(int(val),temp,10));  //prints the int part
  if( precision > 0) {
    strcat(outstr, ".\0"); // print the decimal point
    unsigned long frac;
    unsigned long mult = 1;
    byte padding = precision -1;
    while(precision--)
      mult *=10;

    if(val >= 0)
      frac = (val - int(val)) * mult;
    else
      frac = (int(val)- val ) * mult;
    unsigned long frac1 = frac;

    while(frac1 /= 10)
      padding--;

    while(padding--)
      strcat(outstr,"0\0");

    strcat(outstr,itoa(frac,temp,10));
  }

  // generate space padding 
  if ((widthp != 0)&&(widthp >= strlen(outstr))){
    byte J=0;
    J = widthp - strlen(outstr);
    
    for (i=0; i< J; i++) {
      temp[i] = ' ';
    }

    temp[i++] = '\0';
    strcat(temp,outstr);
    strcpy(outstr,temp);
  }
  return outstr;
}

OK, that's better. The code is junk, and pointless. It seems to be a mix of several different bits of junk, and the fact that you have had any result at all is quite a triumph. The first question to ask is: why have a one wire setup when you are using two wires? Don't bother to answer.

You will get better advice here.

note that there are two programmes to use, one to sniff the address and the other to use the sensor.

Note particularly that the DS18B20 will return a figure that is a float. All you need to do is send that to serial

Serial.println(temp);

and SD

myFile.print(temp);

all that string and char stuff is baloney.

The first question to ask is: why have a one wire setup when you are using two wires? Don't bother to answer.

They are short buses. So, two are needed.

The code was, and still may be a disaster. I'm new and this and tried to piece together different code. I cleaned it up and got rid of the unnecessary, or at least I've tried to. It's posted below. Any advice?

#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SD.h>

#define TEMP_BUS_ONE 16 // first temp: A2
#define TEMP_BUS_TWO 17 // second temp: A3

float dataOne;
float dataTwo;
const int chipSelect = 10;

OneWire a2OneWire(TEMP_BUS_ONE);
DallasTemperature tempOne(&a2OneWire);

OneWire a3OneWire(TEMP_BUS_TWO);
DallasTemperature tempTwo(&a3OneWire);

File myFile; 

void setup()
{
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  
  tempOne.begin();
  Serial.println(tempOne.getDeviceCount(), DEC);
  
  tempTwo.begin();
  Serial.println(tempTwo.getDeviceCount(), DEC);
  
   // SET UP SD CARD FOR WRITING
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;} 
  else {
    Serial.println("SD card initialized");
    myFile = SD.open("temp.txt", FILE_WRITE);}   // open file for writing
  if (myFile) {                                  // if file can be opened, write to it
      Serial.println("temperature.txt file opened for writing");}
  else {                                          // if not, show an error
      Serial.println("ERROR: not able to open temperature.txt");}
}

void loop()
{
  tempOne.requestTemperatures(); // Send the command to get temperatures
  tempTwo.requestTemperatures();

  Serial.print("Inlet Temperature is: "); // Display temperature
  if(tempTwo.getTempCByIndex(0) < 0){
    Serial.println("Error");}
  else{
    float dataTwo = tempTwo.getTempCByIndex(0);
    Serial.print(dataTwo); }
    
  Serial.print(" ");
  
  Serial.print("Outlet Temperature is: ");
  if(tempOne.getTempCByIndex(0) < 0){
    Serial.println("Error");}
  else{
    float dataOne = tempOne.getTempCByIndex(0);
    Serial.println(dataOne);}  
 delay(1000); 
 
// write data to SD card
  myFile.print(dataOne);
  myFile.print(", ");
  myFile.print(dataTwo);
  myFile.print(", ");
  myFile.print("\n");
  myFile.close();
}

sigmundo:
Any advice?

Not much, I'm afraid - other than read reply #3 again.

  myFile.print(dataOne);

myFile.print(", ");
  myFile.print(dataTwo);
  myFile.print(", ");
  myFile.print("\n");
  myFile.close();
}

You are closing the file but not opening it, so don't expect too much on the card. The second comma is redundant.

  else{
    float dataTwo = tempTwo.getTempCByIndex(0);
    Serial.print(dataTwo); }

Getting the temperature from the second sensor, and storing the value in a variable that immediately goes out of scope seems rather pointless.

  else{
    float dataOne = tempOne.getTempCByIndex(0);
    Serial.println(dataOne);}

Getting the temperature from the first sensor, and storing the value in a variable that immediately goes out of scope seems rather pointless.

Writing global variables that you've never assigned new values to, to a file that you haven't opened, seems pointless, too.

Do you have any recommendations for a better way to get and store the data? I'm getting this code from tutorials and not sure why or how the variable goes out of scope.

PaulS:

  else{

float dataTwo = tempTwo.getTempCByIndex(0);
    Serial.print(dataTwo); }



Getting the temperature from the second sensor, and storing the value in a variable that immediately goes out of scope seems rather pointless.

Since PaulS didn't go into the details of his point, I'll try to explain according to my limited understanding.
You need to be aware of the difference between local and global variables

In short, if you declare a new variable, that means create an new variable by writing the type of variable in front of it (in your case "float"), then this variable will only exist inside the brackets where it was created. As soon as the bracket closes, this new variable is deleted for ever.
That is, the scope (the area where this variable is valid) only goes to the closing bracket, and then the variable goes "out of the scope" and dies.
So in your case, the variable dies after

  Serial.println(dataOne);}

and your temperature value ist lost for ever.

So what's the point of local variables?

  • Variables use RAM, which is limited. An erased variable frees up space in RAM.

  • Especially with more complex code, the number of variables can get quite large, and it can get quite confusing if variables are modified in several parts of the code. A local variable can only get modified inside its function, and it can't mess up other parts of the code. This helps preventing bugs.

I hope I have clarified this a bit.
Thomas

Thank you for explaining, I got rid of the Float and now it logs real data!!!

Solved.