I'm creating an Egg Incubator project which keeps freezing.
I had multiple qty of components, adaptors, arduino boards etc.. All combinations freezes within random durations (3min to 2 hours) It usually won't work more than 2 hours.
I even ended up creating 2 incubators trying to isolate the issue.
Project only works thanks to "Watchdog" code I have which keeps resetting arduino after set amount of time when it hangs. Before using watchdog; it would just stay like that without doing anything, after adding watchdog code, it resets when it hangs.
It gets the job done as is but it's so annıoying that it keeps resetting. I'm showing "up duration" on LCD screen, I've not seen more than 2 hours so far.
Used components:
- Arduino Nano Clone
- 5V Relay (Turn-on/off Bulp)
- MG 995 Servo (To turn eggs)
- BME280 Temprature and Humdity Sensor (I2C)
- DS3231 RTC (keep timing for hatch time and turn eggs every hour)
- 16x2 LCD (I2C) (to show Temp, Humidty, next turn and remaing hatch days)
The methods I tried so far;
- Using different set of power adaoptors, arduino boards and components..
- I have 2 of the same setup.
- On one of the setup; I use 1K resistors between 5V<->A4&A5 and 3.3V<->A4&A5. Apperantly it should help I2C comm.
- The other doesn't have 1K resistors and it seems it resets more often than the one I use resistors.
Diagram attached and code is below, any idea where I should look for?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include "RTClib.h"
#include "Time.h"
#include <avr/wdt.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define DS3231_I2C_ADDRESS 0x68
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
Servo eggTurnServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
// Define Target Temp and Humidity Levels
double tempThreshold = 37.7;
double humidThresold = 60;
double currentTemp = 0;
double currentHumid = 0;
const int relayBulpPin = 11;
boolean ledState= true;
const int servoPin= 9;
// Set up timer for 5 seconds before switch on/off lamp so it doesn't keep
// switching onn&off every second.
const unsigned long MinStateChangeDuration=5000;
unsigned long lastBulpChange = 5;
// Date variables to calculate next egg turn, up time and hatch time
DateTime nowTime;
DateTime nextTurn;
DateTime hatchTime;
DateTime bootTime;
time_t remainingTime;
time_t remainingDays;
time_t upTime;
// LCD lines
char line0[16];
char line1[16];
void setup() {
// WatchDog Library code for resetting if it hangs
wdt_disable();
wdt_enable(WDTO_4S);// 4 sec
digitalWrite(relayBulpPin,HIGH);
//initialize the LCD
lcd.begin();
lcd.backlight();
delay(100);
// Initialize the BME
unsigned status;
status = bme.begin(0x76);
if (!status) {
lcd.print("BME280 ERROR");
while (1) delay(10);
}
delay(100);
// Initialize RTC
if (! rtc.begin()) {
lcd.print("Couldn't find RTC!");
delay(5000);
}
delay(300);
pinMode(servoPin , OUTPUT);
pinMode(relayBulpPin , OUTPUT);
bootTime = rtc.now();
delay(300);
}
void loop() {
wdt_reset();
currentTemp = bme.readTemperature();
currentHumid = bme.readHumidity();
delay(500);
nowTime = rtc.now();
nextTurn = DateTime(nowTime.year(), nowTime.month(), nowTime.day(), nowTime.hour()+1, 0, 0);
hatchTime = DateTime(2021, 4, 1, 10, 0, 0);
remainingTime = nextTurn.unixtime()-nowTime.unixtime();
//remainingDays = hatchTime.unixtime()-nowTime.unixtime();
upTime = nowTime.unixtime() - bootTime.unixtime();
delay(500);
lcd.setCursor(0,0);
// Write Temp and Humidity
lcd.print(F("S:"));
lcd.print(currentTemp,1);
lcd.print(F("C - "));
lcd.print(F("N:"));
lcd.print(currentHumid,0);
lcd.print(F("%"));
// Write Date&Time
lcd.setCursor(0,1);
lcd.print(F("D:"));
if(remainingTime/60<10) lcd.print(F("0"));
lcd.print( remainingTime/60, DEC );
lcd.print(":");
if(remainingTime%60<10) lcd.print(F("0"));
lcd.print(remainingTime%60, DEC);
// Write UpTime
lcd.print(F("-UP:"));
if(upTime/60<10) lcd.print(F("0"));
lcd.print( upTime/60, DEC );
lcd.print(":");
if(upTime%60<10) lcd.print(F("0"));
lcd.print(upTime%60, DEC);
delay(500);
// Turn eggs if less than 5 seconds to next turn time.
if( remainingTime < 5 ){
turnServo();
}
// Switch on&off bulp checking 5 seconds buffer between changes and current temp
if (lastBulpChange<5) lastBulpChange++;
else {
lastBulpChange = 0;
if (currentTemp <= tempThreshold){
digitalWrite(relayBulpPin,LOW);
}
else
{
digitalWrite(relayBulpPin,HIGH);
}
}
}
// methods to turn eggs slowly.
void turnServo(){
wdt_disable();
// Turn off lamp temporarily
digitalWrite(relayBulpPin,HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("YUMURTALAR");
lcd.setCursor(0,1);
lcd.print("CEVIRILIYOR...");
delay(1000);
if(nextTurn.hour()%2 == 1) {
eggTurnServo.write(95);
eggTurnServo.attach(servoPin);
for(int i=95; i<=180;i++){
eggTurnServo.write(i);
delay(100);
}
eggTurnServo.detach();
//eggTurnServo2.detach();
}
else {
eggTurnServo.write(180);
eggTurnServo.attach(servoPin);
for(int i=180; i>=95;i--){
eggTurnServo.write(i);
delay(200);
}
eggTurnServo.detach();
}
wdt_enable(WDTO_4S);
}