Tuesday, December 6, 2011

Arduino 1-wire code with basic alarm output

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

// Define digital input pin your one_wire sensors are connected to on the Arduino
#define ONE_WIRE_BUS 10
// define my delay time on next line
#define mydelay 7500 //7500 is 7.5 seconds

// 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 sensor1 = {
0x28, 0x20, 0x13, 0x1C, 0x03, 0x00, 0x00, 0x54 };
DeviceAddress sensor2 = {
0x28, 0xD4, 0xE5, 0x1B, 0x03, 0x00, 0x00, 0x46 };
DeviceAddress sensor3 = {
0x28, 0x92, 0x28, 0x1C, 0x03, 0x00, 0x00, 0xC9 };
DeviceAddress sensor4 = {
0x28, 0xAF, 0x08, 0x1C, 0x03, 0x00, 0x00, 0x96 };
DeviceAddress sensor5 = {
0x28, 0xE0, 0x9D, 0xBF, 0x03, 0x00, 0x00, 0x83 };
DeviceAddress sensor6 = {
0x28, 0x8D, 0x82, 0xBF, 0x03, 0x00, 0x00, 0x19 };
DeviceAddress sensor7 = {
0x28, 0x7D, 0xA9, 0xBF, 0x03, 0x00, 0x00, 0xE3 };
DeviceAddress sensor8 = {
0x28, 0x17, 0x84, 0xBF, 0x03, 0x00, 0x00, 0xFB };

void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(sensor1, 10);
sensors.setResolution(sensor2, 10); //djash 1st cord single sensor
sensors.setResolution(sensor3, 10);
sensors.setResolution(sensor4, 10);
sensors.setResolution(sensor5, 10);
sensors.setResolution(sensor6, 10); //djash cord 2 2nd sensor (end)
sensors.setResolution(sensor7, 10); //djash cord 2 ist sensor (middle)
sensors.setResolution(sensor8, 10);
pinMode(13, OUTPUT);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
// djash these next two lines print if the sensor can't be read.
Serial.print("\r\n");
Serial.print("Sensor missing or unreadable");
}
else {
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}

void loop(void)
{
delay(mydelay); //1000 is 1 second
// djash Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
delay (3500);
float s1 = sensors.getTempC(sensor1);
float s2 = sensors.getTempC(sensor2);
float s3 = sensors.getTempC(sensor3);
float s4 = sensors.getTempC(sensor4);
float s5 = sensors.getTempC(sensor5);
float s6 = sensors.getTempC(sensor6);
float s7 = sensors.getTempC(sensor7);
float s8 = sensors.getTempC(sensor8);

if (s1 > 0)
{
// djash Serial.print("Sensor 1 temp is: ");
printTemperature(sensor1);
Serial.print(",");
}


if (s2 > 0)
{
// djash Serial.print("Sensor 2 temp is: ");
printTemperature(sensor2);
Serial.print(",");
}



if (s3 > 0)
{
// djash Serial.print("Sensor 3 temp is: ");
printTemperature(sensor3);
Serial.print(",");
}


if (s4 > 0)
{
// djash Serial.print("Sensor 4 temp is: ");
printTemperature(sensor4);
Serial.print(",");
}


if (s5 > 0)
{
// djash Serial.print("Sensor 5 temp is: ");
printTemperature(sensor5);
Serial.print(",");
}


if (s6 > 0)
{
// djash Serial.print("Sensor 6 temp is: ");
printTemperature(sensor6);
Serial.print(",");
}


if (s7 > 0)
{
// djash Serial.print("Sensor 7 temp is: ");
printTemperature(sensor7);
Serial.print(",");
}


if (s8 > 0)
{
// djash Serial.print("Sensor 8 temp is: ");
printTemperature(sensor8);
Serial.print(",");
}

Serial.print("\r\n");

// next line writes pin 13 to high (on) if sensor 6 is > 80.00 Deg F.
// an example of an alarm of sorts turns on output 13 (on board led) but can be whatever you want.
// if you write it like this it will be in degrees C. digitalWrite(13, s6 > 80);
digitalWrite(13, (DallasTemperature::toFahrenheit(s6) > 80));

}

No comments:

Post a Comment