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
- */
- /*
- 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 <Arduino.h>
- #include <DHT.h>;
- #include <EEPROM.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <TimeLib.h>
- #include <TimeAlarms.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 = 60;
- int turns =0;
- //Variables
- int chk;
- int h;
- float t;
- 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 = (1 * 3600)* 1000;
- unsigned int hatchYear;
- unsigned int hatchMonth;
- unsigned int hatchDay;
- unsigned int hatchHour;
- unsigned int lockYear;
- unsigned int lockMonth;
- unsigned int lockDay;
- unsigned int hour_now;
- unsigned int minute_now;
- unsigned int second_now;
- unsigned int month_now;
- unsigned int year_now;
- unsigned int day_now;
- unsigned int offTime;
- unsigned int onTime;
- int turnCounter = 0;
- int turnsLeft;
- RTC_DS3231 rtc;
- DHT dht(DHTPIN, DHTTYPE);
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- AlarmId id;
- 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("Mortor 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);
- Alarm.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
- turn_once(); //will work only when called
- turn(); //will turn motor few times in a day
- Maintain(); //will maintain temp and humidity
- if((millis() - rememberTurn) > turnInterval){
- turnEggFlag();
- rememberTurn = millis();
- }
- }
- void turnEggFlag()
- {
- beepFor(200,200);
- turns +=1;
- startMotor = true;
- if(turns >= 4){
- turns = 0;
- }
- }
- 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:
- 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 symbole
- lcd.print(h);
- lcd.write(3); //percentage character
- lcd.setCursor(0,1);
- lcd.print("Turns left: ");
- lcd.print(turns);
- lcd.print("/");
- lcd.print("3 ");
- //show countdown till hatch day
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage = 5;
- }
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print("Press Select To ");
- lcd.setCursor(0, 1);
- lcd.print("Show HatchDay ");
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- calcHatchDay();
- 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(" ");
- Alarm.delay(50);
- }
- showHatchDay = false;
- }
- }
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print("Press Select To ");
- lcd.setCursor(0, 1);
- lcd.print("Turn Motor once ");
- if (Up.wasPressed())
- {
- currentPage += 1;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- beepFor(500, 500);
- turnOnce = true;
- }
- 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())
- {
- calcLockDay();
- 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);
- Alarm.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 += 1;
- }
- 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();
- Alarm.delay(50); //to slow down arduino
- }
- needToShowTime = false; //after we have shown the time for time we can go to normal
- }
- }
- break;
- case 5:
- lcd.setCursor(0, 0);
- lcd.print("Press Select To ");
- lcd.setCursor(0, 1);
- lcd.print("Toggle Light ");
- if (Up.wasPressed())
- {
- currentPage = 0;
- }
- if (Down.wasPressed())
- {
- currentPage -= 1;
- }
- if (Select.wasPressed())
- {
- digitalWrite(relay, !digitalRead(relay));
- }
- 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 (h > setPointH + 5)
- {
- humidityHigh = true;
- } else
- {
- humidityHigh = false;
- }
- previousMillis1 = currentMillis;
- }
- }
- void Maintain()
- {
- if (tempHigh)
- {
- digitalWrite(relay, LOW);
- }
- else
- {
- digitalWrite(relay, HIGH);
- }
- if (humidityHigh)
- {
- digitalWrite(deHumFan, HIGH);
- }
- else
- {
- digitalWrite(deHumFan, LOW);
- }
- }
- void calcHatchDay()
- {
- DateTime now = rtc.now();
- DateTime future (now + TimeSpan(21, 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.
- digitalWrite(buzzer, HIGH);
- Alarm.delay(onTime);
- digitalWrite(buzzer, LOW);
- Alarm.delay(offTime);
- }
- void calcLockDay() {
- DateTime now = rtc.now();
- DateTime future (now + TimeSpan(18, 12, 30, 6));
- lockYear = future.year();
- lockMonth = future.month();
- lockDay = future.day();
- EEPROM.update(22, lockYear);
- EEPROM.update(26, lockMonth);
- EEPROM.update(30, lockDay);
- }
Add Comment
Please, Sign In to add comment