Advertisement
sxiii

DS18x20 Temperature Reading & Turn 8 Relays If Temp Changes

Feb 22nd, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. // CODE TESTED ON ARDUINO 1.6.7 IDE
  2. // HEAVY MODIFIED BY SECURITYXIII TO GET 8 THERMISTORS
  3. // OneWire DS18S20, DS18B20, DS1822 Temperature Example
  4. // http://www.pjrc.com/teensy/td_libs_OneWire.html
  5. // The DallasTemperature library can do all this work for you!
  6. // http://milesburton.com/Dallas_Temperature_Control_Library
  7. #include <OneWire.h>
  8. OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)
  9. // KNOWN ROMs (IDs of Thermistors)    //
  10. const char* therm1 = "28ff988b8141cd";
  11. const char* therm2 = "28ffac6d68142bc";
  12. const char* therm3 = "28ffe6746814243";
  13. const char* therm4 = "28ffd96f681424";
  14. const char* therm5 = "28ff734b814175";
  15. const char* therm6 = "28ff7bb66814378";
  16. const char* therm7 = "28ff8758691438";
  17. const char* therm8 = "28ff57bb81418";
  18. const char* therms[] = {therm1, therm2, therm3, therm4, therm5, therm6, therm7, therm8};
  19. char rel1; char rel2; char rel3; char rel4; char rel5; char rel6; char rel7; char rel8;
  20. char rels[] = {rel1, rel2, rel3, rel4, rel5, rel6, rel7, rel8};
  21. int o; int z;
  22.  
  23. // RELAY LOGIC
  24. char on = LOW; char off = HIGH;
  25. // TEMP
  26. int celmax = 40.00;
  27.  
  28. void setup() {
  29.   char x = OUTPUT;
  30.   for (z = 2; z < 10; z++) {
  31.       pinMode(z, x);
  32.   }
  33.   Serial.begin(9600);
  34. }
  35.  
  36. void loop() {
  37.  
  38. ///////// DO NOT TOUCH - THIS CODE IS WORKING WITH TEMP ! /////////
  39.  
  40.   byte i;
  41.   byte present = 0;
  42.   byte type_s;
  43.   byte data[12];
  44.   byte addr[8];
  45.   float celsius;
  46.   String aa;
  47.   String ab;
  48.   String xx;
  49.   String check;
  50.    
  51.   if ( !ds.search(addr)) {
  52.     Serial.println("No more addresses.");
  53.     Serial.println();
  54.     ds.reset_search();
  55.     delay(0);
  56.     return;
  57.   }
  58.  
  59.   if (OneWire::crc8(addr, 7) != addr[7]) {
  60.       Serial.println("CRC is not valid!");
  61.       return;
  62.   }
  63.  // Serial.println();
  64.  
  65.   // the first ROM byte indicates which chip
  66.   switch (addr[0]) {
  67.     case 0x10:
  68.       aa = "Chip = DS18S20,";  // or old DS1820
  69.       type_s = 1;
  70.       break;
  71.     case 0x28:
  72.       aa = "Chip = DS18B20,";
  73.       type_s = 0;
  74.       break;
  75.     case 0x22:
  76.       aa = "Chip = DS1822,";
  77.       type_s = 0;
  78.       break;
  79.     default:
  80.       aa = "Device is not a DS18x20 family device.";
  81.       return;
  82.   }
  83.  
  84.   ds.reset();
  85.   ds.select(addr);
  86.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  87.  
  88.   // delay(750);     // maybe 750ms is enough, maybe not
  89.   // we might do a ds.depower() here, but the reset will take care of it.
  90.  
  91.   aa += " Data = ";
  92.  
  93.   present = ds.reset();
  94.   ds.select(addr);    
  95.   ds.write(0xBE);         // Read Scratchpad
  96.   aa += String(present, HEX);
  97.  
  98.   //Serial.print(" ");
  99.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  100.     data[i] = ds.read();
  101.     aa += String(data[i], HEX);
  102.    // Serial.print(" ");
  103.   }
  104.   aa += ", CRC = ";
  105.   aa += String(OneWire::crc8(data, 8), HEX);
  106.  
  107.   //Serial.print
  108.  // Serial.println();
  109.  
  110.   // Convert the data to actual temperature
  111.   // because the result is a 16 bit signed integer, it should
  112.   // be stored to an "int16_t" type, which is always 16 bits
  113.   // even when compiled on a 32 bit processor.
  114.   int16_t raw = (data[1] << 8) | data[0];
  115.   if (type_s) {
  116.     raw = raw << 3; // 9 bit resolution default
  117.     if (data[7] == 0x10) {
  118.       // "count remain" gives full 12 bit resolution
  119.       raw = (raw & 0xFFF0) + 12 - data[6];
  120.     }
  121.   } else {
  122.     byte cfg = (data[4] & 0x60);
  123.     // at lower res, the low bits are undefined, so let's zero them
  124.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  125.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  126.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  127.     //// default is 12 bit resolution, 750 ms conversion time
  128.   }
  129.   celsius = (float)raw / 16.0;
  130.   aa += (", Temperature = ");
  131.   aa += (celsius);
  132.   aa += (" celsius, ");
  133.  
  134.   delay(0);
  135.  
  136.   aa += "ROM = ";
  137.   for( i = 0; i < 8; i++) {
  138.     //Serial.write(' '); // <- this can be used to add spaces
  139.  
  140.       check += String(addr[i], HEX);
  141.  
  142.   }
  143.  
  144. ///////// IT'S MOSTLY SAFE TO TOUCH FROM HERE /////////
  145. // Checking that temp is higher than celmax
  146.      
  147. for (o = 0; o < 8; o++) {
  148. if (check == therms[o])  { if (celsius >= celmax) { rels[o] = on; } else { rels[o] = off; } }
  149. digitalWrite(o+2, rels[o]);
  150. }
  151.  
  152.     aa += check;
  153.  
  154.   Serial.print(aa);
  155.   Serial.println();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement