if whit DS18S20

Hi All

imagine I got an DALLAS DS18S20 an every time I connect Arduino, i need to perform an action if tmp rises2 or 3 levels, and Arduino make it some thing…

How can i grab the TEMPC in array and them compare whit initial (on start up)?


#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 insideThermometer, outsideThermometer;

// function that will be called when an alarm condition exists during DallasTemperatures::processAlarms();
void newAlarmHandler(uint8_t* deviceAddress)
  Serial.println("Alarm Handler Start"); 
  Serial.println("Alarm Handler Finish");

void printCurrentTemp(DeviceAddress deviceAddress)

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);
    Serial.print("Current Temp C: ");
  else Serial.print("DEVICE DISCONNECTED");
  Serial.print(" ");

void printAlarmInfo(DeviceAddress deviceAddress)
  char temp;
  temp = sensors.getHighAlarmTemp(deviceAddress);
  Serial.print("High Alarm: ");
  Serial.print(temp, DEC);
  Serial.print(" Low Alarm: ");
  temp = sensors.getLowAlarmTemp(deviceAddress);
  Serial.print(temp, DEC);
  Serial.print(" ");

void setup(void)
  // start serial port
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  // 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(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
  if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 

  Serial.print("Device insideThermometer ");
  Serial.print("Device outsideThermometer ");
  // set alarm ranges
  Serial.println("Setting alarm temps...");
  sensors.setHighAlarmTemp(insideThermometer, 26);
  sensors.setLowAlarmTemp(insideThermometer, 22);
  sensors.setHighAlarmTemp(outsideThermometer, 25);
  sensors.setLowAlarmTemp(outsideThermometer, 21);
  Serial.print("New insideThermometer ");
  Serial.print("New outsideThermometer ");

  // attach alarm handler


void loop(void)
  // ask the devices to measure the temperature
  // 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())
    Serial.println("Oh noes!  There is at least one alarm on the bus.");

  // call alarm handler function defined by sensors.setAlarmHandler
  // for each device reporting an alarm

  if (!sensors.hasAlarm())
    // just print out the current temperature

in this case, just need that the alarm was, plus 1 or 2 or 3 or 10… from initial environment temperature :slight_smile:

  sensors.setHighAlarmTemp(insideThermometer, +1);
  sensors.setLowAlarmTemp(insideThermometer, +2);
  sensors.setHighAlarmTemp(outsideThermometer, +10);
  sensors.setLowAlarmTemp(outsideThermometer, -10);

many tks who reply me

How can i grab the TEMPC in array

Scrap that crappy example. The printTemperature() function is doing more that its name implies that it should.

Rename the function so that the name reflects everything it goes (getAndPrintTemperature()). Then, it should be pretty obvious how to break the function into two functions that do one thing each (getTemperature() and printTemperature()).

Then, the answer to your question should be obvious.


I didn´t get the way to breack and chek if temperature had some increase from 1st value, this values ca change, so the need to get always the current ambient temp…

any help? What shoul i learn to understand?


I didn´t get the way to breack and chek if temperature had some increase from 1st value, this values ca change, so the need to get always the current ambient temp....

You should post the printTemperature() after adding comments to it. If you understand the code well enough to add the comments, then it should be easy to make the function not print the temperature. If you can do that, then it isn't difficult to add a return statement to return the temperature. That requires changing the return type of the function.

The "do this for me" section is called Gigs and Collaborations.

I did something similar for a temporary drop-below-original test with DS18B20. I think this simply involved copying a command in the loop to the end of the setup

tempC = sensors.getTempC (deviceAddress);

and adding


and the end of the loop was

if (tempC<flag)
serial.println(" Temp C < flag");

If you want multiple cases, I guess it is simply a matter of adding them - flag2 = TempC + 16.16 etc., and writing an if section as needed. No arrays needed.

I recognise that you are currently only using one DS18B20 but, FWIW, I submit you would be better off determining the device address in advance and using the simpler Hacktronics approach.