Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.22 KB | None | 0 0
  1. // This library contains functions to set various low-power
  2. // states for the ATmega328
  3. #include <avr/sleep.h>
  4.  
  5. #include <OneWire.h>
  6.  
  7. // This variable is made volatile because it is changed inside
  8. // an interrupt function
  9. volatile int heat_count = 0; // Keep track of how many sleep
  10. // cycles have been completed .
  11. const int interval = 10; // Interval in minutes between waking
  12. // and doing tasks.
  13. int min_heat_count = (interval*60)/8; // Approximate minimum number
  14. // of sleep cycles needed before the heat shoudl turn off
  15.  
  16. int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
  17. int PowerSwitch_Pin = 9; //Powerswitch Output pin on digital 9
  18. float Heat_Temp = -2.0; //temperature to heat until
  19. float Cool_Temp = -7.0; //temerpature to cool until
  20. int TempLED_Pin = 10; //Temperate LED pin on digital 10
  21. float Error_Temp = -1000; //Temperature returned if there is an error.
  22. boolean isOn = false;
  23.  
  24. //Temperature chip i/o
  25. OneWire ds(DS18S20_Pin); // on digital pin 2
  26.  
  27. void setup(void)
  28. {
  29.   watchdogOn(); // Turn on the watch dog timer.
  30.  
  31.   Serial.begin(9600);
  32.   pinMode(PowerSwitch_Pin, OUTPUT);
  33.   pinMode(TempLED_Pin, OUTPUT);
  34.  
  35.   float temperature = getTemp();
  36.   Serial.println("Starting Up!");
  37.   Serial.println(temperature);
  38.   processTemperature(temperature);
  39.   Serial.flush();
  40. }
  41.  
  42. void loop(void)
  43. {
  44.   goToSleep(); // ATmega328 goes to sleep for about 8 seconds
  45.   // and continues to execute code when it wakes up
  46.   float temperature = getTemp();
  47.  
  48.   processTemperature( temperature);
  49.  
  50.   Serial.flush();
  51. }
  52.  
  53. void processTemperature( float temp )
  54. {
  55.   if( temp == Error_Temp )
  56.   {
  57.     //Ack, signal an error.
  58.     Serial.println("Error from Temp Sensor");
  59.     flashLED(TempLED_Pin, 5);
  60.     //Just to be safe, turn the heat on.
  61.     Serial.println("Turn On");
  62.     digitalWrite(PowerSwitch_Pin, HIGH);
  63.     return;
  64.   }
  65.  
  66.   Serial.println(temp);
  67.  
  68.   if( temp <= Cool_Temp ||
  69.       (temp < Heat_Temp && isOn)  )
  70.   {
  71.     //coop is below the minimum temperature OR
  72.     //coop is between the minimum temperature and maximum
  73.     //temperature and is heating up.
  74.     Serial.println("Turn On");
  75.     digitalWrite(PowerSwitch_Pin, HIGH);
  76.     isOn = true;
  77.     flashLED(TempLED_Pin, 2);
  78.     delay(200);
  79.     flashLEDTemp(TempLED_Pin, temp);  
  80.   }
  81.   /*else if( temp >= Heat_Temp && isOn )
  82.   {
  83.     //the coop is above the maximum temperature
  84.     //and is heating up.
  85.     //Check to see if we've been on long enough
  86.     //if so, turn off.
  87.     if( heat_count >= min_heat_count )
  88.     {
  89.       Serial.println("Turn Off");
  90.       digitalWrite(PowerSwitch_Pin, LOW);
  91.       heat_count = 0;
  92.       isOn = false;
  93.       flashLED(TempLED_Pin, 1);
  94.       delay(200);
  95.       flashLEDTemp(TempLED_Pin, temp);  
  96.     }
  97.     else
  98.     {
  99.       flashLED(TempLED_Pin, 2);
  100.       delay(200);
  101.       flashLEDTemp(TempLED_Pin, temp);  
  102.     }
  103.   }*/
  104.   else if( temp >= Heat_Temp ||
  105.            (temp <= Heat_Temp && !isOn) )
  106.   {
  107.     //coop is above the maximum temperature OR
  108.     //coop is between the minimum temperature and maximum
  109.     //temperature and is cooling off.
  110.     Serial.println("Turn Off");
  111.     digitalWrite(PowerSwitch_Pin, LOW);
  112.     isOn = false;
  113.     heat_count = 0;
  114.     flashLED(TempLED_Pin, 1);
  115.     delay(200);
  116.     flashLEDTemp(TempLED_Pin, temp);  
  117.   }
  118. }
  119.  
  120. void flashLED( int pin, int times )
  121. {
  122.   for( int ii=0; ii<times; ii++ )
  123.   {
  124.     digitalWrite(pin, HIGH);
  125.     delay(200);
  126.     digitalWrite(pin, LOW);
  127.     delay(300);    
  128.   }
  129. }
  130.  
  131. void flashLEDTemp( int pin, float temperature )
  132. {
  133.   //if the temp is > 0, do a long flash
  134.   if( temperature > 0.0 )
  135.   {
  136.     digitalWrite(pin, HIGH);
  137.     delay(500);
  138.     digitalWrite(pin, LOW);
  139.     delay(200);
  140.     return;
  141.   }
  142.  
  143.   int numTimesFlash = (int)(-1.0*temperature / 2.0) + 1;
  144.   flashLED( pin, numTimesFlash );
  145. }
  146.  
  147. float getTemp(){
  148.  //returns the temperature from one DS18S20 in DEG Celsius
  149.  
  150.  byte data[12];
  151.  byte addr[8];
  152.  
  153.  if ( !ds.search(addr)) {
  154.    //no more sensors on chain, reset search
  155.    ds.reset_search();
  156.    return Error_Temp;
  157.  }
  158.  
  159.  if ( OneWire::crc8( addr, 7) != addr[7]) {
  160.    Serial.println("CRC is not valid!");
  161.    return Error_Temp;
  162.  }
  163.  
  164.  if ( addr[0] != 0x10 && addr[0] != 0x28) {
  165.    Serial.print("Device is not recognized");
  166.    return Error_Temp;
  167.  }
  168.  
  169.  ds.reset();
  170.  ds.select(addr);
  171.  ds.write(0x44,1); // start conversion, with parasite power on at the end
  172.  
  173.  byte present = ds.reset();
  174.  ds.select(addr);  
  175.  ds.write(0xBE); // Read Scratchpad
  176.  
  177.  
  178.  for (int i = 0; i < 9; i++) { // we need 9 bytes
  179.   data[i] = ds.read();
  180.  }
  181.  
  182.  ds.reset_search();
  183.  
  184.  byte MSB = data[1];
  185.  byte LSB = data[0];
  186.  
  187.  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  188.  float TemperatureSum = tempRead / 16;
  189.  
  190.  return TemperatureSum;
  191.  
  192. }
  193.  
  194. void goToSleep()  
  195. {
  196. // The ATmega328 has five different sleep states.
  197. // See the ATmega 328 datasheet for more information.
  198. // SLEEP_MODE_IDLE -the least power savings
  199. // SLEEP_MODE_ADC
  200. // SLEEP_MODE_PWR_SAVE
  201. // SLEEP_MODE_STANDBY
  202. // SLEEP_MODE_PWR_DOWN -the most power savings
  203. // I am using the deepest sleep mode from which a
  204. // watchdog timer interrupt can wake the ATMega328
  205.  
  206. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode.
  207. sleep_enable(); // Enable sleep mode.
  208. sleep_mode(); // Enter sleep mode.
  209. // After waking from watchdog interrupt the code continues
  210. // to execute from this point.
  211.  
  212. sleep_disable(); // Disable sleep mode after waking.
  213.                      
  214. }
  215.  
  216. void watchdogOn() {
  217.  
  218. // Clear the reset flag, the WDRF bit (bit 3) of MCUSR.
  219. MCUSR = MCUSR & B11110111;
  220.  
  221. // Set the WDCE bit (bit 4) and the WDE bit (bit 3)
  222. // of WDTCSR. The WDCE bit must be set in order to
  223. // change WDE or the watchdog prescalers. Setting the
  224. // WDCE bit will allow updtaes to the prescalers and
  225. // WDE for 4 clock cycles then it will be reset by
  226. // hardware.
  227. WDTCSR = WDTCSR | B00011000;
  228.  
  229. // Set the watchdog timeout prescaler value to 1024 K
  230. // which will yeild a time-out interval of about 8.0 s.
  231. WDTCSR = B00100001;
  232.  
  233. // Enable the watchdog timer interupt.
  234. WDTCSR = WDTCSR | B01000000;
  235. MCUSR = MCUSR & B11110111;
  236.  
  237. }
  238.  
  239. ISR(WDT_vect)
  240. {
  241.   if( isOn )
  242.   {
  243.     heat_count ++; // keep track of how many sleep cycles
  244.                   // have been completed if heat is on.
  245.   }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement