Hi everyone,
I'm fairly new to programming, and have this project I'm doing for my father. He asked me to control a chicken Incubator, but 16 of them. I decided to use the dht22 sensors being it reads temp. and humidity. To keep the internal temperature at a constant 100 Deg F, I will be using a 100w 120/220v light bulb in each of the 16 incubators. All 16 relays are controlled by a SainSmart 16ch relay board. Along with the climate control I also have a 24v Linear actuator in each incubator to tilt the egg trays at a 45 deg each way. This actuator tilts the tray to 45 deg every 1hour. This is where I need help, so far I can log temp and humidity from two sensors (the rest or in the mail) and the light bulbs are turning on and off according to set temps. The millis function is still a little hard to grasp, I've just saw my first arduino code or any type of code, just 3 weeks ago. Anyhow, here's my code so far. OH, thanks in advance
PS. Saintsmart lcd display is also going to be integrated, but later.
/* 16 channel Incubator
created/ modified apr 20 2012
by Roth Ly
Also bits of code has been used from "frostin" of Arduino forum:
http://arduino.cc/forum/index.php?topic=70742.0
This code is for a 16 channel chicken incubator, where a total of
16 seperate incubator will be controlled via one Arduino Mega.
16 Dht22 senor inputs will run in to digital I/O 23,25,27...37.
A 16 channel relay board from sain smart takes care of switching
the heating element(100watt bulb) needed in each of the 16 incubators to keep an
internal temp of 100F. Along side the sensor data and relay switching
there is also a code for turning a separate DPDT relay on and off to activate
a linear actuator every hour.
Modified Blink without Delay
Turns on and off a DPDT relay connected to a digital pin (off for 1hr, on for 1hr)
without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The Blink without Delay circuit:
* Relay pin connected at pin A15 and Ground, with a TIP102 tran and 1n4004 diode.
* This relay acts as a H-bridge to on/off a linear actuator every hour.
Blink without Delay
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
//***************************************************************************
// constants won't change. Used here to
// set pin numbers:
const int relayPin = A15; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 19000; // interval at which to blink (milliseconds)
#include <DHT.h>
#define DHTPIN1 23 // sets Dht22 sensor #1 at pin digital 23
#define DHTPIN2 25 // sets Dht22 sensor #2 at pin digital 25
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
#define bulbPIN1 22 // pin number for relay/heater #1
#define bulbPIN2 24 // pin number for relay/heater #2
#define TEMP_HIGH 29 //at what temp (C*) to turn off relay/heater
#define TEMP_LOW 28 //at what temp to turn on relay/heater
//#define HUMID_HIGH 60
//#define HUMID_LOW 20
float minTemp = 100;
float maxTemp = 0;
//float minHumid = 100;
//float maxHumid = 0;
int bulbValue = 0;
void setup() {
// set the digital pin as output:
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
Serial.println("Program Start");
dht1.begin();// starts sensor 1
dht2.begin(); // starts sensor 2
pinMode(bulbPIN1, OUTPUT); //sets bulbPin as OUTPUT
pinMode(bulbPIN2, OUTPUT);
}
void loop()
{
// READ DATA
float t1 = dht1.readTemperature();
float h1 = dht1.readHumidity();
//int temperatureF = (t1 * 9 / 5) +32.5; // converts to Farenhiet, not sure how to use properly yet
// DO THE MIN/MAX
minTemp = min(minTemp, t1);
maxTemp = max(maxTemp , t1);
// DISPLAT DATA
if (isnan(t1) || isnan(h1)){ // sensor reading check
Serial.println("sensor 1");
} else {
Serial.print("#1 Temp:");
Serial.print(t1);
Serial.println("*C");
Serial.print("#1 Humid:");
Serial.print(h1);
Serial.println("%\t");
// relay/heater
if (t1 > TEMP_HIGH) bulbValue = 1;
else if (t1 < TEMP_LOW) bulbValue = 0;
digitalWrite(bulbPIN1, bulbValue);
delay(2000); // DHT's need a delay between readings ...
}
//****************************************************
{
// READ DATA
// READ DATA
float t2 = dht2.readTemperature();
float h2 = dht2.readHumidity();
//int temperatureF = (t2 * 9 / 5) +32.5;
// DO THE MIN/MAX
minTemp = min(minTemp, t2);
maxTemp = max(maxTemp , t2);
// DISPLAT DATA
if (isnan(t2) || isnan(h2)){
Serial.println("sensor 2");
} else {
Serial.print("#2 Temp:");
Serial.print(t2);
Serial.println("*c");
Serial.print("#2 Humid:");
Serial.print(h2);
Serial.println("%\t");
// CROCKPOT
if (t2 > TEMP_HIGH) bulbValue = 1;
else if (t2 < TEMP_LOW) bulbValue = 0;
digitalWrite(bulbPIN2, bulbValue);
delay(2000); // DHT's need a delay between readings ...
}
//*****************************************************************
}
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(relayPin, ledState);
}
}