[SOLVED] getting 12b data from DS1820

I'm trying to convert from picaxe to arduino and from 30 years of BASIC (starting from GWBASIC that came free with DOS 3.3) to C++ and it ain't pretty. I've used the 1820 w/ picaxe and got the diferent resolutions but haven't been successful w/ Arduino. I used the

#define TEMPERATURE_PRECISION 12
DeviceAddress tempDeviceAddress;
and in setup...
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);

from the DallasTemperature.h library and the data still comes back in .5 degrees. To display I've got . . .

sensors.requestTemperatures();
float T=sensors.getTempCByIndex(0);
msg =+ dt.day; msg += ", "; msg += dt.hour; msg +=":"; msg +=dt.minute; msg += ", "; msg += T;
Serial.println(msg);

All the examples I can find only use 9bit. Info seems narrow and repetitive or way over my head. I haven't even found a site that says "this is the minimum you need to make it work."

PS It's ok to show me I'm an idiot and don't know anything (which is why I'm asking) just don't say it!

hmmm. I wonder if the problem is with "tempDeviceAddress". Does that just create the variable but it has no value?

Post ALL the code, using code tags.

uhhh, what are code tags? Do you want the REMs? (//)

Please read and follow the directions in the "How to use this forum" post.

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

// Data wire is plugged into port 2 on the Arduino //BA->A5
#define ONE_WIRE_BUS 8 //A0 //2
#define TEMPERATURE_PRECISION 12 //9 // Lower resolution

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

//int numberOfDevices; // Number of temperature devices found
//float tempC;
String msg="";
boolean lastState;

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;

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

const int chipSelect = 10;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  pinMode(A2,INPUT); //_PULLUP

  
  // Start up the library
  sensors.begin();
  // Grab a count of devices on the wire
  //numberOfDevices = sensors.getDeviceCount();
 //  if (sensors.getResolution(tempDeviceAddress)!=TEMPERATURE_PRECISION){ // only change if different
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
 //   }
  // Initialize DS3231
///  Serial.println("Initialize DS3231"); //unREM if serial ON
  clock.begin();
  //unREM next line to set clock to compile time
  //ClockSet();
  
  //Serial.print("# "); Serial.println(numberOfDevices,DEC);           //for testing
  //  PrtTProbeInfo();  //unREM & serial ON to prt temp probe info

 ///  Serial.print("Initializing SD card...");

  // 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.");
  lastState = !digitalRead(A2); // set init state so writes first time
  dt = clock.getDateTime();
}

void loop(void)
{ 
  sensors.requestTemperatures();
  float T=sensors.getTempCByIndex(0); //at beg to start w reading
  //msg="";
  msg =+ dt.day; msg += ", "; msg += dt.hour; msg +=":"; msg +=dt.minute; msg += ", "; msg += T;
 /// Serial.println(msg);
  LogIt();
   
  for(int i=0;i<10;i++){ // se if compressor is on once per min
    if(digitalRead(A2)!=lastState){ //if chg state, log time
      lastState=digitalRead(A2);
      dt = clock.getDateTime();
     // msg="";
      msg =+ dt.day; msg += ", "; msg += dt.hour; msg +=":"; msg +=dt.minute; msg += ",, "; msg += lastState;
    ///  Serial.println(msg);
      LogIt();
      }  
 ///   Serial.print(i); //   Serial.println(i);
  delay(59950);  
  }
///  Serial.println("loop done");

  //delay(49999);
  
  //do //wait for new min to correct or drift in loop
  dt = clock.getDateTime();
  while(dt.second < 2){  //  sensors.requestTemperatures(); // just 0 // Send the command to get temperatures
  
///     Serial.print(". ");
    delay(300);
    dt = clock.getDateTime();
  } 


}

//**************** END *****************

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{


  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}


//function to set ds3231 time to compile time
void ClockSet()
{
  // Initialize DS3231
  //Serial.println("Initialize DS3231");; //unREM if serial ON
  clock.begin();

  // Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);

  // Manual (YYYY, MM, DD, HH, II, SS
  // clock.setDateTime(2016, 12, 9, 11, 46, 00);
}


//function to get ds3231 
void ClockGet()
{
  dt = clock.getDateTime();

  Serial.print("Long number format:          ");
  Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));

  Serial.print("Long format with month name: ");
  Serial.println(clock.dateFormat("d F Y H:i:s",  dt));

  Serial.print("Short format with 12h mode: ");
  Serial.println(clock.dateFormat("jS M y, h:ia", dt));

  Serial.print("Today is:                    ");
  Serial.print(clock.dateFormat("l, z", dt));
  Serial.println(" days of the year.");

  Serial.print("Actual month has:            ");
  Serial.print(clock.dateFormat("t", dt));
  Serial.println(" days.");

  Serial.print("Unixtime:                    ");
  Serial.println(clock.dateFormat("U", dt));

  Serial.println();

  //or...

  // For leading zero look to DS3231_dateformat example (copied above)
  Serial.print("Raw data: ");
  Serial.print(dt.year);   Serial.print("-");
  Serial.print(dt.month);  Serial.print("-");
  Serial.print(dt.day);    Serial.print(" ");
  Serial.print(dt.hour);   Serial.print(":");
  Serial.print(dt.minute); Serial.print(":");
  Serial.print(dt.second); Serial.println("");

  delay(1000);
}

// Write to SD Card
void LogIt()
{

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(msg);
    Serial.println(msg);

    dataFile.close();


  }

}

HA! you can teach an old dog new trick! Bow-wow!

I removed larger blocks of comments but not all.

I had skimmed "how to use" but didn't pick up on that.

It's largely cobbled together from various examples to make a datalogger to register time when the compressor goes on/off and the temp every 10 minutes. Most examples include extra features and the difficulty is in trimming what you don't need without killing prep for what you do need - as happened when I called getTempCByIndex(0) without calling requestTemperatures().

All the examples I can find only use 9bit. Info seems narrow and repetitive or way over my head. I haven't even found a site that says "this is the minimum you need to make it work."

Try here

I'm not sure your #define TEMPERATURE_PRECISION 12is kosher, it might be fine but common use is
  sensors.setResolution(nameofThermometer, number); One trick from this older dog is that DS18B20 is 12 bit by default, so you don't need it anyway. I think you should also get 2 sigdig by default with floats, but I'm not sure if that is a problem for you.

It may be 12 by default, but when I used it last w/ picaxe I left it in 9. I think it remembers.

It seems the #define TEMPERATURE_PRECISION 12 just puts the 12 in a constant to simplify later use. My error seems to have been that the code I copied created a variable to the address/name of the thermometer but I left out the code to fill the variable.

And don’t forget there are different versions of this device that allow different resutions , the DS182O is the basic 0.5C version

SOLVED: I was using EXAMPLE code

sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);

but the variable tempDeviceAddress was empty. I inserted

if(sensors.getAddress(tempDeviceAddress, 0));{

upstream (w/ } after) and it works now.

Just in case someone else hits this...