Advertisement
sxiii

Controlling the state of 8-relays by thermistors

Feb 15th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.64 KB | None | 0 0
  1. #include <OneWire.h>
  2.  
  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.  
  8. // KNOWN ROMs (IDs of Thermistors)    //
  9. // 28FF988B8141CD // 28ff988b8141cd   //
  10. // 28FFAC6D68142BC // 28ffac6d68142bc //
  11. // 28FFE6746814243 // 28ffe6746814243 //
  12. // 28FFD96F681424 // 28ffd96f681424   //
  13. // 28FF734B814175 // 28ff734b814175   //
  14. // 28FF7BB66814378 // 28ff7bb66814378 //
  15. // 28FF8758691438 // 28ff8758691438   //
  16. // 28FF57BB81418 // 28ff57bb81418     //
  17. ////////////////////////////////////////
  18.  
  19. OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.   pinMode(2, OUTPUT);
  24.   pinMode(3, OUTPUT);
  25.   pinMode(4, OUTPUT);
  26.   pinMode(5, OUTPUT);
  27.   pinMode(6, OUTPUT);
  28.   pinMode(7, OUTPUT);
  29.   pinMode(8, OUTPUT);
  30.   pinMode(9, OUTPUT);
  31. }
  32.  
  33. void loop() {
  34.  
  35.   byte i;
  36.   byte present = 0;
  37.   byte type_s;
  38.   byte data[12];
  39.   byte addr[8];
  40.   float celsius;
  41.   String aa;
  42.   String ab;
  43.   String xx;
  44.   String check;
  45.    
  46.   if ( !ds.search(addr)) {
  47.     Serial.println("No more addresses.");
  48.     Serial.println();
  49.     ds.reset_search();
  50.     delay(0);
  51.     return;
  52.   }
  53.  
  54.   if (OneWire::crc8(addr, 7) != addr[7]) {
  55.       Serial.println("CRC is not valid!");
  56.       return;
  57.   }
  58.  // Serial.println();
  59.  
  60.   // the first ROM byte indicates which chip
  61.   switch (addr[0]) {
  62.     case 0x10:
  63.       aa = "Chip = DS18S20,";  // or old DS1820
  64.       type_s = 1;
  65.       break;
  66.     case 0x28:
  67.       aa = "Chip = DS18B20,";
  68.       type_s = 0;
  69.       break;
  70.     case 0x22:
  71.       aa = "Chip = DS1822,";
  72.       type_s = 0;
  73.       break;
  74.     default:
  75.       aa = "Device is not a DS18x20 family device.";
  76.       return;
  77.   }
  78.  
  79.   ds.reset();
  80.   ds.select(addr);
  81.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  82.  
  83.   delay(750);     // maybe 750ms is enough, maybe not
  84.   // we might do a ds.depower() here, but the reset will take care of it.
  85.  
  86.   aa += " Data = ";
  87.  
  88.   present = ds.reset();
  89.   ds.select(addr);    
  90.   ds.write(0xBE);         // Read Scratchpad
  91.   aa += String(present, HEX);
  92.  
  93.   //Serial.print(" ");
  94.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  95.     data[i] = ds.read();
  96.     aa += String(data[i], HEX);
  97.    // Serial.print(" ");
  98.   }
  99.   aa += ", CRC = ";
  100.   aa += String(OneWire::crc8(data, 8), HEX);
  101.  
  102.   //Serial.print
  103.  // Serial.println();
  104.  
  105.   // Convert the data to actual temperature
  106.   // because the result is a 16 bit signed integer, it should
  107.   // be stored to an "int16_t" type, which is always 16 bits
  108.   // even when compiled on a 32 bit processor.
  109.   int16_t raw = (data[1] << 8) | data[0];
  110.   if (type_s) {
  111.     raw = raw << 3; // 9 bit resolution default
  112.     if (data[7] == 0x10) {
  113.       // "count remain" gives full 12 bit resolution
  114.       raw = (raw & 0xFFF0) + 12 - data[6];
  115.     }
  116.   } else {
  117.     byte cfg = (data[4] & 0x60);
  118.     // at lower res, the low bits are undefined, so let's zero them
  119.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  120.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  121.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  122.     //// default is 12 bit resolution, 750 ms conversion time
  123.   }
  124.   celsius = (float)raw / 16.0;
  125.   aa += (", Temperature = ");
  126.   aa += (celsius);
  127.   aa += (" celsius, ");
  128.  
  129.   delay(0);
  130.  
  131.   aa += "ROM = ";
  132.   for( i = 0; i < 8; i++) {
  133.     //Serial.write(' '); // <- this can be used to add spaces
  134.  
  135.       // Checking if any of temp is higher than 30 degree C
  136.       check += String(addr[i], HEX);
  137.  
  138.   }
  139.  
  140.       if (check == "28ff988b8141cd" && celsius >= 30.00) { digitalWrite(2, LOW); } else { digitalWrite(2, HIGH); }
  141.       if (check == "28ffac6d68142bc" && celsius >= 30.00) { digitalWrite(3, LOW); } else { digitalWrite(3, HIGH); }
  142.       if (check == "28ffe6746814243" && celsius >= 30.00) { digitalWrite(4, LOW); } else { digitalWrite(4, HIGH); }
  143.       if (check == "28ffd96f681424" && celsius >= 30.00) { digitalWrite(5, LOW); } else { digitalWrite(5, HIGH); }
  144.       if (check == "28ff734b814175" && celsius >= 30.00) { digitalWrite(6, LOW); } else { digitalWrite(6, HIGH); }
  145.       if (check == "28ff7bb66814378" && celsius >= 30.00) { digitalWrite(7, LOW); } else { digitalWrite(7, HIGH); }
  146.       if (check == "28ff8758691438" && celsius >= 30.00) { digitalWrite(8, LOW); } else { digitalWrite(8, HIGH); }
  147.       if (check == "28ff57bb81418" && celsius >= 30.00) { digitalWrite(9, LOW); } else { digitalWrite(9, HIGH); }
  148.      
  149.     aa += check;
  150.  
  151.   Serial.print(aa);
  152.   Serial.println();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement