Hi all!
This is my first time EVER on Arduino or C. I already feel sorry for those reading the code below. Anyways... A friend of mine is having his b-day in about two weeks, so wanted to make him a gift for his dawgie to keep him warm the rest of the winter when he's in his cabin.
I need this to control the temperature, adjust the speed of a 12VDC PWM fan, a heater and run the lights at the hours the dog is awake. Problem is that my Arduino and components are still being shipped to me. Just want to advance as much guess-work as I can. I just need some advice
I bought an ""Arduino Pro Mini"", Atmega328P 8M -which I think it is a clone-. I already tried compiling this code to test but I'm getting errors one after the other and I'm pretty sure I did so many things wrong I shouldn't be allowed to use the IDE for the rest of my life.
If someone could give me some advice on how to set a timer for the lights -while not loosing all other functions in the process- would be so much appreciated. Just found out, after paying for the thing, that it doesn't support RTC? Meaning... I can't use it in any way to trigger a relay every some hours?
Here's the code I came up with so far, along with annotations on every line to what should do/what I think it does.
/*Libraries*/
#include <Wire.h>
#include <DHT.h>
#include <EEPROM.h>
#include <Adafruit_Sensor.h>
/*Definitions*/
const int DHT = A4; // DHT22 SENSOR
const int LightPin = 2; // RELAY1
const int FanPin = 4; // RELAY2
const int HeaterPin = 6; // RELAY3
const int AlarmPin = 8; // RELAY4
const int PWRLED = 9; // POWER LED
const int StatusLed = 12; // STATUS LED
const int FanPWM = 5; // FAN PWM
int LightStatus = HIGH; // DEFAULT VALUES AT STARTUP
int HeaterStatus = HIGH; //..
int FanStatus = HIGH; //..
int AlarmStatus = HIGH; //..
void setup() {
serial.begin(9600); //TO START I2C COMMUNICATION?
float t=0 h=0; //BLANK VALUES FOR TEMP/RH
dht.begin(); //START SENSOR
pinMode(LightPin,OUTPUT); //PIN SETUP
pinMode(FanPin,OUTPUT); //..
pinMode(HeaterPin,OUTPUT); //..
pinMode(AlarmPin,OUTPUT); //..
pinMode(PWRLED,OUTPUT); //..
pinMode(StatusLED,OUTPUT); //..
pinMode(FanPWM,OUTPUT); //..
digitalWrite(PwrLed,HIGH); //LED ON
DigitalWrite(LightPin,LOW); //POWER ON RELAY SELFTEST
DigitalWrite(LightPin,HIGH); //..
delay(300); //..
DigitalWrite(FanPin,LOW); //..
DigitalWrite(FanPin,HIGH); //..
delay(300); //..
DigitalWrite(HeaterPin,LOW); //..
DigitalWrite(HeaterPin,HIGH); //..
delay(300); //..
DigitalWrite(AlarmPin,LOW); //..
DigitalWrite(AlarmPin,HIGH); //..
}
void loop() {
currentMillis = millis(); //TIMING
temprh(); //CYCLE THROUGH SUBROUTINES
pwmsignal(); //..
lightvoid(); //..
}
void temprh() { //TEMP CONTROL
int readData = DHT.read22(DHT); //DHT READ
float t = DHT.temperature; //READ TEMP
float h = DHT.humidity; //READ RH
if (t<21 && (HeaterPin,HIGH) { //LOW TEMP TRIGGER
DigitalWrite(HeaterPin,LOW) //ACTIVATE HEATER RELAY
int HeaterStatus = LOW; //STORE NEW STATE
}
if (t>24) { //HIGH TEMP TRIGGER
DigitalWrite(HeaterPin,HIGH); //DEACTIVATE HEATER RELAY
FanStatus = LOW; //STORE NEW DESIRED FAN STATE
}
}
void pwmsignal() {
if FanStatus == HIGH { //CHECK DESIRED STATUS
DigitalWrite(FanPin,LOW); //TURN ON FAN RELAY***
AnalogWrite(FanPWM, 255); //MAX. PWM SPEED
float t = DHT.temperature; //READ TEMP AGAIN
while (t>24); //KEEP AT MAX. SPEED IF NOT BELOW 24C (will it keep reading the temp?)
delay(1500); //MAINTAIN SPEED FOR 1.5 SEC
AnalogWrite(FanPWM, 170); //DROP TO 65-70%
delay(2000); //MAINTAIN FOR 2 SECONDS
DigitalWrite(FanPin,HIGH); //TURN OFF FAN RELAY
AnalogWrite(FanPWM, 0); //DONT KNOW IF REALLY NEEDED.
int FanStatus = HIGH; //SAVE DESIRED STATUS
}
}
void lightvoid() {
if LightStatus == HIGH { //SAME STRUCTURE BUT NOW I THINK ARDUINO PRO MINI DOESNT
DigitalWrite(LightPin,LOW); //SUPPORT RTC SO I DON'T KNOW HOW TO SET A TIMER.
delay(1000);
int LightStatus = LOW;
}
if LightStatus == LOW {
DigitalWrite(LightPin,HIGH);
delay(1000);
int LightStatus = HIGH;
}
}
This is the way I'd join everything together:
Except for the PWM signal, that turns out I can't run directly off it either due to the frequency, but will jerry-rig some intermediary circuit on pin5.
Due to the memory size on the board, I guess I should compress the code as much as possible, amirite?
Thank you very much in advance