This is the code that I used for the two-sensor and 16x2 LCD system:
//based on: http://swingleydev.org/blog/tag/arduino/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 12
#define alarm 13
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress thermOne = { 0x10,0x7E,0x60,0x41,0x2,0x8,0x0,0x37 };
DeviceAddress thermTwo = { 0x10,0xE3,0x3C,0x41,0x2,0x8,0x0,0x78 };
LiquidCrystal lcd(2, 3, 7, 6, 5, 4);
void setup() {
Serial.begin(9600);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, LOW);
sensors.begin();
sensors.setResolution(thermOne, TEMPERATURE_PRECISION);
sensors.setResolution(thermTwo, TEMPERATURE_PRECISION);
lcd.begin(16, 2);
}
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
}
void loop() {
sensors.requestTemperatures();
printTemperature(thermOne);
Serial.print(",");
printTemperature(thermTwo);
Serial.print(",");
float thermOneC =(sensors.getTempC(thermOne));
float thermTwoC =(sensors.getTempC(thermTwo));
float diff = sqrt(pow((thermOneC - thermTwoC),2));
Serial.print(diff);
Serial.print(",");
if ((diff) > 4.0) {
Serial.println("high");
digitalWrite(alarm, HIGH);
} else if (diff > 2.0) {
Serial.println("med");
analogWrite(alarm, 127);
} else {
Serial.println("off");
digitalWrite(alarm, LOW);
}
///////////////////////////////
// LCD //////////////////////
//based on : http://www.instructables.com/id/ComputerLaptop-Temperature-Monitor-USB/step3/Wiring-up-the-connections/
lcd.setCursor(0, 1);
lcd.print(thermOneC,1);// the ",1" limits the o/p to 1 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(10,1);
lcd.print(thermTwoC,1);
lcd.print("C");
lcd.setCursor(0,0);
lcd.print("Diff=");
lcd.setCursor(6,0);
lcd.print(diff,1);
lcd.setCursor(11,0);
lcd.print("C");
////////////////////////////////
delay(2500);
}
This is the code that I used for the four-sensor and 20x4 LCD system:
//based on: http://swingleydev.org/blog/tag/arduino/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 8 // one wire data is on pin 8
#define TEMPERATURE_PRECISION 12
#define alarm 13 //alarm on pin 13 - ie the internal LED also acts as an indicator
#define maxdiff 4 //the maximum difference allowed between right & left portals on any axle before alarm is triggered. Change this value as required.
#define maxtemp 22//the maximum temperature allowed on any portal before alarm is triggered. Change this value as required
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress thermOne = { 0x10,0x7E,0x60,0x41,0x2,0x8,0x0,0x37 };//sets up the individual sensor address - need to identify what each sensors' address is before anything
DeviceAddress thermTwo = { 0x10,0xE3,0x3C,0x41,0x2,0x8,0x0,0x78 };
DeviceAddress thermThree = { 0x28,0x63,0x67,0x71,0x4,0x0,0x0,0x7D };
DeviceAddress thermFour = { 0x28,0x27,0x75,0x71,0x4,0x0,0x0,0x2B };
LiquidCrystal lcd(2, 3, 7, 6, 5, 4);//pins for LCD
void setup() {
Serial.begin(9600);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, LOW);
sensors.begin();
sensors.setResolution(thermOne, TEMPERATURE_PRECISION);
sensors.setResolution(thermTwo, TEMPERATURE_PRECISION);
sensors.setResolution(thermThree, TEMPERATURE_PRECISION);
sensors.setResolution(thermFour, TEMPERATURE_PRECISION);
lcd.begin(20, 4);
}
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
}
void loop() {
sensors.requestTemperatures();
printTemperature(thermOne);
Serial.print(",");
printTemperature(thermTwo);
Serial.print(",");
printTemperature(thermThree);
Serial.print(",");
printTemperature(thermFour);
Serial.print(",");
float thermOneC =(sensors.getTempC(thermOne));
float thermTwoC =(sensors.getTempC(thermTwo));
float thermThreeC =(sensors.getTempC(thermThree));
float thermFourC =(sensors.getTempC(thermFour));
float difffront = sqrt(pow((thermOneC - thermTwoC),2));
float diffrear = sqrt(pow((thermThreeC - thermFourC),2));
float diff = max(difffront, diffrear);
float maxfront = max(thermOneC, thermTwoC);
float maxrear = max(thermThreeC, thermFourC);
float maxall = max(maxfront, maxrear);
Serial.print(difffront);
Serial.print(",");
Serial.print(diffrear);
Serial.print(",");
if ((diff) > maxdiff) {//switches alarm on or off dependant upon diff in front OR rear temps - to modify the alarm temp difference change the value of maxdiff as defined above
Serial.println("diff high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("diff off");
digitalWrite(alarm, LOW);
}
if ((maxall) > maxtemp) {//switches alarm on or off dependant upon max temp of ANY sensor - to modify the alarm temp difference change the value of maxtemp as defined above
Serial.println("max temp high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("max temp off");
digitalWrite(alarm, LOW);
}
///////////////////////////////
// LCD //////////////////////
//based on : http://www.instructables.com/id/ComputerLaptop-Temperature-Monitor-USB/step3/Wiring-up-the-connections/
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(thermOneC,0);// the ",0" limits the o/p to 0 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(thermTwoC,0);
lcd.print("C");
lcd.setCursor(3,1);
lcd.print("F");
lcd.setCursor(5,1);
lcd.print(" ");
lcd.setCursor(5,1);
lcd.print(difffront,0);
lcd.setCursor(7,1);
lcd.print("C");
lcd.setCursor(3,2);
lcd.print("R");
lcd.setCursor(5,2);
lcd.print(" ");
lcd.setCursor(5,2);
lcd.print(diffrear,0);
lcd.setCursor(7,2);
lcd.print("C");
lcd.setCursor(0, 3);
lcd.print(thermThreeC,0);// the ",1" limits the o/p to 1 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(thermFourC,0);
lcd.print("C");
lcd.setCursor(16,0);
lcd.print("Cab");
lcd.setCursor(16,1);
lcd.print(" ");
lcd.setCursor(16,1);
lcd.print(thermThreeC,0);
lcd.print("C");
lcd.setCursor(16,2);
lcd.print("Out");
lcd.setCursor(16,3);
lcd.print(" ");
lcd.setCursor(16,3);
lcd.print(thermFourC,0);
lcd.print("C");
if ((diff) > maxdiff){
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
if ((maxall) > maxtemp){
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
////////////////////////////////
delay(2000);//defines refresh speed
}
And this is the code that I used for the six-sensor and 20x4 LCD system with all alarms now working:
//based on: http://swingleydev.org/blog/tag/arduino/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 8 // one wire data is on pin 8
#define TEMPERATURE_PRECISION 12
#define alarm 13 //alarm on pin 13 - ie the internal LED also acts as an indicator
#define maxdiff 4 //the maximum difference allowed between right & left portals on any axle before alarm is triggered. Change this value as required.
#define maxtemp 22//the maximum temperature allowed on any portal before alarm is triggered. Change this value as required
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress thermOne = { 0x10,0x7E,0x60,0x41,0x2,0x8,0x0,0x37 };//sets up the individual sensor address - need to identify what each sensors' address is before anything
DeviceAddress thermTwo = { 0x10,0xE3,0x3C,0x41,0x2,0x8,0x0,0x78 };
DeviceAddress thermThree = { 0x28,0x63,0x67,0x71,0x4,0x0,0x0,0x7D };
DeviceAddress thermFour = { 0x28,0x27,0x75,0x71,0x4,0x0,0x0,0x2B };
DeviceAddress thermFive = { 0x28,0xE9,0x14,0x2,0x4,0x0,0x0,0x5B };//air temps
DeviceAddress thermSix = { 0x28,0xC7,0x14,0xD,0x4,0x0,0x0,0x64 };//air temps
LiquidCrystal lcd(2, 3, 7, 6, 5, 4);//pins for LCD
void setup() {
Serial.begin(9600);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, LOW);
sensors.begin();
sensors.setResolution(thermOne, TEMPERATURE_PRECISION);
sensors.setResolution(thermTwo, TEMPERATURE_PRECISION);
sensors.setResolution(thermThree, TEMPERATURE_PRECISION);
sensors.setResolution(thermFour, TEMPERATURE_PRECISION);
sensors.setResolution(thermFive, TEMPERATURE_PRECISION);
sensors.setResolution(thermSix, TEMPERATURE_PRECISION);
lcd.begin(20, 4);
}
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
}
void loop() {
sensors.requestTemperatures();
printTemperature(thermOne);
Serial.print(",");
printTemperature(thermTwo);
Serial.print(",");
printTemperature(thermThree);
Serial.print(",");
printTemperature(thermFour);
Serial.print(",");
printTemperature(thermFive);
Serial.print(",");
printTemperature(thermSix);
Serial.print(",");
float thermOneC =(sensors.getTempC(thermOne));
float thermTwoC =(sensors.getTempC(thermTwo));
float thermThreeC =(sensors.getTempC(thermThree));
float thermFourC =(sensors.getTempC(thermFour));
float thermFiveC =(sensors.getTempC(thermFive));
float thermSixC =(sensors.getTempC(thermSix));
float difffront = sqrt(pow((thermOneC - thermTwoC),2));
float diffrear = sqrt(pow((thermThreeC - thermFourC),2));
float diff = max(difffront, diffrear);
float maxfront = max(thermOneC, thermTwoC);
float maxrear = max(thermThreeC, thermFourC);
float maxall = max(maxfront, maxrear);
Serial.print(difffront);
Serial.print(",");
Serial.print(diffrear);
Serial.print(",");
if ((diff) > maxdiff) {//switches alarm on or off dependant upon diff in front OR rear temps - to modify the alarm temp difference change the value of maxdiff as defined above
Serial.println("diff high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("diff off");
digitalWrite(alarm, LOW);
}
if ((maxall) > maxtemp) {//switches alarm on or off dependant upon max temp of ANY sensor - to modify the alarm temp difference change the value of maxtemp as defined above
Serial.println("max temp high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("max temp off");
digitalWrite(alarm, LOW);
}
///////////////////////////////
// LCD //////////////////////
//based on : http://www.instructables.com/id/ComputerLaptop-Temperature-Monitor-USB/step3/Wiring-up-the-connections/
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(thermOneC,0);// the ",0" limits the o/p to 0 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(thermTwoC,0);
lcd.print("C");
lcd.setCursor(3,1);
lcd.print("F");
lcd.setCursor(5,1);
lcd.print(" ");
lcd.setCursor(5,1);
lcd.print(difffront,0);
lcd.setCursor(7,1);
lcd.print("C");
lcd.setCursor(3,2);
lcd.print("R");
lcd.setCursor(5,2);
lcd.print(" ");
lcd.setCursor(5,2);
lcd.print(diffrear,0);
lcd.setCursor(7,2);
lcd.print("C");
lcd.setCursor(0, 3);
lcd.print(thermThreeC,0);// the ",1" limits the o/p to 1 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(thermFourC,0);
lcd.print("C");
lcd.setCursor(16,0);
lcd.print("Cab");
lcd.setCursor(16,1);
lcd.print(" ");
lcd.setCursor(16,1);
lcd.print(thermFiveC,0);
lcd.print("C");
lcd.setCursor(16,2);
lcd.print("Out");
lcd.setCursor(16,3);
lcd.print(" ");
lcd.setCursor(16,3);
lcd.print(thermSixC,0);
lcd.print("C");
if ((diff) > maxdiff){//blinks display if temp difference limit is exceeded
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
if ((maxall) > maxtemp){//blinks display if max temp limit is exceeded
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
////////////////////////////////
delay(2000);//defines refresh speed
}
The following code (or something similar) needs to be used to determine the "addresses" that are pre-programmed into each sensor (it's how they can be used on a "1-wire system"). Basically when you get your sensors and get everything wired up, you will need to find out what "address" each of your sensors has been programmed with - for this you will need to load this small programme onto the Arduino - once it is loaded and running, you will need to open the "Serial Monitor" on the PC whilst still connected to the Arduino - it's a button on the top right of the Arduino screen with a small "magnifying glass". This will open another window and you should start to see a series of numbers scrolling down the page (one set of numbers for each sensor that you've connected) - you can stop/start the scrolling using the "autoscoll" feature so you can read the numbers easier - you need to replace all of the "Ox__" numbers in my code with these numbers in the format "OxAA, 0xBB,....)" where AA, BB are the individual numbers shown on the serial monitor for each sensor.
Once you've done this, load my code onto the Arduino and you should be good to go:
#include <OneWire.h>
/* DS18S20 Temperature chip i/o */
OneWire ds(8); // on pin 10
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
Serial.print("No more addresses.\n");
ds.reset_search();
delay(250);
return;
}
Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr[i], HEX);
Serial.print(" ");
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
if ( addr[0] != 0x10) {
Serial.print("Device is not a DS18S20 family device.\n");
return;
}
// The DallasTemperature library can do all this work for you!
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print("P=");
Serial.print(present,HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print( OneWire::crc8( data, 8), HEX);
Serial.println();
}
//based on: http://swingleydev.org/blog/tag/arduino/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 12
#define alarm 13
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress thermOne = { 0x10,0x7E,0x60,0x41,0x2,0x8,0x0,0x37 };
DeviceAddress thermTwo = { 0x10,0xE3,0x3C,0x41,0x2,0x8,0x0,0x78 };
LiquidCrystal lcd(2, 3, 7, 6, 5, 4);
void setup() {
Serial.begin(9600);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, LOW);
sensors.begin();
sensors.setResolution(thermOne, TEMPERATURE_PRECISION);
sensors.setResolution(thermTwo, TEMPERATURE_PRECISION);
lcd.begin(16, 2);
}
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
}
void loop() {
sensors.requestTemperatures();
printTemperature(thermOne);
Serial.print(",");
printTemperature(thermTwo);
Serial.print(",");
float thermOneC =(sensors.getTempC(thermOne));
float thermTwoC =(sensors.getTempC(thermTwo));
float diff = sqrt(pow((thermOneC - thermTwoC),2));
Serial.print(diff);
Serial.print(",");
if ((diff) > 4.0) {
Serial.println("high");
digitalWrite(alarm, HIGH);
} else if (diff > 2.0) {
Serial.println("med");
analogWrite(alarm, 127);
} else {
Serial.println("off");
digitalWrite(alarm, LOW);
}
///////////////////////////////
// LCD //////////////////////
//based on : http://www.instructables.com/id/ComputerLaptop-Temperature-Monitor-USB/step3/Wiring-up-the-connections/
lcd.setCursor(0, 1);
lcd.print(thermOneC,1);// the ",1" limits the o/p to 1 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(10,1);
lcd.print(thermTwoC,1);
lcd.print("C");
lcd.setCursor(0,0);
lcd.print("Diff=");
lcd.setCursor(6,0);
lcd.print(diff,1);
lcd.setCursor(11,0);
lcd.print("C");
////////////////////////////////
delay(2500);
}
This is the code that I used for the four-sensor and 20x4 LCD system:
//based on: http://swingleydev.org/blog/tag/arduino/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 8 // one wire data is on pin 8
#define TEMPERATURE_PRECISION 12
#define alarm 13 //alarm on pin 13 - ie the internal LED also acts as an indicator
#define maxdiff 4 //the maximum difference allowed between right & left portals on any axle before alarm is triggered. Change this value as required.
#define maxtemp 22//the maximum temperature allowed on any portal before alarm is triggered. Change this value as required
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress thermOne = { 0x10,0x7E,0x60,0x41,0x2,0x8,0x0,0x37 };//sets up the individual sensor address - need to identify what each sensors' address is before anything
DeviceAddress thermTwo = { 0x10,0xE3,0x3C,0x41,0x2,0x8,0x0,0x78 };
DeviceAddress thermThree = { 0x28,0x63,0x67,0x71,0x4,0x0,0x0,0x7D };
DeviceAddress thermFour = { 0x28,0x27,0x75,0x71,0x4,0x0,0x0,0x2B };
LiquidCrystal lcd(2, 3, 7, 6, 5, 4);//pins for LCD
void setup() {
Serial.begin(9600);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, LOW);
sensors.begin();
sensors.setResolution(thermOne, TEMPERATURE_PRECISION);
sensors.setResolution(thermTwo, TEMPERATURE_PRECISION);
sensors.setResolution(thermThree, TEMPERATURE_PRECISION);
sensors.setResolution(thermFour, TEMPERATURE_PRECISION);
lcd.begin(20, 4);
}
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
}
void loop() {
sensors.requestTemperatures();
printTemperature(thermOne);
Serial.print(",");
printTemperature(thermTwo);
Serial.print(",");
printTemperature(thermThree);
Serial.print(",");
printTemperature(thermFour);
Serial.print(",");
float thermOneC =(sensors.getTempC(thermOne));
float thermTwoC =(sensors.getTempC(thermTwo));
float thermThreeC =(sensors.getTempC(thermThree));
float thermFourC =(sensors.getTempC(thermFour));
float difffront = sqrt(pow((thermOneC - thermTwoC),2));
float diffrear = sqrt(pow((thermThreeC - thermFourC),2));
float diff = max(difffront, diffrear);
float maxfront = max(thermOneC, thermTwoC);
float maxrear = max(thermThreeC, thermFourC);
float maxall = max(maxfront, maxrear);
Serial.print(difffront);
Serial.print(",");
Serial.print(diffrear);
Serial.print(",");
if ((diff) > maxdiff) {//switches alarm on or off dependant upon diff in front OR rear temps - to modify the alarm temp difference change the value of maxdiff as defined above
Serial.println("diff high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("diff off");
digitalWrite(alarm, LOW);
}
if ((maxall) > maxtemp) {//switches alarm on or off dependant upon max temp of ANY sensor - to modify the alarm temp difference change the value of maxtemp as defined above
Serial.println("max temp high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("max temp off");
digitalWrite(alarm, LOW);
}
///////////////////////////////
// LCD //////////////////////
//based on : http://www.instructables.com/id/ComputerLaptop-Temperature-Monitor-USB/step3/Wiring-up-the-connections/
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(thermOneC,0);// the ",0" limits the o/p to 0 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(thermTwoC,0);
lcd.print("C");
lcd.setCursor(3,1);
lcd.print("F");
lcd.setCursor(5,1);
lcd.print(" ");
lcd.setCursor(5,1);
lcd.print(difffront,0);
lcd.setCursor(7,1);
lcd.print("C");
lcd.setCursor(3,2);
lcd.print("R");
lcd.setCursor(5,2);
lcd.print(" ");
lcd.setCursor(5,2);
lcd.print(diffrear,0);
lcd.setCursor(7,2);
lcd.print("C");
lcd.setCursor(0, 3);
lcd.print(thermThreeC,0);// the ",1" limits the o/p to 1 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(thermFourC,0);
lcd.print("C");
lcd.setCursor(16,0);
lcd.print("Cab");
lcd.setCursor(16,1);
lcd.print(" ");
lcd.setCursor(16,1);
lcd.print(thermThreeC,0);
lcd.print("C");
lcd.setCursor(16,2);
lcd.print("Out");
lcd.setCursor(16,3);
lcd.print(" ");
lcd.setCursor(16,3);
lcd.print(thermFourC,0);
lcd.print("C");
if ((diff) > maxdiff){
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
if ((maxall) > maxtemp){
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
////////////////////////////////
delay(2000);//defines refresh speed
}
And this is the code that I used for the six-sensor and 20x4 LCD system with all alarms now working:
//based on: http://swingleydev.org/blog/tag/arduino/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 8 // one wire data is on pin 8
#define TEMPERATURE_PRECISION 12
#define alarm 13 //alarm on pin 13 - ie the internal LED also acts as an indicator
#define maxdiff 4 //the maximum difference allowed between right & left portals on any axle before alarm is triggered. Change this value as required.
#define maxtemp 22//the maximum temperature allowed on any portal before alarm is triggered. Change this value as required
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress thermOne = { 0x10,0x7E,0x60,0x41,0x2,0x8,0x0,0x37 };//sets up the individual sensor address - need to identify what each sensors' address is before anything
DeviceAddress thermTwo = { 0x10,0xE3,0x3C,0x41,0x2,0x8,0x0,0x78 };
DeviceAddress thermThree = { 0x28,0x63,0x67,0x71,0x4,0x0,0x0,0x7D };
DeviceAddress thermFour = { 0x28,0x27,0x75,0x71,0x4,0x0,0x0,0x2B };
DeviceAddress thermFive = { 0x28,0xE9,0x14,0x2,0x4,0x0,0x0,0x5B };//air temps
DeviceAddress thermSix = { 0x28,0xC7,0x14,0xD,0x4,0x0,0x0,0x64 };//air temps
LiquidCrystal lcd(2, 3, 7, 6, 5, 4);//pins for LCD
void setup() {
Serial.begin(9600);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, LOW);
sensors.begin();
sensors.setResolution(thermOne, TEMPERATURE_PRECISION);
sensors.setResolution(thermTwo, TEMPERATURE_PRECISION);
sensors.setResolution(thermThree, TEMPERATURE_PRECISION);
sensors.setResolution(thermFour, TEMPERATURE_PRECISION);
sensors.setResolution(thermFive, TEMPERATURE_PRECISION);
sensors.setResolution(thermSix, TEMPERATURE_PRECISION);
lcd.begin(20, 4);
}
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
}
void loop() {
sensors.requestTemperatures();
printTemperature(thermOne);
Serial.print(",");
printTemperature(thermTwo);
Serial.print(",");
printTemperature(thermThree);
Serial.print(",");
printTemperature(thermFour);
Serial.print(",");
printTemperature(thermFive);
Serial.print(",");
printTemperature(thermSix);
Serial.print(",");
float thermOneC =(sensors.getTempC(thermOne));
float thermTwoC =(sensors.getTempC(thermTwo));
float thermThreeC =(sensors.getTempC(thermThree));
float thermFourC =(sensors.getTempC(thermFour));
float thermFiveC =(sensors.getTempC(thermFive));
float thermSixC =(sensors.getTempC(thermSix));
float difffront = sqrt(pow((thermOneC - thermTwoC),2));
float diffrear = sqrt(pow((thermThreeC - thermFourC),2));
float diff = max(difffront, diffrear);
float maxfront = max(thermOneC, thermTwoC);
float maxrear = max(thermThreeC, thermFourC);
float maxall = max(maxfront, maxrear);
Serial.print(difffront);
Serial.print(",");
Serial.print(diffrear);
Serial.print(",");
if ((diff) > maxdiff) {//switches alarm on or off dependant upon diff in front OR rear temps - to modify the alarm temp difference change the value of maxdiff as defined above
Serial.println("diff high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("diff off");
digitalWrite(alarm, LOW);
}
if ((maxall) > maxtemp) {//switches alarm on or off dependant upon max temp of ANY sensor - to modify the alarm temp difference change the value of maxtemp as defined above
Serial.println("max temp high");
digitalWrite(alarm, HIGH);
} else {
Serial.println("max temp off");
digitalWrite(alarm, LOW);
}
///////////////////////////////
// LCD //////////////////////
//based on : http://www.instructables.com/id/ComputerLaptop-Temperature-Monitor-USB/step3/Wiring-up-the-connections/
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(thermOneC,0);// the ",0" limits the o/p to 0 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(thermTwoC,0);
lcd.print("C");
lcd.setCursor(3,1);
lcd.print("F");
lcd.setCursor(5,1);
lcd.print(" ");
lcd.setCursor(5,1);
lcd.print(difffront,0);
lcd.setCursor(7,1);
lcd.print("C");
lcd.setCursor(3,2);
lcd.print("R");
lcd.setCursor(5,2);
lcd.print(" ");
lcd.setCursor(5,2);
lcd.print(diffrear,0);
lcd.setCursor(7,2);
lcd.print("C");
lcd.setCursor(0, 3);
lcd.print(thermThreeC,0);// the ",1" limits the o/p to 1 decimal place
lcd.print("C");
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(" ");
lcd.setCursor(8,3);
lcd.print(thermFourC,0);
lcd.print("C");
lcd.setCursor(16,0);
lcd.print("Cab");
lcd.setCursor(16,1);
lcd.print(" ");
lcd.setCursor(16,1);
lcd.print(thermFiveC,0);
lcd.print("C");
lcd.setCursor(16,2);
lcd.print("Out");
lcd.setCursor(16,3);
lcd.print(" ");
lcd.setCursor(16,3);
lcd.print(thermSixC,0);
lcd.print("C");
if ((diff) > maxdiff){//blinks display if temp difference limit is exceeded
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
if ((maxall) > maxtemp){//blinks display if max temp limit is exceeded
// Turn off the display:
lcd.noDisplay();
delay(300);
// Turn on the display:
lcd.display();
delay(500);
} else {lcd.display();
}
////////////////////////////////
delay(2000);//defines refresh speed
}
The following code (or something similar) needs to be used to determine the "addresses" that are pre-programmed into each sensor (it's how they can be used on a "1-wire system"). Basically when you get your sensors and get everything wired up, you will need to find out what "address" each of your sensors has been programmed with - for this you will need to load this small programme onto the Arduino - once it is loaded and running, you will need to open the "Serial Monitor" on the PC whilst still connected to the Arduino - it's a button on the top right of the Arduino screen with a small "magnifying glass". This will open another window and you should start to see a series of numbers scrolling down the page (one set of numbers for each sensor that you've connected) - you can stop/start the scrolling using the "autoscoll" feature so you can read the numbers easier - you need to replace all of the "Ox__" numbers in my code with these numbers in the format "OxAA, 0xBB,....)" where AA, BB are the individual numbers shown on the serial monitor for each sensor.
Once you've done this, load my code onto the Arduino and you should be good to go:
#include <OneWire.h>
/* DS18S20 Temperature chip i/o */
OneWire ds(8); // on pin 10
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
Serial.print("No more addresses.\n");
ds.reset_search();
delay(250);
return;
}
Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr[i], HEX);
Serial.print(" ");
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
if ( addr[0] != 0x10) {
Serial.print("Device is not a DS18S20 family device.\n");
return;
}
// The DallasTemperature library can do all this work for you!
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print("P=");
Serial.print(present,HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print( OneWire::crc8( data, 8), HEX);
Serial.println();
}