Since i only can find very, very old threads on onewire Dallastemperature sensor, and non of them touch my problem, i will start a new thread
I am setting up seven onewires 18B20 and i need my code to handle low alarm and high alarm for each sensor different. So that for one sensor high alarm makes one port high/low and low alarm makes a different port high/low.
One sensor high alarm start cooling fan - same sensor, low alarm start heater.
The code i have used, and modified to seven sensors, just make one alarm for both high and low.
I guess its in the libary, requestTemperatures or hasAlarm, but going there is over my head.
I am pretty new to this, so please go easy on me.
Here is my code:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// 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);
// arrays to hold device addresses
DeviceAddress Temp1, Temp2, Temp3, Temp4, Temp5, Temp6, Temp7;
// function that will be called when an alarm condition exists during DallasTemperatures::processAlarms();
void newAlarmHandler(uint8_t* deviceAddress)
{
Serial.println("Alarm Handler Start");
printAlarmInfo(deviceAddress);
printTemp(deviceAddress);
Serial.println();
Serial.println("Alarm Handler Finish");
}
void printCurrentTemp(DeviceAddress deviceAddress)
{
printAddress(deviceAddress);
printTemp(deviceAddress);
Serial.println();
}
void printAddress(DeviceAddress deviceAddress)
{
Serial.print("Address: ");
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
Serial.print(" ");
}
void printTemp(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC != DEVICE_DISCONNECTED)
{
Serial.print("Current Temp C: ");
Serial.print(tempC);
}
else Serial.print("DEVICE DISCONNECTED");
Serial.print(" ");
}
void printAlarmInfo(DeviceAddress deviceAddress)
{
char temp;
printAddress(deviceAddress);
temp = sensors.getHighAlarmTemp(deviceAddress);
Serial.print("High Alarm: ");
Serial.print(temp, DEC);
Serial.print("C");
Serial.print(" Low Alarm: ");
temp = sensors.getLowAlarmTemp(deviceAddress);
Serial.print(temp, DEC);
Serial.print("C");
Serial.print(" ");
}
void setup(void)
{
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// search for devices on the bus and assign based on an index
if (!sensors.getAddress(Temp1, 0)) Serial.println("Unable to find address for Device 0");
if (!sensors.getAddress(Temp2, 1)) Serial.println("Unable to find address for Device 1");
if (!sensors.getAddress(Temp3, 2)) Serial.println("Unable to find address for Device 2");
if (!sensors.getAddress(Temp4, 3)) Serial.println("Unable to find address for Device 3");
if (!sensors.getAddress(Temp5, 4)) Serial.println("Unable to find address for Device 4");
if (!sensors.getAddress(Temp6, 5)) Serial.println("Unable to find address for Device 5");
if (!sensors.getAddress(Temp7, 6)) Serial.println("Unable to find address for Device 6");
Serial.print("Device Temp1 ");
printAlarmInfo(Temp1);
Serial.println();
Serial.print("Device Temp2 ");
printAlarmInfo(Temp2);
Serial.println();
Serial.print("Device Temp3 ");
printAlarmInfo(Temp3);
Serial.println();
Serial.print("Device Temp4 ");
printAlarmInfo(Temp4);
Serial.println();
Serial.print("Device Temp5 ");
printAlarmInfo(Temp5);
Serial.println();
Serial.print("Device Temp6 ");
printAlarmInfo(Temp6);
Serial.println();
Serial.print("Device Temp7 ");
printAlarmInfo(Temp7);
Serial.println();
// set alarm ranges
Serial.println("Setting alarm temps...");
sensors.setHighAlarmTemp(Temp1, 26);
sensors.setLowAlarmTemp(Temp1, 20);
sensors.setHighAlarmTemp(Temp2, 30);
sensors.setLowAlarmTemp(Temp2, 15);
sensors.setHighAlarmTemp(Temp3, 26);
sensors.setLowAlarmTemp(Temp3, 15);
sensors.setHighAlarmTemp(Temp4, 30);
sensors.setLowAlarmTemp(Temp4, 15);
sensors.setHighAlarmTemp(Temp5, 30);
sensors.setLowAlarmTemp(Temp5, 15);
sensors.setHighAlarmTemp(Temp6, 30);
sensors.setLowAlarmTemp(Temp6, 15);
sensors.setHighAlarmTemp(Temp7, 30);
sensors.setLowAlarmTemp(Temp7, 15);
Serial.print("New Temp1 ");
printAlarmInfo(Temp1);
Serial.println();
Serial.print("New Temp2 ");
printAlarmInfo(Temp2);
Serial.println();
Serial.print("New Temp3 ");
printAlarmInfo(Temp3);
Serial.println();
Serial.print("New Temp4 ");
printAlarmInfo(Temp4);
Serial.println();
Serial.print("New Temp5 ");
printAlarmInfo(Temp5);
Serial.println();
Serial.print("New Temp6 ");
printAlarmInfo(Temp6);
Serial.println();
Serial.print("New Temp7 ");
printAlarmInfo(Temp7);
Serial.println();
// attach alarm handler
sensors.setAlarmHandler(&newAlarmHandler);
}
void loop(void)
{
//Anders kode
//char getHighAlarmTemp(uint8_t* Temp1);
//sensors.setHighAlarmTemp(Temp1, 26);
char tempH;
//tempH = sensors.getHighAlarmTemp(Temp1);
sensors.requestTemperatures();
//if (tempH == !sensors.requestTemperatures(Temp1);
{
Serial.println("Oh noes! There is at least one alarm on the bus.");
digitalWrite(3, HIGH);
}
//Anders kode slut
// ask the devices to measure the temperature
sensors.requestTemperatures();
// if an alarm condition exists as a result of the most recent
// requestTemperatures() request, it exists until the next time
// requestTemperatures() is called AND there isn't an alarm condition
// on the device
if (sensors.hasAlarm(Temp1))
{
Serial.println("Oh noes! There is at least one alarm on the bus.");
digitalWrite(3, HIGH);
}
if (sensors.hasAlarm(Temp3))
{
Serial.println("Oh noes! There is at least one alarm on the bus.");
digitalWrite(4, HIGH);
}
// call alarm handler function defined by sensors.setAlarmHandler
// for each device reporting an alarm
sensors.processAlarms();
if (!sensors.hasAlarm())
{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
// just print out the current temperature
printCurrentTemp(Temp1);
printCurrentTemp(Temp2);
printCurrentTemp(Temp3);
printCurrentTemp(Temp4);
printCurrentTemp(Temp5);
printCurrentTemp(Temp6);
printCurrentTemp(Temp7);
Serial.print("New reading"); Serial.println();
}
delay(5000);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.