Questions about what code is doing in a DS18B20 temp sketch

Hello,

I am somewhat new to Arduino programming and am trying to understand exactly what is happening with certain parts of a sketch that reads the temperature of two DS18B20 temperature sensors. Specifically, I am interested to know details about 8 snipets of code which I copied from the sketch (entire sketch is at the bottom of this message.)

It would be very much appreciated if someone could provide specific details as to what the following 8 code snipets are doing. Thanks very much in advance!

Also, somehow the temperatures are getting passed to the variables insideThermometer and outsideThermometer and don’t understand the mechanism.

#1
DeviceAddress insideThermometer = { 0x28, 0x4A, 0x68, 0x79, 0x97, 0x7, 0x3, 0x6E };
DeviceAddress outsideThermometer = { 0x28, 0xB7, 0x56, 0x79, 0x97, 0x7, 0x3, 0x70 };

#2
void setup(void) ← I have seen "void setup() before but don’t know that (void) means here.

#3
void printTemperature(DeviceAddress deviceAddress)

#4
{
float tempC = sensors.getTempC(deviceAddress);

#5
if (tempC == -127.00) {
Serial.print(“Error getting temperature”);
} else {
Serial.print("C: “);
Serial.print(tempC);
Serial.print(” F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}

#6
}
void loop(void)
{

#7
sensors.requestTemperatures();

#8
Serial.print("Inside temperature is: ");
printTemperature(insideThermometer);

//*********** ENTIRE SKETCH BELOW****************

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

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

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

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0x4A, 0x68, 0x79, 0x97, 0x7, 0x3, 0x6E };
DeviceAddress outsideThermometer = { 0x28, 0xB7, 0x56, 0x79, 0x97, 0x7, 0x3, 0x70 };
//DeviceAddress atticThermometer = { 0x28, 0x8F, 0x67, 0x79, 0x97, 0x8, 0x3, 0x6D };

void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution (Can be 9 to 12 bits … lower is faster)
sensors.setResolution(insideThermometer, 11);
sensors.setResolution(outsideThermometer, 11);
// sensors.setResolution(atticThermometer, 11);
}

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

{
float tempC = sensors.getTempC(deviceAddress);

if (tempC == -127.00) {
Serial.print(“Error getting temperature”);
} else {
Serial.print("C: “);
Serial.print(tempC);
Serial.print(” F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}

void loop(void)
{
delay(3000);
sensors.requestTemperatures();

Serial.print(“Inside temperature is: “);
printTemperature(insideThermometer);
Serial.print(”\n\r”); // \n = skips to the next line, \r = carriage return, which tells the cursor to start at the next line.
Serial.print(“Outside temperature is: “);
printTemperature(outsideThermometer);
// Serial.print(”\n\r”);
// Serial.print(“Attic temperature is: “);
// printTemperature(atticThermometer);
Serial.print(”\n\r\n\r”);
}

Thank you,
Scott

Please use code-tags when posting code.

As these are all very, very basic questions I suggest you to take a look at the C/C++ language.
Some keywords: function, class, instance.

I have consulted references to C/C++. What I find is esoteric definitions of these things without concrete examples.
Thanks anyway. Maybe someone else will assist me.

Maybe if you tried to explain what you think they do to us, it might help you understand them.
Otherwise,it looks like you’re trying to get us to do your homework for you.

Please remember to use code tags when posting code.

Let’s try kick-starting the discussionvoid setup(void)  <-- I have seen "void setup() before but don't know that (void) means here. So, you know that the first “void” means “this function (setup) doesn’t return a value”.
Does that inform your best guess about what the “(void)” means, given your knowledge about function parameters?

Okay, great.
With regard to my first question, "void setup(void)" vs. "void setup()", I am thinking these two are equal. I can use either. Do you agree?
Thank you,
Scott

Sure. The first void means it doesn't return anything. The second void means it doesn't take any parameters and empty brackets mean the same thing.

Now have a go at what some of your other snippets are doing and we'll let you know when you get it right. E.g. #6 should be easy.

Steve

ScottJ2505:
Hello,
Also, somehow the temperatures are getting passed to the variables insideThermometer and outsideThermometer and don't understand the mechanism.

No, they're not - you've fallen foul of the infamous poorly named printTemperature routine. It could more correctly be called getTemperatureFromSensorUsingDeviceAddressAndThenPrintIt.

Sorry wildbill but I am not sure I follow you.

It looks to me like the variable "tempC" is being populated by "sensors.getTempC(deviceAddress); Then it appears to me that the value of "tempC" somehow gets passed to the variables "insideThermometer" and "outsideThermometer." I don't know how this happens.

ScottJ2505:
It looks to me like the variable "tempC" is being populated by "sensors.getTempC(deviceAddress);

Absolutely.

ScottJ2505:
Then it appears to me that the value of "tempC" somehow gets passed to the variables "insideThermometer" and "outsideThermometer."

Nope. insideThermometer and outsideThermometer contain device addresses, the unique codes burned into the sensors to differentiate them. printTemperature takes a device address and uses it to ask the sensor with that address what temperature it is reading. Then it prints the temperature to Serial. Nothing is returned to the main loop because nothing is needed. printTemperature (despite its name) took care of getting the temperature and displaying it.

Thanks Steve. Yes, my questions #2 and #6 are taken care of.
With respect to question one …

#1
DeviceAddress insideThermometer = { 0x28, 0x4A, 0x68, 0x79, 0x97, 0x7, 0x3, 0x6E };
DeviceAddress outsideThermometer = { 0x28, 0xB7, 0x56, 0x79, 0x97, 0x7, 0x3, 0x70 };

What I think is happening here is that the variables “insideThermometer” and “outsideThermometer” are being assigned the data type “DeviceAddress” and these two variables are each assigned a specific address unique to each of the two ds18b20 sensors.

Am I on the right track?

ScottJ2505:
#1
DeviceAddress insideThermometer = { 0x28, 0x4A, 0x68, 0x79, 0x97, 0x7, 0x3, 0x6E };
DeviceAddress outsideThermometer = { 0x28, 0xB7, 0x56, 0x79, 0x97, 0x7, 0x3, 0x70 };

What I think is happening here is that the variables “insideThermometer” and “outsideThermometer” are being assigned the data type “DeviceAddress” and these two variables are each assigned a specific address unique to each of the two ds18b20 sensors.

Am I on the right track?

No. insideThermometer is being defined as a variable of data-type DeviceAddress, and it is being assigned a value consisting of eight byte constants.

ScottJ2505:
#1

DeviceAddress insideThermometer = { 0x28, 0x4A, 0x68, 0x79, 0x97, 0x7, 0x3, 0x6E };

DeviceAddress outsideThermometer = { 0x28, 0xB7, 0x56, 0x79, 0x97, 0x7, 0x3, 0x70 };

Each DS18B20 has a unique 8-byte (64-bit) address on the OneWire bus. You can use the address to send commends to a specific device.

ScottJ2505:
#2

void setup(void)  <-- I have seen "void setup() before but don't know that (void) means here.

Same thing.

ScottJ2505:
#3

void printTemperature(DeviceAddress deviceAddress)

The “printTemperature()” function is passed the address of the device from which it should request the temperature.

ScottJ2505:
#4

{

float tempC = sensors.getTempC(deviceAddress);

This is the function that sends the command to read the stored temperature from a specific device.

ScottJ2505:
#5

 if (tempC == -127.00) {

Serial.print(“Error getting temperature”);
} else {
  Serial.print("C: “);
  Serial.print(tempC);
  Serial.print(” F: ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
}

The value -127.00 is an error code that means something went wrong. Perhaps the addresses device does not exist. The DallasTemperature::toFahrenheit() function does the conversion from Celsius to Farenheit.

ScottJ2505:
#6

}

void loop(void)
{

This is the end of the function above loop() followed by the declaration of the function ‘loop()’.

ScottJ2505:
#7

sensors.requestTemperatures();

This is a broadcast command to tell ALL thermometers on the OneWire bus to measure and store the temperature. Depending on the voltage and resolution this can take up to 3/4 of a second. When the temperature is requested from a particular device you will get the stored temperature.

ScottJ2505:
#8

 Serial.print("Inside temperature is: ");

printTemperature(insideThermometer);

This is a .print() and a call to printTemperature(), described above.