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:
S1307Address};
if (rtc.autoprobe(addressList, sizeof(addressList))) {
// Serial.print("Autoprobed ");
switch (rtc.getDevice()) {
case RTCx:
S1307:
// 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;
}
#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:

if (rtc.autoprobe(addressList, sizeof(addressList))) {
// Serial.print("Autoprobed ");
switch (rtc.getDevice()) {
case RTCx:

// 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: