Temperature and Timed Defrost Cycle: Advice for combining two programs

Happy Friday Everyone! I’m getting started on a little project and would hugely appreciate some advice!

Background:

The device is an HRV (heat recovery ventilator) that pulls fresh air into the building and exhausts stale air, but recovers the heat via an air-to-air heat exchanger. The plan is to use two Noctua NF-F12 PWM fans and an Arduino Uno Rev3 (ATmega328P). I was advised that an arduino would be perfect as the controller, and while I’ve done some front end development and used pre-programmed arduino’s for other projects, I’ve never written C++.

The Programs:

The first program is simple, adjusting fan speed based on temperature. Input from a DS18B20 thermometer adjusts the duty cycle for the 4-pin PWM Fans.

The second program is a bit more complex. It is a timed defrost cycle as follows:

Every 50 minutes, it checks the temperature reading of the DS18B20.
If the temp is >=23F, the first program continues and the timer starts again for 50 mins.
If the temp is <=23
F, a 10 minute cycle is triggered where:
Fan one turns off
Fan two goes to 100% duty cycle
A stepper motor moves 90 degrees (to control a baffle which recirculates warm air through the device).

After the 10 minute defrost cycle, the timer for 50 minutes begins again, and program 1 continues.

Combining Programs:

There is an existing project in the project hub that should meet the requirements for program 1. I would love advice on how to interject the timed defrost cycle (program 2) into the existing code below for program 1.

For program 2, I plan to adapt the automated irrigation valve from the project hub. It includes the servo library to trigger the stepper motor, and a timer cycle based on input from a pot, which I assume could be swapped for temperature input from the DS18B20 thermometer.

I should already have code from program 1 to change the duty cycles of the fans during this 10 minute defrost cycle.

Existing Code: (Please advise if this should be moved to the programming section, this is not my code, just what I was planning to adapt)

A few questions for adapting this code:

  1. Can I include all libraries for both code snippets, or could that potentially create conflicts?
  2. What is the best way to insert the timed defrost cycle into the “default” loop of temperature controlled fan speed?

Program 1 (adjust fan speed based on temperature):

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

const int buttonPin = 2;
const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;
const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency
const word TCNT1_TOP = 16000000 / (2 * PWM_FREQ_HZ);
const int SETTEMPINTERVAL = 30000;

int buttonState = 0;
void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(OC1B_PIN, OUTPUT);

  // PWM settings
  // Clear Timer1 control and count registers
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

  // Set Timer1 configuration
  // COM1A(1:0) = 0b10   (Output A clear rising/set falling)
  // COM1B(1:0) = 0b00   (Output B normal operation)
  // WGM(13:10) = 0b1010 (Phase correct PWM)
  // ICNC1      = 0b0    (Input capture noise canceler disabled)
  // ICES1      = 0b0    (Input capture edge select disabled)
  // CS(12:10)  = 0b001  (Input clock select = clock/1)

  TCCR1A |= (1 << COM1A1) | (1 << WGM11);
  TCCR1B |= (1 << WGM13) | (1 << CS10);
  ICR1 = TCNT1_TOP;

  // start serial port
  Serial.begin(9600);
  // Start up the temperature library
  sensors.begin();
}


void loop() {
  tempToPwmDuty();
  delay(SETTEMPINTERVAL);
}

void setPwmDuty(byte duty) {
  OCR1A = (word) (duty * TCNT1_TOP) / 100;
  Serial.println("Fan duty cycle");
  Serial.println(duty);
}

void tempToPwmDuty() {
  // Override temp sensor with button
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Serial.println("Button ON, overriding temp sensor");
    setPwmDuty(100);
    return;
  }

  sensors.requestTemperatures();
  int temp = sensors.getTempCByIndex(0);
  Serial.println("Setting temp via sensor");
  Serial.println(temp);
  switch (temp) {
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
      setPwmDuty(25);
      break;
    case 26:
      setPwmDuty(50);
      break;
    case 27:
      setPwmDuty(75);
    case 28:
      setPwmDuty(100);
      break;
    default:
      setPwmDuty(100);
  }
}

Program 2 (10 minute defrost cycle every 50 minutes:

#include <Servo.h>
Servo myservo;//servo object to control servo
int potPin = A0; // select the input pin for the potentiometer

int val = 0; // variable to store the value coming from the sensor
int timerdelay;

void setup() {
  myservo.attach(9);//Servo Connected to Pin 9
}

void loop() {
  val = analogRead(potPin);
  if (val < 100)
  {
timerdelay = 5 * 1000 * 60: //5 Minutes Delay
  }
  else if (val >= 100 && val <= 200)
  {
    timerdelay = 10 * 1000 * 60; //10 Minutes delay
  }
  else if (val > 200 && val <= 300)
  {
    timerdelay = 15 * 1000 * 60; //15 minutes Delay
  }
  else if (val > 300 && val <= 400)
  {
    timerdelay = 20 * 1000 * 60; //20 Minutes Delay
  }
  else
  {
    timerdelay = 0;
  }
  myservo.write(180);//Open the Valve
  delay(timerdelay);//Wait for specified delay
  myservo.write(0);//Close the Valve

}

If You would use Code tags, the upper left symbil in this window, You will impress mist helpers. Using the Autoformat function in IDE gives even more points.

To begin: How much of the hardware control is up and running?

Good job on getting your code posted with the code tags.

  1. What is the best way to insert the timed defrost cycle into the "default" loop of temperature controlled fan speed?
delay(timerdelay);

The timed defrost cycle code uses delay() and can not be combined with the temperature code in that form. Please review these tutorials about how to convert that code to a form which uses millis() based logical timers for timing and not delay().

Demonstration code for several things at the same time

Using millis() for timing. A beginners guide

Here is a link to a tutorial on combining two codes
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

cattledog:
The timed defrost cycle code uses delay() and can not be combined with the temperature code in that form. Please review these tutorials about how to convert that code to a form which uses millis() based logical timers for timing and not delay().

Perfect! Thank you so much for the quick feedback! This is exactly what I was hoping for…

Railroader:
If You would use Code tags, the upper left symbil in this window, You will impress mist helpers. Using the Autoformat function in IDE gives even more points.

To begin: How much of the hardware control is up and running?

Thanks for the tip! I knew there had to be a code tag, but it kept eluding me! I also auto formatted that code in IDE, so thanks!
There’s no hardware up and running relevant to this thread. I have the heat exchanger and am building the case from EPS foam and MDF. I plan to design the baffle system next while I continue to work on the controller code. Why do you ask? For debugging or is there a different platform you’d recommend for a controller?

I asked to know where You are in the project.
My advice is to divide the project into smaller parts. Start with controlling a fan and make sure You've got that correct, both hardware, power and code. Then add the next part, reading sensors, making sure both hardware and software is under control.

Any Arduino can be used I would say. As I only have used UNOs I say that it will do the job.

There are good tutorials to visit.

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