Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ON 1
- #define OFF 0
- #define DUTY 50 //A single millis loop duty for all needed timers
- #define TWO_SEC 40 //DUTY x 40 = 2000 milliseconds
- #define FIVE_SEC 100 //DUTY x 100 = 5000 milliseconds
- const int powerButtonPin = 18, defrostRelayPin = 25, compressorRelayPin = 26,
- tempThresholdLow = -1027;
- bool btnState = 1, btnLastState = 1, btnPress = 0,
- defrost = 0, mode = OFF;
- float currentTemp;
- uint8_t defrostTimer = 0, tempTimer = 0;
- uint32_t previousTime = 0;
- #define ONE_WIRE_BUS 12
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- void updateFreezer(void) {
- if (mode) {
- Serial.println("POWER ON");
- digitalWrite(compressorRelayPin, ON); // turns on Compressor
- }else{
- defrost = true;
- defrostTimer = 0;
- Serial.println("MANUAL DEFROST");
- digitalWrite(defrostRelayPin, ON); // turns on Defrost
- Serial.println("Harvesting Now");
- }
- return;
- }
- void defrosting(void){
- // Time passed so turn everything off
- Serial.println("Defrost Done Turning OFF Now");
- digitalWrite(defrostRelayPin, OFF); // turn off defrost
- digitalWrite(compressorRelayPin, OFF); // turn off compressor
- return;
- }
- void setup() {
- Serial.begin(115200); // initialize serial port
- sensors.begin(); // Start up the Sensor library
- pinMode(powerButtonPin, INPUT_PULLUP); // Creates active LOW logic and no resistor needed
- pinMode(defrostRelayPin, OUTPUT); // initialize digital pin25 as an output
- pinMode(compressorRelayPin, OUTPUT); // initialize digital pin26 as an output
- digitalWrite(compressorRelayPin, OFF); // turns off Compressor
- digitalWrite(defrostRelayPin, OFF); // turns off Compressor
- }
- void loop() {
- btnState = digitalRead(powerButtonPin); //Check Button State
- if(btnState != btnLastState){ //Check for state Change of the button
- btnPress = true; //To activate the button
- previousTime = millis(); //To debounce the button
- }
- if (millis()- previousTime >= DUTY){
- defrostTimer++; tempTimer++; //variable counters that create timers
- if(btnPress){ //If the button was pressed
- if(btnState == LOW){ //Check if the button was pressed (Active LOW)
- mode = !mode; //Toggle the mode state
- updateFreezer(); //Update the relays
- }
- btnPress = false; //Reset for next pressed
- }
- if(defrost){ //If defrost timer activated
- if(defrostTimer >= TWO_SEC){ //Two second timer
- defrosting(); //Turn freezer OFF
- defrost = 0; //Disable defrost cycle
- }
- }
- if(tempTimer >= FIVE_SEC){ //Read the Temperature every 5 seconds
- tempTimer = 0; //Counter reset
- sensors.requestTemperatures();
- Serial.print("Temperature is: ");
- currentTemp = sensors.getTempCByIndex(0);
- Serial.print(currentTemp); // Print the temperature in Celsius
- Serial.println("°C");
- }
- if ((currentTemp <= tempThresholdLow) && (digitalRead(compressorRelayPin) == ON)){
- mode = OFF; //So the next button press will turn the freezer on
- Serial.println("Entering Auto Defrost");
- digitalWrite(defrostRelayPin, ON); // turn on defrost
- defrostTimer = 0; // start the defrost timer
- defrost = true; // activate defrost mode cycle
- }
- previousTime = millis(); // reset main loop timer for next DUTY cycle period
- }
- btnLastState = btnState; // So we know when the button has been pressed
- }
Advertisement
Add Comment
Please, Sign In to add comment