// Include library found here: // http://milesburton.com/index.php?title=Dallas_Temperature_Control_Library #include // DS18S20 Temperature chip i/o OneWire ds(8); // on pin 8 int ledPin = 13; // LED connected to digital pin 13 // Do configuration // The buffer window size determines how many single temperature data samples // are collected and held at the same time. The program takes the average of // all these samples. With the standard values for windowSize (6) and // fetchInterval (10000) there are six samples collected in a minute. A read- // out thus gives the average over a minute. // // Standard: 6 int const windowSize = 6; // Time between temperature fetches // // Standard: 10 seconds unsigned long fetchInterval = 10000; // Frequency of status blinker // Has nothing to do with the temperature stuff, but indicates that the // arduino is doing something // // Standard: 1 second unsigned long blinkInterval = 1000; // Initialize variables int toggle = 1; double currentTemp, temperature; unsigned long lastBlinkTime; unsigned long lastFetchTime; double tempArray[windowSize]; int first; void setup(void) { // Initialize inputs/outputs // Start serial port Serial.begin(38400); delay(2000); // Initialize the digital pin as an output: pinMode(ledPin, OUTPUT); // At startup, fill array with current temperature currentTemp = fetchTemp(); for(int i = 0; i < windowSize; i++) { shift_in(currentTemp, tempArray, windowSize); } temperature = currentTemp; } // Main loop void loop(void) { // If there has been passed a certain time if (millis() - lastFetchTime > fetchInterval) { // save the last time lastFetchTime = millis(); // Fetch current temp currentTemp = fetchTemp(); // Shift in current temperature into temperature array shift_in(currentTemp, tempArray, windowSize); // Determine averaged temperature temperature = mean(tempArray, windowSize); } // Do some blinking every time if (millis() - lastBlinkTime > blinkInterval) { // save the last time lastBlinkTime = millis(); if(toggle == 1) { digitalWrite(ledPin, LOW); toggle = 0; } else { digitalWrite(ledPin, HIGH); toggle = 1; } } // Send data only when you receive data: if (Serial.available() > 0) { while(Serial.available()) { Serial.flush(); } // Say what you got to say: Serial.println(temperature); } } // Calculates the mean of all items in the array double mean(double *a, double length) { double sum = 0; for(int i = 0; i < length; i++) { sum += a[i]; } return (sum/length); } // Assigns the new item as first element of an array and shifts every other // items one position to the end. The last item is discarded void shift_in(double newItem, double *a, double length) { for(int i = 0; i < length-1; i++) { a[i+1] = a[i]; } a[0] = newItem; } // The self-contained fetch temp function. // Most of the code was taken from the example code of this page: // http://www.arduino.cc/playground/Learning/OneWire double fetchTemp(void) { byte i; byte present = 0; byte data[12]; byte addr[8]; // Some test routines if ( !ds.search(addr)) { ds.reset_search(); } if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.print("CRC is not valid!\n"); } if ( addr[0] != 0x28) { Serial.print("Device is not a DS18B20 family device.\n"); } ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); // Debug output: //Serial.print(data[i], HEX); //Serial.print(" "); } int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract; double result; LowByte = data[0]; HighByte = data[1]; TReading = (HighByte << 8) + LowByte; SignBit = TReading & 0x8000; // test most sig bit if (SignBit) // negative { TReading = (TReading ^ 0xffff) + 1; // 2's comp } Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25 Whole = Tc_100 / 100; // separate off the whole and fractional portions Fract = Tc_100 % 100; result = Whole; result += ((double)Fract/100); if(SignBit) { result *= -1; } return result; }