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 <=23F, 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:
- Can I include all libraries for both code snippets, or could that potentially create conflicts?
- 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
}