Ice maker to air conditioner troubleshooting

Sorry if this is the wrong form.

My project is confusing me. I'm playing with converting a countertop ice machine to an "air conditioner". I power the 2 fans, water pump, and compressor off of a 120vac to 12vdc power supply rated at 6 amps fed through an Arduino relay board rated at 10a at 30vdc and 15a and 125vac. The ice machine was rated at 115vac at 1.7a originally. I added a high speed CPU fan to act as a "blower" that draws about 2 amps max at 12vdc. I initially ran the Arduino Nano (Mini Nano V3.0 ATmega328P)from the USB so i could monitor serial feedback from my script and it worked fine. I then switched the Nano up to a CV/CC buck converter set to between 8vdc and 11vdc. Everything works fine until I turn on the "blower" fan. The script seems to get "confused" and no longer reacts to button inputs as programmed. Any suggestions would be appreciated.

Does everything work if the fan is turned on first?

1 Like

It doesn't seem to matter when I turn it on in the sequence. I coded a function to be able to turn the fan on and off manually, but as soon as it turns on, nonresponsive. Again, this only happens when I power the Nano off of the external power (the Nano is the only thing powered from it).

Then I suspect your power supply is not sufficient for the starting surge needed by the fan.

I wondered the same, but I double-checked, and the buck converter is set to 750ma CC and the meter on it reads a max output current of about 400ma at 10vdc output. The converter is rated to 4A continuous (far more than the Nano would draw). I appreciate your willingness to help.

That sounds like a fun project. I will predict your AC will be big enough to air condition a refrigerator. I would guess you need at least 12000 BTU to cool your room, that will require over 7.5A for the compressor alone. Also you should let the compressor rest for about 5 minutes each time it is turned off. The refrigerant needs to equalize between the high and low sides to allow the compressor to start.

Fortunately it has some smarts to cycle it properly. When the motor failed i figured it would be fun to learn Arduino and A/C repurpasing.

To be honest that is how I learned refrigeration. Later I was also fortunate to work in an appliance repair shop that did refrigeration.

We have a small boat that we might play with it on. It really is a fun experiment trying to get the most out of a small system. I actually pump the water through a CPU radiator along with the cooling fingers. Followed by some creative airflow and Frankenstein is born.

1 Like

Sounds like a nice addition for a hot muggy night blowing on your face:-) Be sure to put the warm side in another room.

Maybe some creative ducting to the outside.

1 Like

So, I've been doing more research. It seems, according to the forums, that VIN could just be lacking in current capability and adjusting my buck converter to 5v and feeding the 5v pin might be better? Any thoughts or observations on this?

Hi, @n3now
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Better then a thousand words...

Your code might help to.

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Thanks... Tom.. :grinning: :+1: :coffee: :australia:

1 Like

I actually tried the 5vdc fixed input solution and it works. I'm posting the specifics (so far) so others can use/abuse as they see fit. There's a lot more for me to learn here and I look forward to playing around more. The hardware is as follows:

Lobotomized Frigidaire EFIC117-SS countertop ice maker
NOYITO AC to DC Isolated Power Supply Module AC 120V 110V - 245V to DC 12V 6A 72W Peak (Amazon)
Mini Nano V3.0 ATmega328P Microcontroller Board w/USB Cable for Arduino (Amazon)
ANMBEST Relay Module with Optocoupler High/Low Level Trigger for Arduino (5V Relay 6 Channel) (Amazon)
Boost Buck Converter, DROK DC 5.5-30V to 0.5-30V 5V 12V 24V Output Adjustable Power Supply Regulator Module, 4A 35W High Power Voltage Step Up Down Converter Board with Case LCD Display (Amazon)

//Button dfinitons
const int PWR_BTN = 6;  //Power button (red))
const int SEL_BTN = 7;  //Select button (orange)

//LED definitions
const int WATER_LED = 3;  //Water low LED (blue)
const int ICE_LED = 4;    //Ice LED (yellow)
const int PWR_LED = 5;    //Power LED (green)

//Relay controls
const int BYPASS_VALVE = 2; //Compressor bypass (120VAC - Relay 1)
const int WATER_PUMP = 9;   //Water pump (12VDC - Relay 6) and S LED
const int COMP_FAN = 10;    //Compressor fan (12VDC - Relay 5)
const int BLOWER_FAN = 11;  //Blower fan (12VDC -  Relay 4)
const int COMPRESSOR = 12;  //Compressor (120VAC - Relay 2) and L LED

//Water sensor pins
const int WATER_PWR = 8;     //Water sensor power supply pin (white)
const int WATER_SIGNAL = A5; //Water signal positive (gray)

//Constants used in the program
const int MIN_PRESS_TIME = 250;      //Minimum time to register a button press (milliseconds)
const int BYPASS_PRESS_TIME = 5000;  //minimum time to initiate thaw cycle (milliseconds)
const int WATERLEVELLIMIT = 250;     //Set minimum water level
const int LOOP_TIME = 1000;          //Loop timer for the program (milliseconds)
const int THAW_TIME = 20000;         //Thaw timer (milliseconds)
const int verboseStatus = HIGH;      //Display status info flag (High = yes, LOW = no)

//Water sensor settings
int waterCounterLimit = 10;  //Establish water check interval
int waterVal = 0;            //Initilize water sensor value
int waterCounter = 0;        //Initialize waterCounter
int waterLevel = LOW;        //Set waterLevel flag
int lastSelBtnState = LOW;   //Initialize button state
int currentSelBtnState;      //Initialize button state

//Button press time variables
unsigned long pressedTime = 0;   //Initialize button press time
unsigned long releasedTime = 0;  //Initialize button release time
long pressDuration = 0;          //Initialize button press duration time

//Power button and states
int lastPwrBtnState = HIGH;        //The previous state from the power button pin
int currentPwrBtnState;            //The current reading from the power button pin
int currentPwrState;               //The current status of the power
int currentBlowerState = LOW;      //The current status of the blower fan
int currentCompressorState = LOW;  //The current status of the compressor

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(PWR_BTN, INPUT_PULLUP);   //Set up power button to be normally high
  pinMode(SEL_BTN, INPUT_PULLUP);   //Set up the select button to be normally high
  pinMode(PWR_LED, OUTPUT);         //Set up the power LED for output
  pinMode(ICE_LED, OUTPUT);         //Set up the ice LED for output
  pinMode(WATER_LED, OUTPUT);       //Set up the water LED for output
  pinMode(WATER_PUMP, OUTPUT);      //Set up water pump relay output
  pinMode(COMP_FAN, OUTPUT);        //Set up compressor cooling fan output
  pinMode(BLOWER_FAN, OUTPUT);      //Set up blower fan output
  pinMode(COMPRESSOR, OUTPUT);      //Set up compressor output
  pinMode(BYPASS_VALVE, OUTPUT);    //Set up the bypass valve output
  pinMode(WATER_PWR, OUTPUT);       //Set up water sensor power
  digitalWrite(WATER_PWR, LOW);     //Turn off the water sensor 
  blinkLED(PWR_LED,2,200);          //Blink the power LED twice at 200msec interval
  digitalWrite(PWR_LED, LOW);       //Turn off power LED
  Serial.println("Initialized");
}

void loop() {
   if (waterCounter == 0){
    checkWaterLevel();
    if (waterLevel){
      digitalWrite(WATER_LED, LOW);
    } else {
        digitalWrite(WATER_LED, HIGH);
      }
  }  //(waterConter == 0)
  if (waterCounter < waterCounterLimit){
    waterCounter ++;
  } else if (waterCounter == waterCounterLimit) {
    waterCounter = 0;
  }  //(waterCounter < waterCounterLimit)
  
  if (verboseStatus){
    Serial.print("Water counter: ");
    Serial.println(waterCounter);
  }  //(verboseStatus)
  checkPowerBtn();
  checkSelectBtn();
  delay (LOOP_TIME);
}

void checkPowerBtn(){ 
  currentPwrBtnState = digitalRead(PWR_BTN);
  if (lastPwrBtnState == HIGH && currentPwrBtnState == LOW) { //power button pressed
    pressedTime = millis();
  } else if(lastPwrBtnState == LOW && currentPwrBtnState == HIGH) { //power button released
    releasedTime = millis();
    pressDuration = releasedTime - pressedTime;
    if (pressDuration > MIN_PRESS_TIME && currentPwrState == LOW) {
      if (waterVal > WATERLEVELLIMIT) {
        powerOn();
      }  //(waterVal > WATERLEVELLIMIT)
    } else if(pressDuration >= MIN_PRESS_TIME && currentPwrState == HIGH) { 
        if (pressDuration >= BYPASS_PRESS_TIME) {
          if (currentCompressorState == HIGH) {
            Serial.println("Manually disable compressor");
            digitalWrite(COMPRESSOR, LOW);
            currentCompressorState = LOW;
          } else {
              Serial.println("Manually enable compressor");
              digitalWrite(COMPRESSOR, HIGH);
              currentCompressorState = HIGH;
            }
        }  //(pressDuration >= BYPASS_PRESS_TIME)
        if (pressDuration < BYPASS_PRESS_TIME) {
          powerOff();
        }  //(pressDuration < BYPASS_PRESS_TIME)
      }  //else if (pressDuration >= MIN_PRESS_TIME && currentPwrState == HIGH)
    } //else if(lastPwrBtnState == LOW && currentPwrBtnState == HIGH)
  lastPwrBtnState = currentPwrBtnState; 
}  //checkPwrButton

void checkSelectBtn() {
  currentSelBtnState = digitalRead(SEL_BTN);
  if (currentPwrState == HIGH) {
    if (lastSelBtnState == HIGH && currentSelBtnState == LOW) {
      pressedTime = millis();
    } else if(lastSelBtnState == LOW && currentSelBtnState == HIGH) {
        releasedTime = millis();
        pressDuration = releasedTime - pressedTime;
        if (pressDuration  >= MIN_PRESS_TIME) {
          if (pressDuration >= BYPASS_PRESS_TIME) {
            thawCycle();
          } else {
            if (currentBlowerState == HIGH) {
              Serial.println("Manually turn off blower");
              digitalWrite(BLOWER_FAN, LOW);
              currentBlowerState = LOW;
            } else {
                Serial.println("Manually turn on blower");
                digitalWrite(BLOWER_FAN, HIGH);
                currentBlowerState = HIGH;
              }
            }
        } //(pressDuration >= BYPASS_PRESS_TIME)
      } //else if(lastSelBtnState == LOW && currentSelBtnState == HIGH)
  }  //(currentPwrState == HIGH)
  lastSelBtnState = currentSelBtnState;
}  //checkSelectBtn

void powerOn(){
  Serial.println("Powering on");
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.println("Turn on water pump and wait 10 seconds");
  }  //(verboseStatus)
  digitalWrite(WATER_PUMP, HIGH);
  blinkLED(PWR_LED,10,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.println("Turn on compressor fan and wait 1 second");
  }  //(verboseStatus)
  digitalWrite(COMP_FAN, HIGH);
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.println("Enable compressor and wait 6 seconds");
  }  //(verboseStatus)
  digitalWrite(COMPRESSOR, HIGH);
  currentCompressorState = HIGH;
  blinkLED(PWR_LED,6,500);  //Delay using LED blink
  digitalWrite(BLOWER_FAN, HIGH);
  currentBlowerState = HIGH;
  digitalWrite(PWR_LED, HIGH);
  currentPwrState = HIGH;
  Serial.println("Power up complete");
  waterCounter = 0;
}  //powerOn

void powerOff(){
  Serial.println("Powering off");
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.println("Turn off compressor and delay 1 second");
  }  //(verboseStatus)
  digitalWrite(COMPRESSOR, LOW);
  currentCompressorState = LOW;
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.println("Turn off blower fan and wait 3 seconds");
  }  //(verboseStatus)
  digitalWrite(BLOWER_FAN, LOW);
  currentBlowerState = LOW;
  blinkLED(PWR_LED,3,500);  //Delay using LED blink
  digitalWrite(PWR_LED, HIGH);
  if (verboseStatus){
    Serial.println("Turn off water pump and wait 10 seconds");
  }  //(verboseStatus)
  digitalWrite(WATER_PUMP, LOW);
  blinkLED(PWR_LED,10,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.println("Turn off compressor fan");
  }  //(verboseStatus)
  digitalWrite(COMP_FAN, LOW);
  blinkLED(PWR_LED,3,200);  //Delay using LED blink
  digitalWrite(WATER_LED, LOW);
  digitalWrite(PWR_LED, LOW);
  currentPwrState = LOW;
  Serial.println("Shutdown complete");
}  //powerOff

void thawCycle(){
  int tempBlowerState = currentBlowerState;
  Serial.println("Thaw cycle");
  if (tempBlowerState == HIGH){
    if (verboseStatus){
      Serial.println("Turn off blower fan and wait 1 seconds");
    }  //(verboseStatus)
    digitalWrite(BLOWER_FAN, LOW);
    currentBlowerState = LOW;
  }  //(currentBlowerState == HIGH)
  blinkLED(ICE_LED,1,500);  //Delay using LED blink
  if (verboseStatus){
    Serial.print("Turn on bypass valve for ");
    Serial.print((THAW_TIME) / 1000);
    Serial.println(" seconds");
  }  //(verboseStatus)
  digitalWrite(ICE_LED, HIGH);
  digitalWrite(BYPASS_VALVE, HIGH);
  blinkLED(ICE_LED,20,500);  //Delay using LED blink
  // delay(THAW_TIME);
  if (verboseStatus){
    Serial.println("Turn off bypass valve and wait for 1 second");
  }  //(verboseStatus)
  digitalWrite(BYPASS_VALVE, LOW);
  blinkLED(ICE_LED,1,500);  //Delay using LED blink
  digitalWrite(ICE_LED, LOW);
  if (tempBlowerState == HIGH) {
    if (verboseStatus){
      Serial.println("Turn on blower fan");
    }  //(verboseStatus)
    digitalWrite(BLOWER_FAN, HIGH);
    currentBlowerState = HIGH;
  }  //(blowerState == HIGH)
}  //thawCycle

void checkWaterLevel(){
  Serial.println("Check water level");
  digitalWrite(WATER_PWR, HIGH);  //Turn on the water sensor
  waterVal = analogRead(WATER_SIGNAL);  //Read the analog value from the sensor
  digitalWrite(WATER_PWR, LOW);  //Turn off the water sensor 
  if (waterVal > WATERLEVELLIMIT ) {
    if (verboseStatus) {
      Serial.print("Water level good at ");
      Serial.println(waterVal);
    }  //(verboseStatus)
    waterLevel = HIGH;
  } else {
    if (verboseStatus){
      Serial.print("Water level below threshold of ");
      Serial.println(WATERLEVELLIMIT);
    }  //(verboseStatus)
    waterLevel = LOW;
  }  //(waterVal > WATERLEVELLIMIT )
}  //checkWaterLevel

void blinkLED(int LEDPin,int reps, int blinkTime){  //reps = # of cycles to blink, blinkTime = on/off time in msec
  if (verboseStatus){
    Serial.print("Blinking pin")
  }
  for (int x=1; x<=reps; x++){
    digitalWrite(LEDPin, LOW);
    delay(blinkTime);
    digitalWrite(LEDPin, HIGH);
    delay(blinkTime);
  }  //(int x=1; x<=reps; x++)
}  //blinkLED

Here's an updated sketch with what I think is a better way to handle debug options.

#define DEBUG   //If you comment this line, the DPRINT & DPRINTLN lines are defined as blank.
#ifdef DEBUG    //Macros are usually in all capital letters.
   #define DPRINT(...)    Serial.print(__VA_ARGS__)     //DPRINT is a macro, debug print
   #define DPRINTLN(...)  Serial.println(__VA_ARGS__)   //DPRINTLN is a macro, debug print with new line
#else
   #define DPRINT(...)     //now defines a blank line
   #define DPRINTLN(...)   //now defines a blank line
#endif

//Button dfinitons
const int PWR_BTN = 6;  //Power button (red))
const int SEL_BTN = 7;  //Select button (orange)

//LED definitions
const int WATER_LED = 3;  //Water low LED (blue)
const int ICE_LED = 4;    //Ice LED (yellow)
const int PWR_LED = 5;    //Power LED (green)

//Relay controls
const int BYPASS_VALVE = 2; //Compressor bypass (120VAC - Relay 1)
const int WATER_PUMP = 9;   //Water pump (12VDC - Relay 6) and S LED
const int COMP_FAN = 10;    //Compressor fan (12VDC - Relay 5)
const int BLOWER_FAN = 11;  //Blower fan (12VDC -  Relay 4)
const int COMPRESSOR = 12;  //Compressor (120VAC - Relay 2) and L LED

//Water sensor pins
const int WATER_PWR = 8;     //Water sensor power supply pin (white)
const int WATER_SIGNAL = A5; //Water signal positive (gray)

//Constants used in the program
const int MIN_PRESS_TIME = 250;      //Minimum time to register a button press (milliseconds)
const int BYPASS_PRESS_TIME = 5000;  //minimum time to initiate thaw cycle (milliseconds)
const int WATERLEVELLIMIT = 250;     //Set minimum water level
const int LOOP_TIME = 1000;          //Loop timer for the program (milliseconds)
const int THAW_TIME = 20000;         //Thaw timer (milliseconds)

//Water sensor settings
int waterCounterLimit = 10;  //Establish water check interval
int waterVal = 0;            //Initilize water sensor value
int waterCounter = 0;        //Initialize waterCounter
int waterLevel = LOW;        //Set waterLevel flag
int lastSelBtnState = LOW;   //Initialize button state
int currentSelBtnState;      //Initialize button state

//Button press time variables
unsigned long pressedTime = 0;   //Initialize button press time
unsigned long releasedTime = 0;  //Initialize button release time
long pressDuration = 0;          //Initialize button press duration time

//Power button and states
int lastPwrBtnState = HIGH;        //The previous state from the power button pin
int currentPwrBtnState;            //The current reading from the power button pin
int currentPwrState;               //The current status of the power
int currentBlowerState = LOW;      //The current status of the blower fan
int currentCompressorState = LOW;  //The current status of the compressor

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(PWR_BTN, INPUT_PULLUP);   //Set up power button to be normally high
  pinMode(SEL_BTN, INPUT_PULLUP);   //Set up the select button to be normally high
  pinMode(PWR_LED, OUTPUT);         //Set up the power LED for output
  pinMode(ICE_LED, OUTPUT);         //Set up the ice LED for output
  pinMode(WATER_LED, OUTPUT);       //Set up the water LED for output
  pinMode(WATER_PUMP, OUTPUT);      //Set up water pump relay output
  pinMode(COMP_FAN, OUTPUT);        //Set up compressor cooling fan output
  pinMode(BLOWER_FAN, OUTPUT);      //Set up blower fan output
  pinMode(COMPRESSOR, OUTPUT);      //Set up compressor output
  pinMode(BYPASS_VALVE, OUTPUT);    //Set up the bypass valve output
  pinMode(WATER_PWR, OUTPUT);       //Set up water sensor power
  digitalWrite(WATER_PWR, LOW);     //Turn off the water sensor 
  blinkLED(PWR_LED,2,200);          //Blink the power LED twice at 200msec interval
  digitalWrite(PWR_LED, LOW);       //Turn off power LED
  Serial.println("\nInitialized");
}

void loop() {
   if (waterCounter == 0){
    checkWaterLevel();
    if (waterLevel){
      digitalWrite(WATER_LED, LOW);
    } else {
        digitalWrite(WATER_LED, HIGH);
      }
  }  //(waterConter == 0)
  if (waterCounter < waterCounterLimit){
    waterCounter ++;
  } else if (waterCounter == waterCounterLimit) {
    waterCounter = 0;
  }  //(waterCounter < waterCounterLimit)
  
  DPRINT("Water counter: ");
  DPRINTLN(waterCounter);
  checkPowerBtn();
  checkSelectBtn();
  delay (LOOP_TIME);
}

void checkPowerBtn(){ 
  currentPwrBtnState = digitalRead(PWR_BTN);
  if (lastPwrBtnState == HIGH && currentPwrBtnState == LOW) { //power button pressed
    pressedTime = millis();
  } else if(lastPwrBtnState == LOW && currentPwrBtnState == HIGH) { //power button released
    releasedTime = millis();
    pressDuration = releasedTime - pressedTime;
    if (pressDuration > MIN_PRESS_TIME && currentPwrState == LOW) {
      if (waterVal > WATERLEVELLIMIT) {
        powerOn();
      }  //(waterVal > WATERLEVELLIMIT)
    } else if(pressDuration >= MIN_PRESS_TIME && currentPwrState == HIGH) { 
        if (pressDuration >= BYPASS_PRESS_TIME) {
          if (currentCompressorState == HIGH) {
            Serial.println("Manually disable compressor");
            digitalWrite(COMPRESSOR, LOW);
            currentCompressorState = LOW;
          } else {
              Serial.println("Manually enable compressor");
              digitalWrite(COMPRESSOR, HIGH);
              currentCompressorState = HIGH;
            }
        }  //(pressDuration >= BYPASS_PRESS_TIME)
        if (pressDuration < BYPASS_PRESS_TIME) {
          powerOff();
        }  //(pressDuration < BYPASS_PRESS_TIME)
      }  //else if (pressDuration >= MIN_PRESS_TIME && currentPwrState == HIGH)
    } //else if(lastPwrBtnState == LOW && currentPwrBtnState == HIGH)
  lastPwrBtnState = currentPwrBtnState; 
}  //checkPwrButton

void checkSelectBtn() {
  currentSelBtnState = digitalRead(SEL_BTN);
  if (currentPwrState == HIGH) {
    if (lastSelBtnState == HIGH && currentSelBtnState == LOW) {
      pressedTime = millis();
    } else if(lastSelBtnState == LOW && currentSelBtnState == HIGH) {
        releasedTime = millis();
        pressDuration = releasedTime - pressedTime;
        if (pressDuration  >= MIN_PRESS_TIME) {
          if (pressDuration >= BYPASS_PRESS_TIME) {
            thawCycle();
          } else {
            if (currentBlowerState == HIGH) {
              Serial.println("Manually turn off blower");
              digitalWrite(BLOWER_FAN, LOW);
              currentBlowerState = LOW;
            } else {
                Serial.println("Manually turn on blower");
                digitalWrite(BLOWER_FAN, HIGH);
                currentBlowerState = HIGH;
              }
            }
        } //(pressDuration >= BYPASS_PRESS_TIME)
      } //else if(lastSelBtnState == LOW && currentSelBtnState == HIGH)
  }  //(currentPwrState == HIGH)
  lastSelBtnState = currentSelBtnState;
}  //checkSelectBtn

void powerOn(){
  Serial.println("Powering on");
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Turn on water pump and wait 10 seconds");
  digitalWrite(WATER_PUMP, HIGH);
  blinkLED(PWR_LED,10,500);  //Delay using LED blink
  DPRINTLN("Turn on compressor fan and wait 1 second");
  digitalWrite(COMP_FAN, HIGH);
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Enable compressor and wait 6 seconds");
  digitalWrite(COMPRESSOR, HIGH);
  currentCompressorState = HIGH;
  blinkLED(PWR_LED,6,500);  //Delay using LED blink
  digitalWrite(BLOWER_FAN, HIGH);
  currentBlowerState = HIGH;
  digitalWrite(PWR_LED, HIGH);
  currentPwrState = HIGH;
  Serial.println("Power up complete");
  waterCounter = 0;
}  //powerOn

void powerOff(){
  Serial.println("Powering off");
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Turn off compressor and delay 2 seconds");
  digitalWrite(COMPRESSOR, LOW);
  currentCompressorState = LOW;
  blinkLED(PWR_LED,2,500);  //Delay using LED blink
  DPRINTLN("Turn off blower fan and wait 3 seconds");
  digitalWrite(BLOWER_FAN, LOW);
  currentBlowerState = LOW;
  blinkLED(PWR_LED,3,500);  //Delay using LED blink
  digitalWrite(PWR_LED, HIGH);
  DPRINTLN("Turn off water pump and wait 10 seconds");
  digitalWrite(WATER_PUMP, LOW);
  blinkLED(PWR_LED,10,500);  //Delay using LED blink
  DPRINTLN("Turn off compressor fan");
  digitalWrite(COMP_FAN, LOW);
  blinkLED(PWR_LED,3,200);  //Delay using LED blink
  digitalWrite(WATER_LED, LOW);
  digitalWrite(PWR_LED, LOW);
  currentPwrState = LOW;
  Serial.println("Shutdown complete");
}  //powerOff

void thawCycle(){
  int tempBlowerState = currentBlowerState;
  Serial.println("Thaw cycle");
  if (tempBlowerState == HIGH){
    DPRINTLN("Turn off blower fan and wait 1 seconds");
    digitalWrite(BLOWER_FAN, LOW);
    currentBlowerState = LOW;
  }  //(currentBlowerState == HIGH)
  blinkLED(ICE_LED,1,500);  //Delay using LED blink
  DPRINT("Turn on bypass valve for ");
  DPRINT((THAW_TIME) / 1000);
  DPRINTLN(" seconds");
  digitalWrite(ICE_LED, HIGH);
  digitalWrite(BYPASS_VALVE, HIGH);
  blinkLED(ICE_LED,20,500);  //Delay using LED blink
  // delay(THAW_TIME);
  DPRINTLN("Turn off bypass valve and wait for 1 second");
  digitalWrite(BYPASS_VALVE, LOW);
  blinkLED(ICE_LED,1,500);  //Delay using LED blink
  digitalWrite(ICE_LED, LOW);
  if (tempBlowerState == HIGH) {
    DPRINTLN("Turn on blower fan");
    digitalWrite(BLOWER_FAN, HIGH);
    currentBlowerState = HIGH;
  }  //(blowerState == HIGH)
}  //thawCycle

void checkWaterLevel(){
  Serial.println("Check water level");
  digitalWrite(WATER_PWR, HIGH);  //Turn on the water sensor
  waterVal = analogRead(WATER_SIGNAL);  //Read the analog value from the sensor
  digitalWrite(WATER_PWR, LOW);  //Turn off the water sensor 
  if (waterVal > WATERLEVELLIMIT ) {
    DPRINT("Water level good at ");
    DPRINTLN(waterVal);
    waterLevel = HIGH;
  } else {
    DPRINT("Water level below threshold of ");
    DPRINTLN(WATERLEVELLIMIT);
    waterLevel = LOW;
  }  //(waterVal > WATERLEVELLIMIT )
}  //checkWaterLevel

void blinkLED(int LEDPin, int reps, int blinkTime){  //reps = # of cycles to blink, blinkTime = on/off time in msec
  DPRINT("Blinking pin ");
  DPRINT(LEDPin);
  DPRINT(" for ");
  DPRINT(reps);
  DPRINT(" reps at ");
  DPRINT(blinkTime);
  DPRINTLN(" milliseconds interval");
  for (int x=1; x<=reps; x++){
    digitalWrite(LEDPin, LOW);
    delay(blinkTime);
    digitalWrite(LEDPin, HIGH);
    delay(blinkTime);
  }  //(int x=1; x<=reps; x++)
}  //blinkLED

Updated code. Added debug printing and set up the water level check to run using timer 1.

/* ICE2AC - Version 2. Updated 10/29/2023
   Written by Kevin Filiatrault
   Free to use/modify
*/

#include <TimerOne.h>

#define DEBUG   //If you comment this line, the DPRINT & DPRINTLN lines are defined as blank.
#ifdef DEBUG    //Macros are usually in all capital letters.
   #define DPRINT(...)    Serial.print(__VA_ARGS__)     //DPRINT is a macro, debug print
   #define DPRINTLN(...)  Serial.println(__VA_ARGS__)   //DPRINTLN is a macro, debug print with new line
#else
   #define DPRINT(...)     //now defines a blank line
   #define DPRINTLN(...)   //now defines a blank line
#endif

//Button dfinitons
const int PWR_BTN = 6;  //Power button (red))
const int SEL_BTN = 7;  //Select button (orange)

//LED definitions
const int WATER_LED = 3;  //Water low LED (blue)
const int ICE_LED = 4;    //Ice LED (yellow)
const int PWR_LED = 5;    //Power LED (green)

//Relay controls
const int BYPASS_VALVE = 2; //Compressor bypass (120VAC - Relay 1)
const int WATER_PUMP = 9;   //Water pump (12VDC - Relay 6) and S LED
const int COMP_FAN = 10;    //Compressor fan (12VDC - Relay 5)
const int BLOWER_FAN = 11;  //Blower fan (12VDC -  Relay 4)
const int COMPRESSOR = 12;  //Compressor (120VAC - Relay 2) and L LED

//Water sensor pins
const int WATER_PWR = 8;     //Water sensor power supply pin (white)
const int WATER_SIGNAL = A5; //Water signal positive (gray)

//Constants used in the program
const int MIN_PRESS_TIME = 250;      //Minimum time to register a button press (milliseconds)
const int BYPASS_PRESS_TIME = 5000;  //minimum time to initiate thaw cycle (milliseconds)
const int WATERLEVELLIMIT = 250;     //Set minimum water level
const int LOOP_TIME = 1000;          //Loop timer for the program (milliseconds)
const int THAW_TIME = 20000;         //Thaw timer (milliseconds)

//Water sensor settings
int waterInterval = 10000000;  //Establish water check interval (microseconds)
int waterVal = 0;            //Initilize water sensor value
int lastSelBtnState = LOW;   //Initialize button state
int currentSelBtnState;      //Initialize button state

//Button press time variables
unsigned long pressedTime = 0;   //Initialize button press time
unsigned long releasedTime = 0;  //Initialize button release time
long pressDuration = 0;          //Initialize button press duration time

//Power button and states
int lastPwrBtnState = HIGH;        //The previous state from the power button pin
int currentPwrBtnState;            //The current reading from the power button pin
int currentPwrState;               //The current status of the power
int currentBlowerState = LOW;      //The current status of the blower fan
int currentCompressorState = LOW;  //The current status of the compressor

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(PWR_BTN, INPUT_PULLUP);   //Set up power button to be normally high
  pinMode(SEL_BTN, INPUT_PULLUP);   //Set up the select button to be normally high
  pinMode(PWR_LED, OUTPUT);         //Set up the power LED for output
  pinMode(ICE_LED, OUTPUT);         //Set up the ice LED for output
  pinMode(WATER_LED, OUTPUT);       //Set up the water LED for output
  pinMode(WATER_PUMP, OUTPUT);      //Set up water pump relay output
  pinMode(COMP_FAN, OUTPUT);        //Set up compressor cooling fan output
  pinMode(BLOWER_FAN, OUTPUT);      //Set up blower fan output
  pinMode(COMPRESSOR, OUTPUT);      //Set up compressor output
  pinMode(BYPASS_VALVE, OUTPUT);    //Set up the bypass valve output
  pinMode(WATER_PWR, OUTPUT);       //Set up water sensor power
  digitalWrite(WATER_PWR, LOW);     //Turn off the water sensor 
  blinkLED(PWR_LED,2,200);          //Blink the power LED twice at 200msec interval
  digitalWrite(PWR_LED, LOW);       //Turn off power LED
  Timer1.initialize(waterInterval);
  Timer1.attachInterrupt(checkWaterLevel);
  Serial.println("Initialized");
}

void loop() {
  checkPowerBtn();
  checkSelectBtn();
  // delay (LOOP_TIME);
}

void checkPowerBtn(){ 
  currentPwrBtnState = digitalRead(PWR_BTN);
  if (lastPwrBtnState == HIGH && currentPwrBtnState == LOW) { //power button pressed
    pressedTime = millis();
  } else if(lastPwrBtnState == LOW && currentPwrBtnState == HIGH) { //power button released
    releasedTime = millis();
    pressDuration = releasedTime - pressedTime;
    if (pressDuration > MIN_PRESS_TIME && currentPwrState == LOW) {
      if (waterVal > WATERLEVELLIMIT) {
        powerOn();
      }  //(waterVal > WATERLEVELLIMIT)
    } else if(pressDuration >= MIN_PRESS_TIME && currentPwrState == HIGH) { 
        if (pressDuration >= BYPASS_PRESS_TIME) {
          if (currentCompressorState == HIGH) {
            Serial.println("Manually disable compressor");
            digitalWrite(COMPRESSOR, LOW);
            currentCompressorState = LOW;
          } else {
              Serial.println("Manually enable compressor");
              digitalWrite(COMPRESSOR, HIGH);
              currentCompressorState = HIGH;
            }
        }  //(pressDuration >= BYPASS_PRESS_TIME)
        if (pressDuration < BYPASS_PRESS_TIME) {
          powerOff();
        }  //(pressDuration < BYPASS_PRESS_TIME)
      }  //else if (pressDuration >= MIN_PRESS_TIME && currentPwrState == HIGH)
    } //else if(lastPwrBtnState == LOW && currentPwrBtnState == HIGH)
  lastPwrBtnState = currentPwrBtnState; 
}  //checkPwrButton

void checkSelectBtn() {
  currentSelBtnState = digitalRead(SEL_BTN);
  if (currentPwrState == HIGH) {
    if (lastSelBtnState == HIGH && currentSelBtnState == LOW) {
      pressedTime = millis();
    } else if(lastSelBtnState == LOW && currentSelBtnState == HIGH) {
        releasedTime = millis();
        pressDuration = releasedTime - pressedTime;
        if (pressDuration  >= MIN_PRESS_TIME) {
          if (pressDuration >= BYPASS_PRESS_TIME) {
            thawCycle();
          } else {
            if (currentBlowerState == HIGH) {
              Serial.println("Manually turn off blower");
              digitalWrite(BLOWER_FAN, LOW);
              currentBlowerState = LOW;
            } else {
                Serial.println("Manually turn on blower");
                digitalWrite(BLOWER_FAN, HIGH);
                currentBlowerState = HIGH;
              }
            }
        } //(pressDuration >= BYPASS_PRESS_TIME)
      } //else if(lastSelBtnState == LOW && currentSelBtnState == HIGH)
  }  //(currentPwrState == HIGH)
  lastSelBtnState = currentSelBtnState;
}  //checkSelectBtn

void powerOn(){
  Serial.println("Powering on");
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Turn on water pump and wait 10 seconds");
  digitalWrite(WATER_PUMP, HIGH);
  blinkLED(PWR_LED,10,500);  //Delay using LED blink
  DPRINTLN("Turn on compressor fan and wait 1 second");
  digitalWrite(COMP_FAN, HIGH);
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Enable compressor and wait 6 seconds");
  digitalWrite(COMPRESSOR, HIGH);
  currentCompressorState = HIGH;
  blinkLED(PWR_LED,6,500);  //Delay using LED blink
  digitalWrite(BLOWER_FAN, HIGH);
  currentBlowerState = HIGH;
  digitalWrite(PWR_LED, HIGH);
  currentPwrState = HIGH;
  Serial.println("Power up complete");
}  //powerOn

void powerOff(){
  Serial.println("Powering off");
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Turn off compressor and delay 1 second");
  digitalWrite(COMPRESSOR, LOW);
  currentCompressorState = LOW;
  blinkLED(PWR_LED,1,500);  //Delay using LED blink
  DPRINTLN("Turn off blower fan and wait 3 seconds");
  digitalWrite(BLOWER_FAN, LOW);
  currentBlowerState = LOW;
  blinkLED(PWR_LED,3,500);  //Delay using LED blink
  digitalWrite(PWR_LED, HIGH);
  DPRINTLN("Turn off water pump and wait 10 seconds");
  digitalWrite(WATER_PUMP, LOW);
  blinkLED(PWR_LED,10,500);  //Delay using LED blink
  DPRINTLN("Turn off compressor fan");
  digitalWrite(COMP_FAN, LOW);
  blinkLED(PWR_LED,3,200);  //Delay using LED blink
  digitalWrite(WATER_LED, LOW);
  digitalWrite(PWR_LED, LOW);
  currentPwrState = LOW;
  Serial.println("Shutdown complete");
}  //powerOff

void thawCycle(){
  int tempBlowerState = currentBlowerState;
  Serial.println("Thaw cycle");
  if (tempBlowerState == HIGH){
    DPRINTLN("Turn off blower fan and wait 1 seconds");
    digitalWrite(BLOWER_FAN, LOW);
    currentBlowerState = LOW;
  }  //(currentBlowerState == HIGH)
  blinkLED(ICE_LED,1,500);  //Delay using LED blink
  DPRINT("Turn on bypass valve for ");
  DPRINTLN((THAW_TIME) / 1000);
  DPRINTLN(" seconds");
  digitalWrite(ICE_LED, HIGH);
  digitalWrite(BYPASS_VALVE, HIGH);
  blinkLED(ICE_LED,20,500);  //Delay using LED blink
  // delay(THAW_TIME);
  DPRINTLN("Turn off bypass valve and wait for 1 second");
  digitalWrite(BYPASS_VALVE, LOW);
  blinkLED(ICE_LED,1,500);  //Delay using LED blink
  digitalWrite(ICE_LED, LOW);
  if (tempBlowerState == HIGH) {
    DPRINTLN("Turn on blower fan");
    digitalWrite(BLOWER_FAN, HIGH);
    currentBlowerState = HIGH;
  }  //(blowerState == HIGH)
}  //thawCycle

void checkWaterLevel(){
  Serial.println("Check water level");
  digitalWrite(WATER_PWR, HIGH);  //Turn on the water sensor
  waterVal = analogRead(WATER_SIGNAL);  //Read the analog value from the sensor
  digitalWrite(WATER_PWR, LOW);  //Turn off the water sensor 
  if (waterVal > WATERLEVELLIMIT ) {
    DPRINT("Water level good at ");
    DPRINTLN(waterVal);
    digitalWrite(WATER_LED, LOW);
  } else {
    DPRINT("Water level below threshold of ");
    DPRINTLN(WATERLEVELLIMIT);
    digitalWrite(WATER_LED, HIGH);
  }  //(waterVal > WATERLEVELLIMIT )
}  //checkWaterLevel

void blinkLED(int LEDPin,int reps, int blinkTime){  //reps = # of cycles to blink, blinkTime = on/off time in msec
  DPRINT("Blinking pin ");
  DPRINT(LEDPin, DEC);
  DPRINT(" for ");
  DPRINT(reps);
  DPRINT(" reps at ");
  DPRINT(blinkTime);
  DPRINTLN(" milliseconds interval");
  for (int x=1; x<=reps; x++){
    digitalWrite(LEDPin, LOW);
    delay(blinkTime);
    digitalWrite(LEDPin, HIGH);
    delay(blinkTime);
  }  //(int x=1; x<=reps; x++)
}  //blinkLED

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.