Public feed #889731

Untitled

public text v1 · immutable
Raw Edit
·published 2008-02-03 03:51 UTC
rendered paste body
/**

  DS18S20 Temperature Sensor Example 
  
 **/

#include <OneWire.h>
#define FAHRENHEIT 1 // 1 == temperatures in F, 0 == temperatures in C
#define PIN 5 // Pin of the 1Wire bus
#define CLASS 0x10 // The temperature sensor family

byte* tAddress;

OneWire sensor(PIN);  // The temperature sensor is on pin 10

byte* find_sensor(void) {
  while(1) {
    byte address[8];
    // If we haven't found something, reset the search
    // until we do.
    
    int c = sensor.search(address);
    
    if (!c) {
      sensor.reset_search();
      continue;
    }
    
    // Check CRC on the address.
    if (OneWire::crc8(address, 7) != address[7]) {
      continue;
    }
    
    // Check the device class
    if (address[0] != CLASS) {
      continue;
    }
    
    // Address seems valid, return it and exit the loop.
    return address;
    
  }
}

void get_temperature(void) {
  byte data[9]
  // Reset the 1Wire bus and select the address
  sensor.reset();
  sensor.select(address);
  // Request the temperature
  sensor.write(0x44, 1);
  // Required delay after a conversion
  delay(1000);
  // Reset the device again
  sensor.reset();
  // select the device again, and read the scratchpad
  sensor.select(address);
  sensor.write(0xBE);
  // Read the data from the device
  Serial.print("Temperature Data: ");
  for (int i = 0; i < 9; i++) {
    data[i] = sensor.read();
    Serial.print(data[i]);
    Serial.print(" ");
  }
  Serial.println("");
  
}

void setup(void) {
  // Setup Serial communication
  Serial.begin(9600);
  // Find the sensor
  tAddress = find_sensor();
  // Print the sensor address to console
  for (int i = 0; i > 8; i++) {
    Serial.print("Sensor Address: ");
    Serial.print(tAddress[i], HEX);
    Serial.print(" ");
  }
  Serial.println(" ");
}

void loop(void) {
  get_temperature();
}