macca-nz

auto_defrost_freezer

Feb 27th, 2023
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.20 KB | Source Code | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #define ON        1
  4. #define OFF       0
  5. #define DUTY      50        //A single millis loop duty for all needed timers
  6. #define TWO_SEC   40        //DUTY x 40   = 2000 milliseconds
  7. #define FIVE_SEC  100       //DUTY x 100  = 5000 milliseconds
  8.  
  9. const int powerButtonPin = 18, defrostRelayPin = 25, compressorRelayPin = 26,
  10.           tempThresholdLow = -1027;
  11. bool      btnState = 1, btnLastState = 1, btnPress = 0,
  12.           defrost = 0, mode = OFF;
  13. float     currentTemp;
  14. uint8_t   defrostTimer = 0, tempTimer = 0;
  15. uint32_t  previousTime = 0;
  16.  
  17. #define ONE_WIRE_BUS 12
  18.  
  19. OneWire oneWire(ONE_WIRE_BUS);
  20.  
  21. DallasTemperature sensors(&oneWire);
  22.  
  23. void updateFreezer(void) {
  24.       if (mode) {
  25.         Serial.println("POWER ON");
  26.         digitalWrite(compressorRelayPin, ON);  // turns on Compressor
  27.       }else{
  28.         defrost = true;
  29.         defrostTimer = 0;
  30.         Serial.println("MANUAL DEFROST");
  31.         digitalWrite(defrostRelayPin, ON);  // turns on Defrost
  32.         Serial.println("Harvesting Now");
  33.       }
  34.     return;
  35. }
  36.  
  37. void defrosting(void){
  38.     // Time passed so turn everything off
  39.     Serial.println("Defrost Done Turning OFF Now");
  40.     digitalWrite(defrostRelayPin, OFF);     // turn off defrost
  41.     digitalWrite(compressorRelayPin, OFF);  // turn off compressor
  42.     return;
  43. }
  44.  
  45. void setup() {
  46.   Serial.begin(115200);                   // initialize serial port
  47.   sensors.begin();                        // Start up the Sensor library
  48.   pinMode(powerButtonPin, INPUT_PULLUP);  // Creates active LOW logic and no resistor needed
  49.   pinMode(defrostRelayPin, OUTPUT);       // initialize digital pin25 as an output
  50.   pinMode(compressorRelayPin, OUTPUT);    // initialize digital pin26 as an output
  51.   digitalWrite(compressorRelayPin, OFF);  // turns off Compressor
  52.   digitalWrite(defrostRelayPin, OFF);     // turns off Compressor
  53. }
  54.  
  55. void loop() {  
  56.     btnState = digitalRead(powerButtonPin); //Check Button State
  57.     if(btnState != btnLastState){           //Check for state Change of the button
  58.         btnPress = true;                     //To activate the button
  59.         previousTime = millis();            //To debounce the button
  60.     }
  61.     if (millis()- previousTime >= DUTY){
  62.         defrostTimer++; tempTimer++;          //variable counters that create timers
  63.             if(btnPress){                      //If the button was pressed
  64.                 if(btnState == LOW){          //Check if the button was pressed (Active LOW)
  65.                     mode = !mode;             //Toggle the mode state
  66.                     updateFreezer();          //Update the relays
  67.                 }
  68.               btnPress = false;               //Reset for next pressed
  69.             }
  70.             if(defrost){                      //If defrost timer activated
  71.                 if(defrostTimer >= TWO_SEC){  //Two second timer
  72.                     defrosting();             //Turn freezer OFF
  73.                     defrost = 0;              //Disable defrost cycle
  74.                 }
  75.             }
  76.             if(tempTimer >= FIVE_SEC){        //Read the Temperature every 5 seconds
  77.                 tempTimer = 0;                //Counter reset
  78.                 sensors.requestTemperatures();
  79.                 Serial.print("Temperature is: ");
  80.                 currentTemp = sensors.getTempCByIndex(0);
  81.                 Serial.print(currentTemp);                // Print the temperature in Celsius
  82.                 Serial.println("°C");
  83.             }
  84.             if ((currentTemp <= tempThresholdLow) && (digitalRead(compressorRelayPin) == ON)){
  85.                 mode = OFF;                  //So the next button press will turn the freezer on
  86.                 Serial.println("Entering Auto Defrost");
  87.                 digitalWrite(defrostRelayPin, ON);     // turn on defrost
  88.                 defrostTimer = 0;                     // start the defrost timer
  89.                 defrost = true;                        // activate defrost mode cycle
  90.             }
  91.         previousTime = millis();                       // reset main loop timer for next DUTY cycle period
  92.     }
  93.     btnLastState = btnState;                           // So we know when the button has been pressed
  94. }
Advertisement
Add Comment
Please, Sign In to add comment