Are Pointer's the Answer?

Good Day!
Hacktronics was nice enough to do the hard work on reading ds18b20 sensors for me, and I have a working sketch to read and display 8 sensors as I require. The problem is the code just isn’t elegant. It seems the repetitive display of the sensor values is loop fodder for sure, both to the serial port and to the LCD (which has delayed display of each sensor).

There are definitions that specify “DeviceAddress” and assign a variable name. (Sens1, Sens2…) I need to figure out how to pass that name to a loop, changing it each iteration. It appears I may need to point to the defined name in some way, as everything I’ve tried has either given me type errors, scope errors, or just plain not worked to display the values. Each attempt I’ve made, is attempting to change “Sensx” x value, such as concatenation, and separate variables for the “x” value, have failed, but I don’t think I have enough understanding of pointers to try them. (Basically nothing in the reference pages for pointers and confusion from the web)

#include <DallasTemperature.h>
#include <LiquidCrystal.h>

// This Arduino sketch reads DS18B20 “1-Wire” digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

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

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

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

// initialize the Liquid Crystal library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);[/color]

// 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 Sens1 = { 0x28, 0x01, 0xD5, 0xAF, 0x01, 0x00, 0x00, 0x3D };
DeviceAddress Sens2 = { 0x28, 0x63, 0xC2, 0xB1, 0x01, 0x00, 0x00, 0x90 };
DeviceAddress Sens3 = { 0x28, 0x6D, 0xF1, 0xB1, 0x01, 0x00, 0x00, 0x49 };
DeviceAddress Sens4 = { 0x28, 0x70, 0xFA, 0xB0, 0x01, 0x00, 0x00, 0xA7 };
DeviceAddress Sens5 = { 0x28, 0x90, 0xB9, 0xB1, 0x01, 0x00, 0x00, 0xA6 };
DeviceAddress Sens6 = { 0x28, 0xEB, 0x6A, 0xB1, 0x01, 0x00, 0x00, 0xD6 };
DeviceAddress Sens7 = { 0x28, 0xEB, 0x72, 0xB1, 0x01, 0x00, 0x00, 0x94 };
DeviceAddress Sens8 = { 0x28, 0xF1, 0x1A, 0xB1, 0x01, 0x00, 0x00, 0x2F };

void setup(void)
{
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);

//Cursor start point
lcd.setCursor(0, 0);

Serial.begin(9600);

sensors.begin();
// set the resolution to 10 bit)
sensors.setResolution(Sens1, 10);
sensors.setResolution(Sens2, 10);
sensors.setResolution(Sens3, 10);
sensors.setResolution(Sens4, 10);
sensors.setResolution(Sens5, 10);
sensors.setResolution(Sens6, 10);
sensors.setResolution(Sens7, 10);
sensors.setResolution(Sens8, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print(“Error getting temperature”);
lcd.print(“Err get temp”);
}
else {
Serial.print(“C: “);
Serial.print(tempC);
Serial.print(” F: “);
Serial.print(DallasTemperature::toFahrenheit(tempC));
lcd.print(“C:”);
lcd.print(tempC);
lcd.print(” F:”);
lcd.print(DallasTemperature::toFahrenheit(tempC));
}
}

void loop(void)
{
lcd.setCursor(0, 0);
Serial.print(“Getting temperatures…\n\r”);
lcd.print(“Getting temps…”);
sensors.requestTemperatures();

Serial.print("\n\r");
Serial.print(“Sens1 is: “);
printTemperature(Sens1);
Serial.print(”\n\r”);
Serial.print("Sens2 is: ");
printTemperature(Sens2);

REMOVED IDENTICAL BLOCKS OF CODE FOR Sens3-Sen8 TO FIT FORUM

//Print each sensor to LCD with 5 second delay.
lcd.setCursor(0, 0);
lcd.clear();
lcd.print(“Sens1 is: “);
lcd.setCursor(0, 1);
printTemperature(Sens1);
Serial.print(”\n\r”);
delay(5000);

lcd.setCursor(0, 0);
lcd.clear();
lcd.print(“Sens2 is: “);
lcd.setCursor(0, 1);
printTemperature(Sens2);
Serial.print(”\n\r”);
delay(5000);

REMOVED IDENTICAL BLOCKS OF CODE FOR Sens3-Sens8 TO FIT FORUM

lcd.setCursor(0,0);
lcd.clear();
lcd.print(“Waiting for next”);
lcd.setCursor(0,1);
lcd.print(“sample…”);
delay(86000);
}

So does anyone have any ideas regarding getting the repetitive Serial.print and lcd.print statements into a loop? Maybe an example of how to use the pointers in a loop? (if that is the need?)

Thanks in Advance,
-fab

The answer to your problem is an array.

AHH! Yes! I will look into getting the functions using DeviceAddress to accept an array address!

(Hadn't thought of that!)

Thanks for the idea!