Hi all,
First time poster here, please let me know if this is the wrong place for this topic!
The aim is to create an automated planted terrarium for a colony of tropical weaver ants . I would like the system to perform the following operations:
- Turn some LED strip lights on/off at 9am/9pm
- Run a fan for 5 minutes every hour, with a servo opening/closing a hatch at the beginning and end
- Control a heater to maintain temperatures in the range of 25-30C
- Display temperature and humidity on an LCD screen
I am very apprehensive of dealing with mains voltage (which the heater runs on). I have it triple insulated, but still want my code and circuited looked at by more experienced folks before I plug it in. I've invariably made mistakes as I am still learning- Any thoughts or assistance would be greatly appreciated.
LCD is wired as per example sketches
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
#include "DHT.h"
#include <Servo.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
Servo servo; // create servo object to control a servo
DHT dht;
RTC_DS1307 rtc;
unsigned long previousMillis = 0;
int fanState = LOW;
// --------CONSTANTS (won't change)---------------
const int fanPin = 7;
const int servoPin = 9;
const int heaterPin = 8;
const int lightPin = 10;
const int fanDuration = 300000; // number of millisecs that fan is on
const int interval = 3300000; // number of milisecs when the fan is off
#define maxtemp 29.0
#define mintemp 24.0
//------------ VARIABLES (will change)---------------------
byte targetfanState = HIGH; // this variable is necesary to help arduino monitor the status of LED
//the initial condition (t=0) can be either HIGH or LOW
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousfanMillis = 0; // will store last time the LED was updated
int pos = 0; // variable to store the servo position
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//========== THE SETUP ==============================
void setup() {
// put your setup code here, to run once:
Wire.begin();
pinMode(fanPin, OUTPUT);
pinMode(servoPin, OUTPUT);
pinMode(heaterPin, OUTPUT);
pinMode(lightPin, OUTPUT);
digitalWrite(fanPin, LOW);
digitalWrite(servoPin, LOW);
digitalWrite(heaterPin, LOW);
digitalWrite(lightPin, LOW);
servo.attach(3); // attaches the servo on pin 9 to the servo object
dht.setup(6);
Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.setCursor(0,1);
lcd.print("Hum: ");
}
//========== THE LOOP ==============================
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
if ((now.hour() >= 21) || (now.hour() < 9)){
digitalWrite(lightPin, LOW);
}
else digitalWrite (lightPin, HIGH);
float h = dht.getHumidity();
float t = dht.getTemperature();
//error check
if (isnan(h) || isnan(t)) {
lcd.setCursor(0,0);
lcd.print("sensorfail");
return;
}
if (t < mintemp){
digitalWrite(heaterPin, HIGH);
}
if (t > maxtemp){
digitalWrite(heaterPin, LOW);
}
lcd.setCursor(6,0);
lcd.print(t,2);
lcd.print(" *C ");
lcd.setCursor(5,1);
lcd.print(h,2);
lcd.println(" % ");
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
void updateTargetfan() {
if (targetfanState == LOW) { //if the fan is OFF
Serial.println("LOW");
if ((unsigned long) currentMillis - previousfanMillis >= interval) {
servo.write(180); //change depending on original position of servo
delay(2000);
targetfanState = HIGH;
digitalWrite(fanPin, targetfanState);
// and save the time when we made the change
previousfanMillis += interval;
// NOTE: The previous line could alternatively be
// previousLedMillis = currentMillis
// Adding on the interval is a better way to ensure that succesive periods are identical
}
}
else { // i.e. if onBoardLedState is HIGH (if the LED is ON)
Serial.println("HIGH");
if ((unsigned long) currentMillis - previousfanMillis >= fanDuration) {
targetfanState = LOW;
digitalWrite(fanPin, targetfanState);// and save the time when we made the change
delay(1000);
servo.write(0); //change depending on original position of servo
delay(2000);
previousfanMillis += fanDuration;
// NOTE: The previous line could alternatively be
// previousLedMillis = currentMillis
// Adding on the duration is a better way to ensure that succesive periods are identical
}
if (fanPin = HIGH){
lcd.setCursor(12,1);
lcd.print("F");}
if (heaterPin = HIGH){
lcd.setCursor(12,0);
lcd.print("H");}
}
delay(10000); }
}