Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are free to modify the code as much as you want and share it with other people if you want. Try to add any improvements and bugs to the original git hub repository so that i can take a look as well, of course this is not a requirement.
- do not delete this comment section, as this will help the next reader link to the original post and original content. This is necessary to avoid any confusion later on. Happy tinkering!
- Youtube Channel: https://www.youtube.com/channel/UCtvpQinm9lOqgLaEp4WIo-w?
- Github Repository: https://github.com/manhoosbilli1/Arduino-Incubator
- Arduino-forum thread: https://forum.arduino.cc/index.php?topic=631158.0
- // Written by manhoosbilli1 aka kaka, public domain
- */
- #include <DHT.h>;
- #include <EEPROM.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <TimeLib.h>
- #include "RTClib.h"
- #include <JC_Button.h>
- #define DHTPIN 2 // what pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- #define btn_up 12
- #define btn_down 11
- #define btn_select 10
- #define lSw1 8
- #define lSw2 9
- #define m1 5
- #define m2 6
- #define relay 3
- #define buzzer A1
- #define deHumFan A3
- bool runDemo = false;
- bool showHatchDay = 0;
- bool needToShowTime = 0;
- bool updAlarm;
- bool turnOnce = false;
- bool humidityHigh = false;
- bool tempHigh = false;
- bool showLockDay = false;
- float setPointT = 37.5;
- int setPointH = 45;
- int turns = 0;
- int chk;
- int h;
- int lockDayH = 60;
- float t;
- boolean humidityLow = true;
- boolean isLockDay = false;
- boolean tempVeryHighMsg=false;
- boolean tempVeryHigh = false;
- boolean alarmOn = false;
- int currentPage = 0;
- bool startMotor = false;
- unsigned long previousMillis = 0;
- unsigned long previousMillis1 = 0;
- unsigned long previousMillis2 = 0;
- unsigned long previousMillis3 = 0;
- unsigned long previousMillis4 = 0;
- unsigned long rememberTurn;
- unsigned long currentMillis = 0;
- unsigned int tDuration = 5000;
- unsigned int turnInterval = 20 * 1000;
- unsigned int hatchYear;
- unsigned int hatchMonth;
- unsigned int hatchDay;
- unsigned int hatchHour;
- unsigned int lockYear;
- unsigned int lockMonth;
- unsigned int lockDay;
- unsigned int offTime;
- unsigned int onTime;
- unsigned int timer;
- int turnCounter = 0;
- int turnsLeft;
- RTC_DS3231 rtc;
- DHT dht(DHTPIN, DHTTYPE);
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- Button Up(btn_up);
- Button Select(btn_select);
- Button Down(btn_down);
- Button ls1(lSw1);
- Button ls2(lSw2);
- byte motorOn[8] = {
- 0x00,
- 0x00,
- 0x11,
- 0x1B,
- 0x15,
- 0x11,
- 0x11,
- 0x00
- };
- byte centigrade[8] = {
- 0x10,
- 0x06,
- 0x09,
- 0x08,
- 0x08,
- 0x09,
- 0x06,
- 0x00
- };
- byte highT[8] =
- { B00100,
- B01010,
- B01010,
- B01110,
- B01110,
- B11111,
- B11111,
- B01110
- }; //thermometer icon
- byte highH[8] =
- { B00100,
- B00100,
- B01010,
- B01010,
- B10001,
- B10001,
- B10001,
- B01110
- }; //drop icon
- byte percentage [8] = {
- 0x00,
- 0x00,
- 0x01,
- 0x0A,
- 0x04,
- 0x0A,
- 0x10,
- 0x00
- };
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- //buttons
- dht.begin();
- Up.begin();
- Down.begin();
- Select.begin();
- ls1.begin();
- ls2.begin();
- lcd.begin();
- //lcd
- lcd.backlight();
- lcd.createChar(0, motorOn);
- lcd.createChar(1, centigrade);
- lcd.createChar(2, highH);
- lcd.createChar(3, percentage);
- lcd.createChar(4, highT);
- lcd.home();
- EEPROM.get(10, hatchYear);
- EEPROM.get(14, hatchMonth);
- EEPROM.get(18, hatchDay);
- EEPROM.get(22, lockYear);
- EEPROM.get(26, lockMonth);
- EEPROM.get(30, lockDay);
- //rtc
- if (!rtc.begin())
- {
- lcd.setCursor(0, 0);
- lcd.print("Couldn't find... ");
- lcd.setCursor(0, 1);
- lcd.print("RTC! Restart! ");
- while (1)
- ;
- }
- if (rtc.lostPower())
- {
- lcd.setCursor(0, 0);
- lcd.print("RTC lost power ");
- lcd.setCursor(0, 1);
- lcd.println("Upload Code Again ");
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- //pinmodes and initialisation
- pinMode(m1, OUTPUT);
- pinMode(m2, OUTPUT);
- pinMode(relay, OUTPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(deHumFan, OUTPUT);
- /*
- lcd.setCursor(0, 0);
- lcd.print("Motor is ");
- lcd.setCursor(0, 1);
- lcd.print("Calibrating...");
- while (ls1.read() == LOW && ls2.read() == LOW) {
- digitalWrite(m1, HIGH);
- digitalWrite(m2, LOW);
- }
- lcd.setCursor(0, 0);
- lcd.print("Motor Is Now ");
- lcd.print("Calibrated ");
- digitalWrite(m1, LOW);
- digitalWrite(m2, LOW);
- */
- delay(2000);
- }
- enum states
- {STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT};
- int currentState = STATE_IDLE;
- void loop()
- {
- DateTime now = rtc.now();
- currentMillis = millis();
- Up.read();
- Down.read();
- Select.read();
- ls1.read();
- ls2.read();
- updateSensor(); //will update sensor every 2 minutes.
- menu(); //will take care of lcd interface
- Maintain(); //will maintain temp and humidity
- ifIsLockDay();
- if(alarmOn == true && Select.pressedFor(1000)){
- alarmOn = false;
- timer = millis();
- }
- if(alarmOn == false){
- if(currentMillis - timer >= 5000){
- alarmOn = true;
- }
- }
- }
- void turnEggFlag()
- {
- beepFor(200, 200);
- startMotor = true;
- }
- void turn_once()
- {
- if (turnOnce)
- {
- switch (currentState)
- {
- case STATE_IDLE:
- if (ls1.read() == false && ls2.read() == true)
- {
- digitalWrite(m1, HIGH);
- digitalWrite(m2, LOW);
- currentState = STATE_MOVING_RIGHT;
- }
- if (ls1.read() == true && ls2.read() == false)
- {
- digitalWrite(m1, LOW);
- digitalWrite(m2, HIGH);
- currentState = STATE_MOVING_LEFT;
- }
- break;
- case STATE_MOVING_RIGHT:
- if (ls1.read() == true && ls2.read() == false)
- {
- digitalWrite(m1, LOW);
- digitalWrite(m2, LOW);
- turnOnce = false;
- currentState = STATE_IDLE;
- break;
- }
- break;
- case STATE_MOVING_LEFT:
- if (ls1.read() == false && ls2.read() == true)
- {
- digitalWrite(m1, LOW);
- digitalWrite(m2, LOW);
- turnOnce = false;
- currentState = STATE_IDLE;
- break;
- }
- break;
- }
- }
- }
- void turn() {
- if (startMotor)
- {
- switch (currentState)
- {
- case STATE_IDLE:
- if (ls1.read() == false && ls2.read() == true)
- {
- digitalWrite(m1, HIGH);
- digitalWrite(m2, LOW);
- currentState = STATE_MOVING_RIGHT;
- }
- if (ls1.read() == true && ls2.read() == false)
- {
- digitalWrite(m1, LOW);
- digitalWrite(m2, HIGH);
- currentState = STATE_MOVING_LEFT;
- }
- break;
- case STATE_MOVING_RIGHT:
- if (ls1.read() == true && ls2.read() == false)
- {
- digitalWrite(m1, LOW);
- digitalWrite(m2, LOW);
- startMotor = false;
- currentState = STATE_IDLE;
- break;
- }
- break;
- case STATE_MOVING_LEFT:
- if (ls1.read() == false && ls2.read() == true)
- {
- digitalWrite(m1, LOW);
- digitalWrite(m2, LOW);
- startMotor = false;
- currentState = STATE_IDLE;
- break;
- }
- break;
- }
- }
- }
- void menu()
- {
- switch (currentPage) {
- case 0:
- //screen messages
- lcd.setCursor(0, 0);
- lcd.write(4); //temp symbol
- lcd.print(t);
- lcd.write(1); //centigrade
- lcd.print(" ");
- lcd.setCursor(11, 0);
- lcd.write(2); //hum symbol
- lcd.print(h);
- lcd.write(3); //percentage character
- if (isLockDay == true && tempVeryHighMsg == false) {
- lcd.setCursor(0, 1);
- lcd.print("No turns Please ");
- } else if (isLockDay == false && tempVeryHighMsg == false) {
- lcd.setCursor(0, 1);
- lcd.print("Turns left: ");
- lcd.print(turns);
- lcd.print("/");
- lcd.print("5 ");
- } else if (isLockDay == true && tempVeryHighMsg == true) {
- lcd.setCursor(0,1);
- lcd.print("TEMP VERY HIGH ");
- } else if (isLockDay == false && tempVeryHighMsg == true) {
- lcd.setCursor(0,1);
- lcd.print("TEMP VERY HIGH ");
- }
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage = 4;
- }
- if (Select.wasPressed()) {
- turns += 1;
- if (turns >= 5 && Select.wasPressed()) {
- turns = 0;
- }
- }
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print("Press Select To ");
- lcd.setCursor(0, 1);
- lcd.print("Calc HatchDay ");
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- calcLockDay();
- calcHatchDay();
- lcd.setCursor(0,0);
- lcd.print("Calculating ");
- lcd.setCursor(0,1);
- lcd.print("Hatch Day Now ");
- delay(2000);
- currentPage = 0;
- }
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print("Press Select To ");
- lcd.setCursor(0, 1);
- lcd.print("Show Hatch Day ");
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- /*beepFor(500, 500);
- turnOnce = true;
- */
- showHatchDay = true;
- if (showHatchDay)
- {
- previousMillis = millis();
- while ((millis() - previousMillis) <= 3000)
- {
- lcd.setCursor(0, 0);
- lcd.print(" Hatch Day ");
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.print(hatchYear);
- lcd.print("/");
- lcd.print(hatchMonth);
- lcd.print("/");
- lcd.print(hatchDay);
- lcd.print(" ");
- delay(50);
- }
- showHatchDay = false;
- }
- }
- break;
- case 3:
- lcd.setCursor(0, 0);
- lcd.print("Press Select To ");
- lcd.setCursor(0, 1);
- lcd.print("Show Lock day ");
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- showLockDay = true;
- if (showLockDay)
- {
- previousMillis = millis();
- while ((millis() - previousMillis) <= 3000)
- {
- lcd.setCursor(0, 0);
- lcd.print(" Lock Day ");
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.print(lockYear);
- lcd.print("/");
- lcd.print(lockMonth);
- lcd.print("/");
- lcd.print(lockDay);
- lcd.print(" ");
- delay(50);
- }
- showLockDay = false;
- }
- }
- break;
- case 4:
- lcd.setCursor(0, 0);
- lcd.print("Press Select ");
- lcd.setCursor(0, 1);
- lcd.print("To Show Time ");
- if (Up.wasPressed())
- {
- currentPage = 0;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- needToShowTime = true;
- if (needToShowTime)
- { //triggers a function which will print to lcd
- previousMillis3 = millis(); //and wait for said time
- while ((millis() - previousMillis3) <= tDuration) //show for 5 seconds
- {
- showTime();
- delay(50); //to slow down arduino
- }
- needToShowTime = false; //after we have shown the time for time we can go to normal
- }
- }
- break;
- }
- }
- void showTime()
- {
- DateTime now = rtc.now();
- lcd.setCursor(0, 0);
- lcd.print("Date: ");
- lcd.setCursor(7, 0);
- lcd.print(now.year(), DEC);
- lcd.print('/');
- lcd.print(now.month(), DEC);
- lcd.print('/');
- lcd.print(now.day(), DEC);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("Time: ");
- lcd.setCursor(7, 1);
- lcd.print("");
- lcd.print(now.hour(), DEC);
- lcd.print(':');
- lcd.print(now.minute(), DEC);
- lcd.print(':');
- lcd.print(now.second(), DEC);
- lcd.print(" ");
- }
- void updateSensor()
- {
- if (currentMillis - previousMillis1 > 2000)
- {
- h = dht.readHumidity();
- t = dht.readTemperature();
- if (t > setPointT)
- {
- tempHigh = true;
- }
- else
- {
- tempHigh = false;
- }
- if(t>=setPointT+2){
- tempVeryHigh = true;
- } else {
- tempVeryHigh = false;
- }
- if (h >= (setPointH))
- {
- humidityHigh = true;
- } else {
- humidityHigh = false;
- }
- if (h < (setPointH - 10)) {
- humidityLow = true;
- } else
- {
- humidityLow = false;
- }
- previousMillis1 = currentMillis;
- }
- }
- void Maintain()
- {
- if (tempHigh)
- {
- digitalWrite(relay, LOW);
- }
- else
- {
- digitalWrite(relay, HIGH);
- }
- if (humidityHigh)
- {
- digitalWrite(deHumFan, HIGH);
- }
- else
- {
- digitalWrite(deHumFan, LOW);
- }
- if(tempVeryHigh == true && alarmOn == true) {
- beepFor(100, 100);
- tempVeryHighMsg = true;
- } else {
- tempVeryHighMsg = false;
- }
- }
- void calcHatchDay()
- {
- DateTime now = rtc.now();
- DateTime future (now + TimeSpan(20, 12, 30, 6));
- hatchYear = future.year();
- hatchMonth = future.month();
- hatchDay = future.day();
- hatchHour = future.hour();
- EEPROM.update(0, hatchYear);
- EEPROM.update(14, hatchMonth);
- EEPROM.update(18, hatchDay);
- }
- void beepFor(int onTime, int OffTime) { //remember to type previousMillis = millis(); just before calling the function.
- if(alarmOn){
- digitalWrite(buzzer, HIGH);
- delay(onTime);
- digitalWrite(buzzer, LOW);
- delay(offTime);
- }
- }
- void calcLockDay() {
- DateTime now = rtc.now();
- DateTime future (now + TimeSpan(17, 12, 30, 6));
- lockYear = future.year();
- lockMonth = future.month();
- lockDay = future.day();
- EEPROM.update(22, lockYear);
- EEPROM.update(26, lockMonth);
- EEPROM.update(30, lockDay);
- }
- void ifIsLockDay() {
- DateTime now = rtc.now();
- if (now.day() == lockDay && now.month() == lockMonth) {
- isLockDay = true;
- setPointH = 60;
- }
- }
Add Comment
Please, Sign In to add comment