Onewire Dallastemperature low and high alarm separate

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.

void loop(void)
{ 
  char tempH; 
  sensors.requestTemperatures();
  {
    Serial.println("Oh noes!  There is at least one alarm on the bus.");
    digitalWrite(3, HIGH);
  }
   sensors.requestTemperatures();

???
Why are you requesting temperatures twice each time through loop()?
Why are you reporting an alarm each time through loop()???

Hey Johnwasser

Thanks for a very fast response
Cut and paste for most of it, i am learning on the fly, and it is nice that the code obviously can be trimmed down, thank you.

Do you have som code suggestions that might fulfill my quest.

Kind regards
Anders

Johnwasser

Did you mean the code that i put in //comment. If not i can not see where i do it twice.

And i get that i have to "insert" my code - sorry for that.

I'd love to help but I can't find any good documentation for the DallasTemperature library. Apparently you are supposed to buy the book Beginning Arduino to learn how to use the library.

johnwasser:
I'd love to help but I can't find any good documentation for the DallasTemperature library. Apparently you are supposed to buy the book Beginning Arduino to learn how to use the library.

Johnwasser
Once again-, thanks for a fast responce.
I have that book, chapter 9 is about libraries, but nothing on the DS18B20. I will read, see what i can learn about labraries.
I have attached what i have on the 18B20, but i guess that you might need specifik information on the library.

???
Why are you requesting temperatures twice each time through loop()?
Why are you reporting an alarm each time through loop()???

Could you point that out for me, if it was not the //comment part
Thanks and regards
Anders

sf-DS18B20.pdf (256 KB)

DallasTemperature.cpp (19.9 KB)

DallasTemperature.h (6.22 KB)

change.txt (3.72 KB)

It looks like there is only one alarm flag per device. To determine if the alarm is a HIGH alarm or a LOW alarm you have to read the current temperature and compare it to the two alarm temperatures. Something like this:

if (sensors.hasAlarm(deviceAddress)) {
    float tempC = sensors.getTempC(deviceAddress);
    if (tempC != DEVICE_DISCONNECTED) {
        if (tempC >= sensors.getHighAlarmTemp(deviceAddress)) {
            // HIGH alarm
       }   
 
        if (tempC <= sensors.getLowAlarmTemp(deviceAddress)) {
            // LOW alarm
        }
    }
}

johnwasser:
It looks like there is only one alarm flag per device. To determine if the alarm is a HIGH alarm or a LOW alarm you have to read the current temperature and compare it to the two alarm temperatures. Something like this:

if (sensors.hasAlarm(deviceAddress)) {

float tempC = sensors.getTempC(deviceAddress);
    if (tempC != DEVICE_DISCONNECTED) {
        if (tempC >= sensors.getHighAlarmTemp(deviceAddress)) {
           // HIGH alarm
      }  
        if (tempC <= sensors.getLowAlarmTemp(deviceAddress)) {
           // LOW alarm
       }
    }
}

Johnwasser
Yes i thought that it might be something like that - the one flag pr. device. So i guess i will have to work around that, with some help :slight_smile:
Thank you for the code. I will now go to bed, bringing with me "the book" and your code. I will not rest until my brain is wrapped around this, and I am so looking forward to all the eurekas coming up (hopefully).

I will try and formulate a question, might get the terminology a bit off. If i want each sensor to perform different tasks (one sensor controlling one set of cooling and heating devices, another sensor controlling a different set), will I have to "spell it out" using temp1, temp2 etc. instead of deviceAddress? If my question is totally nonsense - just ignore :smiley:

regards and goodnight

Johnwasser

You have saved me a lot of time and giving my understanding a serious push in the right direction.
It wasn't so hard after all. No hard brain curling but a few eurekas. I think it was the float part i first saw as difficult :slight_smile:
I found the "requesting temperatures twice" and "reporting" issue you mentioned. Me fooling around with code (trail and... you know)

Well here is how it looks for now:

if (sensors.hasAlarm(Temp1)) {
    float tempC = sensors.getTempC(Temp1);
    if (tempC != DEVICE_DISCONNECTED) {
        if (tempC >= sensors.getHighAlarmTemp(Temp1)) {
          digitalWrite(3, HIGH);  
          // HIGH alarm
       }   
        if (tempC <= sensors.getLowAlarmTemp(Temp1)) {
          digitalWrite(4, HIGH);
        // LOW alarm
        }
    }
}

This i have some trouble understanding if (tempC != DEVICE_DISCONNECTED) {
If tempC not equal to what?

I will do some more modifying and trimming and if it is okay with you, i will run the code by you, when almost done.

And for now - thank you very, very much Johnwasser

Kind regards
Anders (Denmark)

This i have some trouble understanding

if (tempC != DEVICE_DISCONNECTED) {

If tempC not equal to what?

Look in the DallasTemperature.h file you pasted and you will see on line 55/56 this

// Error Codes
#define DEVICE_DISCONNECTED -127

It is a value returned if the device does not exits that you can check for so you don't start doing stuff based on duff temperature data.

Riva:

This i have some trouble understanding

if (tempC != DEVICE_DISCONNECTED) {

If tempC not equal to what?

Look in the DallasTemperature.h file you pasted and you will see on line 55/56 this

// Error Codes

#define DEVICE_DISCONNECTED -127



It is a value returned if the device does not exits that you can check for so you don't start doing stuff based on duff temperature data.

Aha, so the math add up once again. Thank you Riva, my brain would not accept something being computed, that was not defined in some way - but it was. I will sleep well tonight XD.

Kind regards
Anders