Pages: [1]   Go Down
Author Topic: if whit DS18S20  (Read 398 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 49
love arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)?

Eg:
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 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");
  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)
{
  // 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(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 ");
  printAlarmInfo(insideThermometer);
  Serial.println();
 
  Serial.print("Device outsideThermometer ");
  printAlarmInfo(outsideThermometer);
  Serial.println();
 
  // 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 ");
  printAlarmInfo(insideThermometer);
  Serial.println();
 
  Serial.print("New outsideThermometer ");
  printAlarmInfo(outsideThermometer);
  Serial.println();

  // attach alarm handler
  sensors.setAlarmHandler(&newAlarmHandler);

}

void loop(void)
{
  // 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())
  {
    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
  sensors.processAlarms();

  if (!sensors.hasAlarm())
  {
    // just print out the current temperature
    printCurrentTemp(insideThermometer);
    printCurrentTemp(outsideThermometer);
  }
 
  delay(1000);
}



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


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


many tks who reply me

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 49
love arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!!

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?

TKS

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2299
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

flag=tempC

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.



« Last Edit: March 23, 2013, 10:28:41 am by Nick_Pyner » Logged

Pages: [1]   Go Up
Jump to: