#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
// Data wire is plugged into pin 6 on the Arduino
#define ONE_WIRE_BUS 6
// 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);
//variables
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
int TEMPERATURE_PRECISION = 12;
unsigned long time0 = millis();
//lcd setup
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13; // pin 13 will control the backlight
String strI;
float tempCelsius;
float tempFahrenheit;
String strC;
String strF;
void setup(void)
{
// start serial port for viewing temps on laptop
Serial.begin(9600);
Serial.println("-= COB Multiple Temperature Sensor Readings=-");
sensors.begin(); // Start up the library
// Grab a count of devices on the wire
int numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for (int i = 0; i < numberOfDevices; i++)
{
// Search the wire for address
if (sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
delay(100);
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
//LCD setup
pinMode(backLight, OUTPUT); //set pin 13 as output
analogWrite(backLight, 150); //controls the backlight intensity 0-254
lcd.begin(16, 2); // columns, rows. size of display
lcd.clear(); // clear the screen
lcd.setCursor(0, 0); // set cursor to column 0, row 0 (first row)
lcd.print("Hello. Starting Up... "); // input your text here
lcd.setCursor(0, 1); // move cursor down one
delay(3000);
}
void loop(void)
{
Serial.print("\nTime: ");
print_time(millis() - time0);
numberOfDevices = sensors.getDeviceCount();
String strNumberOfDevices = String(numberOfDevices);
// call sensors.requestTemperatures() to issue a global temperature request to *all* devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
for (int i = 0; i < numberOfDevices; i++) {
strI = String(i + 1);
tempCelsius = sensors.getTempCByIndex(i);
tempFahrenheit = Celcius2Fahrenheit(tempCelsius);
strC = String(tempCelsius);
strF = String (tempFahrenheit);
//print temps to computer
Serial.print("\nTemperature Device #" + strI + ": ");
Serial.print( strC + " C / " + strF + " F " );
if (strI == "2") {
lcd.clear();
lcd.print( strC + " C " + strF + " F " );
}
// }
}
delay(10000);
}
// 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);
}
}
void print_time(unsigned long t_milli)
{
char buffer[20];
int days, hours, mins, secs;
int fractime;
unsigned long inttime;
inttime = t_milli / 1000;
fractime = t_milli % 1000;
// inttime is the total number of number of seconds
// fractimeis the number of thousandths of a second
// number of days is total number of seconds divided by 24 divided by 3600
days = inttime / (24 * 3600);
inttime = inttime % (24 * 3600);
// Now, inttime is the remainder after subtracting the number of seconds
// in the number of days
hours = inttime / 3600;
inttime = inttime % 3600;
// Now, inttime is the remainder after subtracting the number of seconds
// in the number of days and hours
mins = inttime / 60;
inttime = inttime % 60;
// Now inttime is the number of seconds left after subtracting the number
// in the number of days, hours and minutes. In other words, it is the
// number of seconds.
secs = inttime;
// Don't bother to print days
//sprintf(buffer, "%02d:%02d:%02d.%03d", hours, mins, secs, fractime);
sprintf(buffer, "%02d:%02d:%02d", hours, mins, secs);
//lcd.print(buffer);
Serial.print(buffer);
}
int Celcius2Fahrenheit(float celsius)
{
return celsius * 115 / 64 + 32; // max celsius is about 280 Note: 115/64 = 1.796875 ~~ 0.3% error in theory in practice it will round downwards so it will be worse
}