Arduino İncubator

ltore123

In the Brooder
Feb 15, 2017
13
0
15
#include <EEPROM.h>
#include <RTCx.h>
//library for comminication between ic2 rtc and lcd
#include <Wire.h>
// Lİbrary to reset Arduino if it lockss up
#include <avr/wdt.h>
// Library for comminication with LCD
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
// Library forDHT11 or DHT22
#include "DHT.h"

#define DHTPIN 3 // DHT22 PIN Digital 3
#define DHTTYPE DHT22 // We use DHT 22 Sensor

//LCD
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// 0x3F is the I2C bus address for an unmodified module

DHT dht(DHTPIN, DHTTYPE);


//////////////////////////////////////////

float at = 0; // Adjusted temperature (EEPROM)
float t; // Temperature
float t_calibre = 0; // temperature calibration (After factory Reset 0 ) (EEPROM) (-5.00 ... 0 .. +5.00 )
long tmesec = 0; // read tempreature every second
float t_max; // Maximum temprature
float h_max; // Maximum humidity
int ah = 0 ; // Adjusted value for humidity (EEPROM)
float h; // Humidity
float h_calibre = 0; // Humidity calibration
int motor_sec = 0; // How many seconds motor turns the eggs (EEPROM) (1 - 15 Seconds)
int motor_time = 0; // How often motor turns the eggs (EEPROM) (60 - 120 - 180 240 Minutes)
int rest_time = 0; // Daily resting for eggs (EEPROM) (0 - 60 - 120 Minutes)
int incub_time = 21; // Total incubation time (EEPROM) (1 - 45 Days)
long incub_time_start = 0; // Stating day of incubation Unixtimestamp (EEPROM)
long days_left = 0; // Days left for incubation
int beep = 11; // Alarm buzzer pin
int next_motor_time = -1; // Calculates the next turning time. (if it is 14:45:55 makes it 14 )
int last_motor_time = -1; // Not to let the motor turn more than one time in an hour keeps the track of last turning time (if it is 14:45:55 makes it 14 )
long motorMillis = 0; // Turning time as miliseconds. Current ms > turning ms + (turning_sec * 1000) turning stops.
char mode = 'S'; // Menu S = Setup R = Working W = Clock adjustment M = Main Menu
int menu = 0; // Main Menu 0 => Settings menu 1=> Clock adjustment 2=> Factory reset 3=>Tempereture calibarasyon 4=>Humidity calibarasyon 5=>New incubation 6=>Exit
char adjust_settings = 'H'; // Menu helper values H = Erases starting date E = Does not erase Date

//////////////////////////////////////////
// Buttons
int downPin = 8; // Down button digital pin 8
int upPin = 7; // Up button digital pin 7
int okPin = 9; // OK button digital pin 9
int menuPin = 10; // Menu Button digital pin 10
int down = 0;
int up = 0;
int ok = 0;

//////////////////////////////////////////
// Relays
#define RELAY_TEMP 4 // Heater digital pin 4
#define RELAY_HUM 5 // Humidifier digital pin 5
#define RELAY_MOTOR 6 // Motor digital pin 6

//////////////////////////////////////////
// LED ler
int LED0 = 14; // TEMP Led Pin 14
int LED1 = 15; // Humidifier Led Pin 15
int LED2 = 16; // Motor Led Pin 16
const int buzzerPin = 11;
void setup() {
lcd.begin(20, 4);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.setCursor(1,0); //Start at character 3 on line 0
lcd.print("INCUBATORRAM");
delay(1000);
lcd.setCursor(2,1);
lcd.print(" By Levent Tore");
delay(1000);
lcd.setCursor(4,2);
lcd.print("GOOD");
delay(1000);
lcd.setCursor(2,3);
lcd.print("www.kulucka.ga");
delay(2000);
lcd.setBacklight(0x1);
lcd.clear();
// Locking up
wdt_enable(WDTO_4S); // Call the functions for lockup. İf it does not come to this line in 4 seconds Arduino resets itself
dht.begin(); // Start DHT22
delay(2000);
// Leds are output
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// Buzzer
pinMode(3, OUTPUT);
pinMode(buzzerPin, OUTPUT);

// Relays
pinMode(RELAY_TEMP, OUTPUT);
pinMode(RELAY_HUM, OUTPUT);
pinMode(RELAY_MOTOR, OUTPUT);
digitalWrite(RELAY_TEMP,HIGH);
digitalWrite(RELAY_HUM,HIGH);
digitalWrite(RELAY_MOTOR,HIGH);


// When connected to Pc for DEBUG mode
// Serial.begin(9600);

// Starting Wire protocol for I2C communication
Wire.begin();
////////////////////////////////////
//Starting clock
uint8_t addressList[] = {RTCx::MCP7941xAddress, RTCx::DS1307Address};
if (rtc.autoprobe(addressList, sizeof(addressList))) {
// Serial.print("Autoprobed ");
switch (rtc.getDevice()) {
case RTCx::DS1307:
// Serial.print("DS1307
break;
case RTCx::MCP7941x:
// Serial.print("MCP7941x
break;
default:
// Ooops. Must update this example!
// Serial.print("unknownce");
break;
}
// Serial.print(" at 0x");
// Serial.println(rtc.getAddress(), HEX);
}
else {
// Nothing found at any of the addresses listed.
// Serial.println("No RTCx found");
return;
}

// Enable the battery backup. This happens by default on the DS1307
// but needs to be enabled on the MCP7941x.
rtc.enableBatteryBackup();

// Ensure the oscillator is running.
rtc.startClock();

if (rtc.getDevice() == RTCx::MCP7941x) {
// Serial.print("Calibration
// Serial.println(rtc.getCalibration(), DEC);
// rtc.setCalibration(-127);
}

rtc.setSQW(RTCx::freq4096Hz);
////////////////////////////////////////////////////////////


mode = EEPROM.read(16); // Reading working mode from EEPROM
if(mode == 0)
{
// İf there is no mode going directly to General Settings Menu.
mode = 'S';
EEPROM.write(16, mode);
}

// İf the mode is working mode reading necessery data from EEPROM only one time.
if(mode == 'R')
{
//at = EEPROM.read(20);
EEPROM_readAnything(20, at);
ah = EEPROM.read(24);
motor_sec = EEPROM.read(28);
motor_time = EEPROM.read(32);
rest_time = EEPROM.read(36);
incub_time = EEPROM.read(40);
// Reading timestamp
EEPROM_readAnything(44, incub_time_start);
// Calibration
EEPROM_readAnything(54, t_calibre);
}

// Opening screen and clearing it
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin(20, 4);
lcd.clear();


// Buttons are input.
pinMode(downPin, INPUT_PULLUP);
pinMode(upPin, INPUT_PULLUP);
pinMode(okPin, INPUT_PULLUP);
pinMode(menuPin, INPUT_PULLUP);
}

// values to understand if holding the buutons pressed
long onTime = 0;
int lastReadingOk = LOW;
int hold = 0;
// values for time adjustment
int clock_menu = 1;
int temp_date = 0;
int temp_day = 0;
int temp_month = 0;
int temp_year = 0;
int temp_hour = 0;
int temp_minute = 0;

// Main Program Starts here

void loop() {


wdt_reset(); // Call the functions for lockup. İf it does not come to this line in 4 seconds Arduino resets itself
//For clock
struct RTCx::tm tm;
///////////////////////
// Adjusting Time
if(mode == 'W')
{
down = digitalRead(downPin);
up = digitalRead(upPin);
ok = digitalRead(okPin);

lcd.setBacklight(HIGH);

lcd.setCursor(0, 0);
lcd.print("Adjust ");

if(temp_date == 0)
{
rtc.readClock(tm);
temp_day = tm.tm_mday;
temp_month = tm.tm_mon+1;
temp_year = tm.tm_year+1900;
temp_hour = tm.tm_hour;
temp_minute = tm.tm_min;
temp_date = 1;
}

lcd.setCursor(1, 2);
if(temp_day < 10){lcd.print(" ");}
lcd.print(temp_day);
lcd.print("/");
if(temp_month < 10){lcd.print(" ");}
lcd.print(temp_month);
lcd.print("/");
lcd.print(temp_year);
lcd.print("-");
if(temp_hour < 10){lcd.print("0
lcd.print(temp_hour);
lcd.print(":");
if(temp_minute < 10){lcd.print("0
lcd.print(temp_minute);


lcd.setCursor(0, 3);
lcd.print("Accept;

if(clock_menu == 1)
{
//Adjust Day
lcd.setCursor(2, 2);
lcd.blink();
if(down == HIGH)
{
temp_day = temp_day - 1;
}
if(up == HIGH)
{
temp_day = temp_day + 1;
}
if(temp_day < 1)
{
temp_day = 1;
}
if(temp_day > 31)
{
temp_day = 31;
}
}
if(clock_menu == 2)
{
//Set the month
lcd.setCursor(5, 2);
lcd.blink();
if(down == HIGH)
{
temp_month = temp_month - 1;
}
if(up == HIGH)
{
temp_month = temp_month + 1;
}
if(temp_month < 1)
{
temp_month = 1;
}
if(temp_month > 12)
{
temp_month = 12;
}
}
if(clock_menu == 3)
{
//Adjust year
lcd.setCursor(10, 2);
lcd.blink();
if(down == HIGH)
{
temp_year = temp_year - 1;
}
if(up == HIGH)
{
temp_year = temp_year + 1;
}
if(temp_year < 2017)
{
temp_year = 2017;
}
if(temp_year > 2020)
{
temp_year = 2020;
}
}
if(clock_menu == 4)
{
//Saat Ayarla
lcd.setCursor(13, 2);
lcd.blink();
if(down == HIGH)
{
temp_hour = temp_hour - 1;
}
if(up == HIGH)
{
temp_hour = temp_hour + 1;
}
if(temp_hour < 0)
{
temp_hour = 0;
}
if(temp_hour > 23)
{
temp_hour = 23;
}
}
if(clock_menu == 5)
{
//Adjust minutes
lcd.setCursor(16, 2);
lcd.blink();
if(down == HIGH)
{
temp_minute = temp_minute - 1;
}
if(up == HIGH)
{
temp_minute = temp_minute + 1;
}
if(temp_minute < 0)
{
temp_minute = 59;
}
if(temp_minute > 59)
{
temp_minute = 0;
}
}

if(ok != HIGH)
{

clock_menu ++;
if(clock_menu > 5)
{
if(ok != HIGH)
{
// Clock adustment

tm.tm_min = temp_minute;
tm.tm_hour = temp_hour;
tm.tm_wday = 0;
tm.tm_mday = temp_day;
tm.tm_mon = temp_month-1;
tm.tm_year = temp_year-2017;

rtc.setClock(&tm);
clock_menu = 1;
temp_date = 0;
mode = 'R';
lcd.clear();
lcd.noBlink();
}
}
}
delay(100);
}

////////////////////////////
// Main Menu
if(mode == 'A')
{
// İf Menu is selected Turn off heater and humidifier
if(digitalRead(RELAY_TEMP)!=HIGH)
{
digitalWrite(RELAY_TEMP,HIGH);
delay(50);
digitalWrite(LED0,LOW);
}
if(digitalRead(RELAY_HUM)!=HIGH)
{
digitalWrite(RELAY_HUM,HIGH);
delay(50);
digitalWrite(LED1,LOW);
}

// Menu 0 -> Settings
// Menu 1 -> Clock
// Menu 2 -> Reset
// Menu 3 -> Temperature Calibration
// Menu 4 -> Humidity Calibration
// Menu 5 -> New incubation
// Menu 6 -> Exit
down = digitalRead(downPin);
up = digitalRead(upPin);
ok = digitalRead(okPin);
lcd.setCursor(0, 0);
lcd.print("SELECT ");
if(down == HIGH)
{
menu = menu - 1;
}
if(up == HIGH)
{
menu = menu + 1;
}
if(menu < 0){menu = 6;}
if(menu > 6){menu = 0;}

if(menu == 0)
{
lcd.setCursor(0, 1);
lcd.print("Settings");
}
if(menu == 1)
{
lcd.setCursor(0, 1);
lcd.print("Adjust Time");
}
if(menu == 2)
{
lcd.setCursor(0, 1);
lcd.print("Factoryings ");
}
if(menu == 3)
{
lcd.setCursor(0, 1);
lcd.print("Tempreturebration ");
}
if(menu == 4)
{
lcd.setCursor(0, 1);
lcd.print("Humiditybration ");
}
if(menu == 5)
{
lcd.setCursor(0, 1);
lcd.print("Newbation");
}
if(menu == 6)
{
lcd.setCursor(0, 1);
lcd.print("Exit ");
}
if(ok == LOW)
{
lcd.clear();
if(menu == 0)
{
adjust_settings = 'E';
mode = 'S';
}
if(menu == 1)
{mode = 'W';}
if(menu == 2)
{
/////////////
// Eppromu sil
mode = 'S';
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
}
if(menu == 3)
{
// Temperature Calibration
mode = 'H';
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperaturebration");

}
if(menu == 4)
{
// Humidity Calibration
mode = 'G';
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humiditybration");

}
if(menu == 5)
{
// Enter New İncubation
mode = 'S';
adjust_settings = 'H';
}

if(menu == 6)
{
// Continue to work
mode = 'R';
}
menu = 0;
}
delay(150);
}

///////////////////////////////
// Temperature Sensor Calibration + -
if(mode == 'H')
{
down = digitalRead(downPin);
up = digitalRead(upPin);
ok = digitalRead(okPin);

Serial.println(down);
if(down == LOW)
{
t_calibre = t_calibre + 0.1;
}
if(up == LOW)
{
t_calibre = t_calibre - 0.1;
}
if(t_calibre > 5) {t_calibre = +5;}
if(t_calibre < -5) {t_calibre = -5;}
lcd.setCursor(0, 1);
lcd.print(t_calibre);
lcd.print(" ");
if(ok == LOW)
{
// Temperature Calibration
EEPROM_writeAnything(54, t_calibre);
mode = 'A';
menu = 3;
delay(150);
}
delay(100);
}

// Humidity Sensor Calibration + -
if(mode == 'G')
{
down = digitalRead(downPin);
up = digitalRead(upPin);
ok = digitalRead(okPin);

Serial.println(down);
if(down == LOW)
{
h_calibre = h_calibre + 0.1;
}
if(up == LOW)
{
h_calibre = h_calibre - 0.1;
}
if(h_calibre > 10) {h_calibre = +10;}
if(h_calibre < -10) {h_calibre = -10;}
lcd.setCursor(0, 1);
lcd.print(h_calibre);
lcd.print(" ");
if(ok == LOW)
{
// Humidity calibration
EEPROM_writeAnything(64, h_calibre);
mode = 'A';
menu = 4;
delay(150);
}
delay(100);
}


// General Settings Menu
if(mode == 'S')
{
lcd.setCursor(0, 0);
lcd.print("GENERALINGS");
down = digitalRead(downPin);
up = digitalRead(upPin);
ok = digitalRead(okPin);
/////////////////
// Temperature Settings
if(menu == 0)
{
if(at == 0) { at = 37,5;};
lcd.setCursor(0, 1);
lcd.print("TEMPERATURE
if(down == HIGH)
{
at = at - 0.1;
}
if(up == HIGH)
{
at = at + 0.1;
}
if(at <= 25)
{
at = 25;
}
if(at > 41)
{
at = 41;
}
if(ok != HIGH)
{
while(ok != HIGH)
{
ok = digitalRead(okPin);
}
menu = 1;
lcd.clear();
}
lcd.print(at);
lcd.print(" C");
lcd.setCursor(0, 4);
lcd.print("OKNext");
}
// Humidity Settings
if(menu == 1)
{
lcd.setCursor(0, 1);
lcd.print("HUMIDITY;
if(ah == 0) { ah = 50;};

if(down == HIGH)
{
ah = ah - 1;
}
if(up == HIGH)
{
ah = ah + 1;
}
if(h <= 40)
{
ah = 40;
}
if(ah > 85)
{
ah = 85;
}
lcd.print(ah);
lcd.print(" %");
lcd.setCursor(0, 4);
lcd.print("OKNEXT");
if(ok != HIGH)
{
while(ok != HIGH)
{
ok = digitalRead(okPin);
}
menu = 2;
lcd.clear();
}
}
// Motor Settings
if(menu == 2)
{
if(motor_sec == 0) {motor_sec = 10;};
lcd.setCursor(0, 1);
lcd.print("Motoring seconds : ");
lcd.setCursor(0, 2);
lcd.print("Turninig: ");
lcd.setCursor(0, 4);
lcd.print("OKNEXT");
lcd.setCursor(17, 1);
lcd.print(motor_sec);
lcd.print(" ");
lcd.setCursor(14, 1);
lcd.blink();

if(down == HIGH)
{
motor_sec = motor_sec - 1;
}
if(up == HIGH)
{
motor_sec = motor_sec + 1;
}
if(motor_sec <= 1)
{
motor_sec = 1;
}
if(motor_sec > 15)
{
motor_sec = 15;
}

if(ok != HIGH)
{
while(ok != HIGH)
{
ok = digitalRead(okPin);
lcd.noBlink();
}
menu = 3;
lcd.clear();
}
}
// MOTOR TIME
if(menu == 3)
{
if(motor_time == 0) {motor_time = 60;};
lcd.setCursor(0, 1);
lcd.print("Turning Sec : ");
lcd.setCursor(0, 2);
lcd.print("Motor: ");
lcd.setCursor(17, 1);
lcd.print(motor_sec);
lcd.print(" ");
lcd.setCursor(16, 2);
if(motor_time == 60) {lcd.setCursor(17, 2);};
lcd.print(motor_time);
lcd.setCursor(0, 4);
lcd.print("OKNEXT");

lcd.setCursor(15, 2);
lcd.blink();

if(down == HIGH)
{
motor_time = motor_time - 60;
}
if(up == HIGH)
{
motor_time = motor_time + 60;
}
if(motor_time <= 60)
{
motor_time = 60;
}
if(motor_time > 240)
{
motor_time = 240;
}
if(ok != HIGH)
{
while(ok != HIGH)
{
ok = digitalRead(okPin);
lcd.noBlink();
}
menu = 4;
// Adjust settings for egg turning
next_motor_time = -1;
last_motor_time = -1;
lcd.clear();
}

}
// Eggs Resting Time
if(menu == 4)
{
lcd.setCursor(0, 1);
lcd.print("Resting minute.: ");

lcd.setCursor(10, 2);
if(rest_time < 70) {lcd.print(" "); lcd.setCursor(11, 2);};

lcd.print(rest_time);

if(down == HIGH)
{
rest_time = rest_time - 60;
}
if(up == HIGH)
{
rest_time = rest_time + 60;
}
if(rest_time <= 0)
{
rest_time = 0;
}
if(rest_time > 120)
{
rest_time = 120;
}

if(ok != HIGH)
{
while(ok != HIGH)
{
ok = digitalRead(okPin);
lcd.noBlink();
}
menu = 5;
lcd.clear();
}

}
if(menu == 5)
{
lcd.setCursor(0, 1);
lcd.print("İncubation days: ");

lcd.setCursor(10, 2);
if(incub_time < 10) {lcd.print(" "); lcd.setCursor(11, 2);};

lcd.print(incub_time);

lcd.setCursor(0, 3);
lcd.print("OKNEXT");

if(down == HIGH)
{
incub_time = incub_time - 1;
}
if(up == HIGH)
{
incub_time = incub_time + 1;
}
if(incub_time <= 1)
{
incub_time = 1;
}
if(incub_time > 45)
{
incub_time = 45;
}
if(ok != HIGH)
{
if(ok != HIGH)
{
while(ok != HIGH)
{
ok = digitalRead(okPin);
lcd.noBlink();
}
mode = 'R';
menu = 0;
lcd.clear();

EEPROM.write(16, mode);
//Temperature
EEPROM_writeAnything(20, t);
// Humidity
EEPROM.write(24, h);
// Motor turning
EEPROM.write(28, motor_sec);
// Motor turning time
EEPROM.write(32, motor_time);
// Eggs resting
EEPROM.write(36, rest_time);
// İncubation Period
EEPROM.write(40, incub_time);
// İncubataion ending Time
rtc.readClock(tm);
RTCx::time_t t = RTCx::mktime(&tm);

if(adjust_settings == 'E')
{

}
if(adjust_settings == 'H')
{
// Writing current time as starting time. Unixtimestamp

EEPROM_writeAnything(44, t);
}
}
}
}
delay(100);
}
//////////////////
// Working Menu
if(mode == 'R')
{


up = digitalRead(upPin);
// İf up button is pressed turns the eggs
while(up != HIGH)
{
if(digitalRead(RELAY_MOTOR)!=LOW)
{
digitalWrite(RELAY_MOTOR,LOW);
digitalWrite(LED2,HIGH);
}
up = digitalRead(upPin);
// Locking up
wdt_reset(); // Call the functions for lock up. İf it does not come to this line in 4 seconds Arduino resets itself
}

// Button
ok = digitalRead(okPin);

// Heater Control
lcd.setCursor(0, 0);
lcd.print("A
lcd.setCursor(1, 0);
lcd.print(at);
lcd.setCursor(0, 1);
lcd.print("A
lcd.print(ah);
lcd.setCursor(0, 2);
lcd.print("D
lcd.setCursor(2, 2);
lcd.print(incub_time);
/////////////////
// Check temperature and humidity every 2 seconds
//
if(tmesec != tm.tm_sec)
{
t = dht.readTemperature();
t = t + t_calibre;
h = dht.readHumidity();
h = h + h_calibre;
tmesec = tm.tm_sec;
}
/////////////////////////
//buzzer alarm
if (h >= 85 <= 35) {

tone(buzzerPin, 100);


delay(100);

noTone(buzzerPin);


delay(100);


}

else {

noTone(buzzerPin);


}
/////////////////////////
if (t >= 43 <= 30) {

tone(buzzerPin, 100);


delay(100);

noTone(buzzerPin);


delay(100);


}

else {

noTone(buzzerPin);


}
// Turn Off Humidifier
if(ah < h)
{
// Turn Off Humidifier
digitalWrite(RELAY_HUM,HIGH);
delay(20);
digitalWrite(LED1,LOW);
// Fark cok ise nem alma icin bir relay ayarlanabilir.
lcd.setCursor(19, 1);
lcd.print(" ");
}
// Turn Off Heater
if(at < t)
{
// Turn Off Heater
digitalWrite(RELAY_TEMP,HIGH);
delay(50);
digitalWrite(LED0,LOW);
lcd.setCursor(19, 0);
lcd.print(" ");
}
/////////////////////////////////////////////
// Checking DHT22
if (isnan(h) || isnan(t))
{
// Humidity Sensor not working
lcd.clear();
lcd.setCursor(5, 3);
lcd.print("ERROR

if (isnan(h))
{
lcd.print(" HUMIDITY");
}
if (isnan(t))
{
lcd.print(" TEMPERATURE");
}
// İf it gives error tun off the heater and humidifier
if(digitalRead(RELAY_TEMP)!=HIGH)
{
digitalWrite(RELAY_TEMP,HIGH);
delay(50);
}
if(digitalRead(RELAY_HUM)!=HIGH)
{
digitalWrite(RELAY_HUM,HIGH);
delay(50);
}
// Isı sensoru hatalı oldugu icin Reset atsin diye 5 SN delay veriyoruz
delay(5000);
}
else
{
lcd.setCursor(12, 3);
lcd.print(" ");

lcd.setCursor(7, 0);
lcd.print(t, 2);

if(t > t_max)
{
t_max = t;
}
// Maximum Tempereture
lcd.setCursor(13, 0);
lcd.print("M
lcd.setCursor(14, 0);
lcd.print(t_max, 2);
if(h > h_max)
{
h_max = h;
}
// Write Maximum humidity
lcd.setCursor(13, 1);
lcd.print("M
lcd.setCursor(14, 1);
lcd.print(h_max, 2);


lcd.setCursor(4, 1);
lcd.print("Hum
lcd.print(h);


// Calculate days left
rtc.readClock(tm);
RTCx::time_t t = RTCx::mktime(&tm);
///////////////////////////////////////
// Subtract todays date from last day
days_left = t - incub_time_start;
// Calculating days left
days_left = days_left / 60 / 60 / 24;
days_left = incub_time - days_left;

//Serial.println(incub_time_start);

lcd.setCursor(5, 2);
lcd.print(days_left);
lcd.setCursor(7, 2);
lcd.print(" Day");

rtc.readClock(tm);
lcd.setCursor(1, 3);
printTm(Serial, &tm);


lcd.setCursor(12, 2);
lcd.print("Turn
if(next_motor_time < 10 && next_motor_time > 0){lcd.print("0
if(next_motor_time > -1)
{
lcd.print(next_motor_time);
}
delay(100);
}
/////////////////
// Turn off the motor

if (millis() - motorMillis >= motor_sec*1000 && motorMillis>0)
{
digitalWrite(RELAY_MOTOR,HIGH);
delay(50);
digitalWrite(LED2,LOW);
motorMillis = 0;
}
if(tm.tm_sec == 30)
{
////////////////////////////////////////
// EGG TURNİNG
// motor_time 60->1 120->2 180->3 240->4

//İf turnıng the eggs is not setup
if(next_motor_time == -1)
{
// Turn the eggs after machine starts up
next_motor_time = tm.tm_hour;
}

if(tm.tm_hour == next_motor_time && last_motor_time != next_motor_time or ok != HIGH)
{
// Turn the eggs
//Serial.print(digitalRead(RELAY_MOTOR));

digitalWrite(RELAY_MOTOR,LOW);
delay(50);
digitalWrite(LED2,HIGH);
motorMillis = millis();
if(motor_time == 60)
{next_motor_time = tm.tm_hour+1;}
if(motor_time == 120)
{next_motor_time = tm.tm_hour+2;}
if(motor_time == 180)
{next_motor_time = tm.tm_hour+3;}
if(motor_time == 240)
{next_motor_time = tm.tm_hour+4;}
// Bir Daha Bu saatte Donmesin
last_motor_time = tm.tm_hour;
if(next_motor_time > 23) {next_motor_time = next_motor_time-24;}
}
}

if(tm.tm_sec == 15 or tm.tm_sec == 45)
{
///////////////
// Eggs Resting
if((tm.tm_hour == 12 && rest_time > 0) or (tm.tm_hour == 13 && rest_time > 60))
{
// TURN OFF HEATER and HUMIDIFIER
if(digitalRead(RELAY_TEMP)!=HIGH)
{
digitalWrite(RELAY_TEMP,HIGH);
delay(50);
}
if(digitalRead(RELAY_HUM)!=HIGH)
{
digitalWrite(RELAY_HUM,HIGH);
delay(50);
}
lcd.setCursor(19, 0);
lcd.print("S
}
else
{
// Heater Control
if(at > t)
{
// Turn on the Heater
digitalWrite(RELAY_TEMP,LOW);
delay(50);
digitalWrite(LED0,HIGH);
delay(50);
lcd.setCursor(19, 0);
lcd.print("W
}
//////////////////////////////////////////
// Humidity Control
// Humidity is Low
if(ah > h)
{
// Turn On the Humidifier
digitalWrite(RELAY_HUM,LOW);
digitalWrite(LED1,HIGH);
lcd.setCursor(19, 1);
lcd.print("W
}
}
}



// to go back to setup
// to go to main menu
menu = digitalRead(menuPin);
if (menu == LOW && lastReadingOk == HIGH) {
// Serial.println("bas 1");
onTime = millis();
}
//held
if (menu == LOW && lastReadingOk == LOW) {
if ((millis() - onTime) > 500) {
hold = 1;
if ((millis() - onTime) > 500) {
hold = 2;
}
}
}
if (menu == HIGH && lastReadingOk == LOW)
{
onTime = 0;
if (hold == 1) {
hold = 0;
mode = 'A';
lcd.clear();
}
if (hold == 2) {
hold = 0;
mode = 'A';
lcd.clear();
}
}
lastReadingOk = menu;
delay(20);
}
}

// print date and time on LCD screen
void printTm(Stream &str, struct RTCx::tm *tm)
{
lcd.setCursor(0, 3);

if(tm->tm_mday<10){lcd.print('0
lcd.print(tm->tm_mday
lcd.print('/');
if(tm->tm_mon<10){lcd.print('0
lcd.print(tm->tm_mon1);
lcd.print('/');
lcd.print(tm->tm_year1900);

lcd.print(' ');
lcd.setCursor(15, 3);
if(tm->tm_hour<10){lcd.print('0
lcd.print(tm->tm_hour
lcd.print(':');
if(tm->tm_min<10){lcd.print('0
lcd.print(tm->tm_min

}

// To write or read long values from EEPROM
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
 
Last edited:
incubator-schema.jpg
IMG_20170708_221615.jpg
incubator-schema.jpg

İt is not that hard İt has 4 buttons and a menu
button 1 to get the menu on the screen (red Button)
button 2 for OK or accept (green Button)
button 3 Down button to browse the menu downwards
botton 4 Up button to browse the menu upwards
You can adjust the temperature, humidity, how often motor turns the eggs (60, 120 or 240 minutes select choose of them) and how long (seconds) the motor turns
İt has clock you can adjust the date and time
on the screen first line left side desired temperature in the middle current temperature on the right side maximum temperature so far
on the screen second line left side desired humidity in the middle current humidity on the right side maximum humidity since the beginning
on the screen third line left side total incubation days e.g. chickens 21 other birds more or less than that in the middle of third line number of days left for the incubation on the right side of third line time of the motor will turn the eggs next time
on the screen fourth line left side shows the current date on the right side shows the current time
on the very far right side of the first and second line the letter W (Working) shows up on the screen when the heater and humidifier kicks in it disapears when the heater or humidifier turns off

Meterials
Arduino Uno
DHT22 sensor
4 Channel Relay
RTC DS1307 and PC battery
Humidifier
2 light bulbs
3 Leds (I did not need them. you do not have to use them )
12V dc Motor
PC buzzer
Wire hanger
2 plastic vegetable baskets
2 Fans 5V (They are always on. One of them at the buttom the other one on the top )
PC Power supply
Used refrigerator (mine is small commercial beer refrigerator)
Use seperate power source for the motor
 
Last edited:
ATTENTİON
Sorry the code above is missing things I copied and pasted it but I think it does not fit in the posting area of the web page
I will put a link to my google drive ASP
 
Any code that works technically isn't a bad code.

I would suggest you look into using switch/case as its a easier way to control things like menus. My incubator has 26 basic options on the menu of which 16 can be changed by holding the edit button and then pressing up or down. After releasing the edit button if the info has changed and its important then its updated into the arduino epprom.
In switch case this is easier.

I have cut a lot of code out of the program I am using but hopefully there's enough posted to give you a idea of how switch case can be used (im running a hatcher and incubator using peltiers and a home made turner so there might be some references that look odd)

You can also do switch/case with in switch/case to make the menu as deep as you would like


byte temp_char[8] = {B01110, B01010, B01010, B01110, B01110, B11111, B11111, B01110,};
byte hum_char[8] = {B00100, B01110, B11111, B00100, B10001, B00100, B10001, B00100,};
byte bell_Char[8] = {B00100, B01110, B01110, B01110, B11111, B11111, B00100, B00000};
byte arrow_Char[8] = {B00000, B00000, B10000, B10000, B10111, B10011, B10101, B01000};
DS1307 clock;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address


void setup()
{
lcd.begin(20, 4);
lcd.createChar(1, temp_char);
lcd.createChar(2, hum_char);
lcd.createChar(3, bell_Char);
lcd.createChar(4, arrow_Char);

clock.begin();

sensor1.begin();
sensor2.begin();

pinMode (edit_input, INPUT_PULLUP);
pinMode (up_input, INPUT_PULLUP);
pinMode (down_input, INPUT_PULLUP);
pinMode (light_input, INPUT_PULLUP);
pinMode (safety_input, INPUT);
pinMode (r1, OUTPUT);
pinMode (r2, OUTPUT);
pinMode (r3, OUTPUT);
pinMode (r4, OUTPUT);
pinMode (8, OUTPUT);//peltier incubator
pinMode (11, OUTPUT);//peltier hatcher
pinMode (light_hatcher, OUTPUT);//light hatcher
pinMode (fans_hatcher, OUTPUT);//fans
Serial.begin(9600);
// load from memory test code..................


temp_setpoint = EEPROM.readFloat (0); //float
humidity_setpoint = EEPROM.readFloat (88);
}


void loop() {

if ((clock_update == 1) && (edit_button == 1)) {
clock.fillByYMD(set_year, set_month, set_day); //Jan 19,2013
clock.fillByHMS(set_hour, set_minute, 30); //15:28 30"
clock.fillDayOfWeek(SAT);//Saturday
clock.setTime();//write time to the RTC chip
clock_update = 0;
}

//write to memory code

if ((memory_update != 0) && (edit_button == 1)) {
switch (memory_update) {
case 1:
EEPROM.writeFloat(0, temp_setpoint);
break;
case 3:
EEPROM.writeFloat(88, humidity_setpoint);
break;

}
memory_update = 0;
}



//temp cal code

adj_humidity = humidity - cal_humidity;
adj_temp = temp - cal_temp;
hatch_adj_hum = humidity_two - hatch_cal_hum;
hatch_adj_temp = temp_two - hatch_cal_temp;
turner_postion = analogRead (A2);


//inputs
edit_button = digitalRead(edit_input);
up_button = digitalRead(up_input);
down_button = digitalRead(down_input);
light_button = digitalRead(light_input);
turner_safety = digitalRead(safety_input);



// temp read..............................................................................

if (temp_timer >= 1) {
humidity = sensor1.readHumidity();
// Read temperature as Fahrenheit
temp = sensor1.readTemperature(true);


//return to home screen......................................................
if (edit_button == 0) {
screen_timer = 0; //do not time in edit
}
if ((screen != 0) && (screen_timer >= 120)) {
screen = 0;
screen_timer = 0;
}//2 mins return to home screen

clock.getTime();
if (prev_clock_second != (clock.second)) {
temp_timer++;//slow call to sensors
screen_lock_timer++;
prev_clock_second = (clock.second);
}

if (prev_clock_minute != (clock.minute)) {
min_timer--;
prev_clock_minute = (clock.minute);
}

//change screens.................................................................
if (edit_button == 1) {
buttons(16);
}




//normal screens.............................................................
if (edit_button == 1) {
switch (screen) {
case 8:
CleanLcd();
lcd.setCursor(0, 0);
lcd.print ("set temp");
lcd.setCursor(0, 1);
lcd.print (" incubator ");
lcd.setCursor(0, 2);
lcd.print (temp_setpoint, 1);
lcd.setCursor(0, 3);
HoldToEditText();//function call to save memory
break;

case 9:
CleanLcd();
lcd.setCursor(0, 0);
lcd.print ("set humidity");
lcd.setCursor(0, 1);
lcd.print (" incubator ");
lcd.setCursor(0, 2);
lcd.print (humidity_setpoint, 1);
lcd.setCursor(0, 3);
HoldToEditText();
break;

case 18:
CleanLcd();
lcd.setCursor(0, 0);
lcd.print ("set minute");
lcd.setCursor(0, 1);
lcd.setCursor(0, 2);
lcd.print ("current minute: ");
lcd.print(clock.minute, DEC);
lcd.setCursor(0, 3);
HoldToEditText();
break;

case 19:
CleanLcd();
lcd.setCursor(0, 0);
lcd.print ("set hour");
lcd.setCursor(0, 1);
lcd.setCursor(0, 2);
lcd.print ("current hour: ");
lcd.print(clock.hour, DEC);
lcd.setCursor(0, 3);
HoldToEditText();
break;



}
}


//edit code..................................................................................
if (edit_button == 0) {
switch (screen) {
case 0:
nochange();//function call for screens that can not be editted
break;


case 8:
CleanLcd();
prev_temp_setpoint = temp_setpoint;

buttons(4);//function call for up/down buttons
lcd.setCursor(0, 0);
lcd.print ("change temp");
lcd.setCursor(0, 1);
lcd.print (temp_setpoint, 1);
lcd.setCursor(0, 2);
UpDownText();
lcd.setCursor(0, 3);
EditText();//function to save memory
if ( prev_temp_setpoint != temp_setpoint) {
memory_update = 1;//set bit to as something changed so update epprom
}
break;

case 9:
CleanLcd();
prev_humidity_setpoint = humidity_setpoint;
buttons(5);
lcd.setCursor(0, 0);
lcd.print ("change humidity");
lcd.setCursor(0, 1);
lcd.print (humidity_setpoint, 1);
lcd.setCursor(0, 2);
UpDownText();
lcd.setCursor(0, 3);
EditText();

if ( prev_humidity_setpoint != humidity_setpoint) {
memory_update = 3;
}
break;

case 18:
CleanLcd();
clock_update = 1;//update clock when leaving edit mode
buttons(11);
if (set_minute < 0) {
set_minute = 59;
}
if (set_minute > 59) {
set_minute = 0;
}

lcd.setCursor(0, 0);
lcd.print ("set minute");
lcd.setCursor(0, 1);
UpDownText();
lcd.setCursor(0, 2);
lcd.print ("enter minute: ");
if (set_minute < 10) {
lcd.print(" ");
}
lcd.print(set_minute);

lcd.setCursor(0, 3);
EditText();

break;

case 19:
CleanLcd();
clock_update = 1;
buttons(12);
if (set_hour < 0) {
set_hour = 23;
}
if (set_hour > 23) {
set_hour = 0;
}
lcd.setCursor(0, 0);
lcd.print ("set hour");
lcd.setCursor(0, 1);
UpDownText();
lcd.setCursor(0, 2);
lcd.print ("enter hour: ");
if (set_hour < 10) {
lcd.print(" ");
}
lcd.print(set_hour);
lcd.setCursor(0, 3);
EditText();
break;

}
}



}/* --(end main loop )-- */
void nochange() {
CleanLcd();
lcd.setCursor(0, 0);
lcd.print ("nothing can");
lcd.setCursor(0, 1);
lcd.print ("be changed");
lcd.setCursor(0, 2);
lcd.print ("on this screen");
lcd.setCursor(0, 3);
lcd.print ("info only");
}

void buttons(int type) {
if ((up_button == 0) && (prev_up_button == 1)) {
switch (type) {
case 4:
temp_setpoint = temp_setpoint + 0.1;
break;
case 5:
humidity_setpoint = humidity_setpoint + 0.1;
break;
case 11:
set_minute++;
break;
case 12:
set_hour++;
break;
case 16:
screen ++;
break;
}
prev_up_button = 0;
}
if (up_button == 1) {
prev_up_button = 1; //debounce
}

if ((down_button == 0) && (prev_down_button == 1)) {
switch (type) {
case 4:
temp_setpoint = temp_setpoint - 0.1;
break;
case 5:
humidity_setpoint = humidity_setpoint - 0.1;
break;
case 11:
set_minute--;
break;
case 12:
set_hour--;
break;
case 16:
screen --;
break;
}
prev_down_button = 0;
}
if (down_button == 1) {
prev_down_button = 1; //debounce
}
}

void CleanLcd() {
if (clear_oneshot == 0) {
lcd.clear();
clear_oneshot = 1;
}
}

void EditText() {
lcd.print ("release edit to set");
}

void UpDownText() {
lcd.print ("use up and down");
}

void HoldToEditText() {
lcd.print ("hold edit to change");
}
 
Meterials

Arduino Uno 3$
LCD screen 20x4 4$
DHT22 temp humidity sensor 2.5$
4 Channel relay 2$
RTC DS1307 clock 50 cent
12v dc motor free
24 volt nem nozülü 4$
4x push button 1$
2x light bulb 100w 25w and 2x light bulb sockets 2$
2x plastic vegetable basket free from local market
1x 5 volt dc fan 1x notebook fan
Commercial mini beverage fridge (front tempered glass)
1x pc power supply
2x Plastic dishes
5 feet electric wire

code link https://drive.google.com/open?id=0B0eWzIn-plF2SVNHR3BGTTBOY1E
 

New posts New threads Active threads

Back
Top Bottom