Egg Incubator version 2

Having it set to turn for 5 seconds every 20 min seems to work good. The problem was either having the length of time to long and it froze up the memory or having the RTC module that wasn't needed with this sketch still plugged in and causing havoc. Hopefully it makes it through the night okay. Fingers crossed :).

Now to think of how to add features like a door light and a rack centering when the door is open. I also want to add info on the lcd screen to show the turner count down and when its turning.

I guess I spoke to soon. This morning the LCD was dim and the temp was froze so I reset the arduino and it came back bright again at around 34 degrees and quickly heated up to the 37.5. After a big sigh I reprogrammed the arduino nano with the clean macro F version I made that only controls temp and humidity. Now to wait 24 hours and see what it does.....

Could you please post a schematic or even a hand drawing showing how everything is connected to the arduino?

Do you have anything that runs at night that might be causing surges or interference on the power line - any large equipment in the same building?

Anita_Bath:
I guess I spoke to soon. This morning the LCD was dim and the temp was froze so I reset the arduino and it came back bright again at around 34 degrees and quickly heated up to the 37.5. After a big sigh I reprogrammed the arduino nano with the clean macro F version I made that only controls temp and humidity. Now to wait 24 hours and see what it does.....

Another worthwhile test would be to run the broken version again, either removing the wire controlling the motor so it doesn't turn or removing this line:

  digitalWrite(turner, Turner_State);

As to David_2018's question, I recall another incubator thread some years ago where the cause of the locking up was a nearby old refrigerator. A strategically placed cap solved the issue.

That said, if it was working without the turning enabled, I don't think it's so likely to be an external cause. Time will tell.

I would also be inclined to increase the frequency of turning a lot, so you can get it tested faster.

why don't you replace the call to millis() (presumably just one) with a routine that returns an unsigned long incremented by 100 ( or some other large) number. A 24 hour test should complete in ~15 minutes

gcjr:
why don't you replace the call to millis() (presumably just one) with a routine that returns an unsigned long incremented by 100 ( or some other large) number. A 24 hour test should complete in ~15 minutes

Did you mean "multiplied by 100"?

gcjr:
why don't you replace the call to millis() (presumably just one) with a routine that returns an unsigned long incremented by 100 ( or some other large) number. A 24 hour test should complete in ~15 minutes

Could you please show me how to do this? Would save me a lot of time :slight_smile:

The version I made with Macro F from the code I found online that only used 16 percent of the memory failed after 5 hours. The screen stayed bright but was blank and after resetting it the temp was down to 34 again.... these poor eggs haha. I am as lost as a midget in a corn field on this now.

I think it is time I write the most simplistic code I can for just temp and humidity and have no lcd.

I approve of an approach that starts small and adds functionality as what you have proves out in testing.

However, looking at the problems you have had with your wiring already (LCD getting scrambled for example), I'm not convinced that you have code issues.

It might be worth replacing your outputs with leds and your inputs with pots and see if it still crashes. I will be mildly surprised if it does.

I approve of an approach that starts small and adds functionality as what you have proves out in testing.

However, looking at the problems you have had with your wiring already (LCD getting scrambled for example), I'm not convinced that you have code issues.

It might be worth replacing your outputs with leds and your inputs with pots and see if it still crashes. I will be mildly surprised if it does.

And run it far from any refrigerators.

I just wrote this up and uploaded it to the arduino and its working. Lets see if it stops after a while.

//Simple egg incubator temp and humidity control

#include "Adafruit_Si7021.h"
Adafruit_Si7021 sensor = Adafruit_Si7021();

const int heater = A0;
const int humidifier = 10;

float heaterOffThreshold = 37.6;
float heaterOnThreshold = 37;
float humidifierOnThreshold = 45;
float humidifierOffThreshold = 48;

float Temperature;
float humidity;

void setup() {
  
 sensor.begin();
 
 pinMode(LED_BUILTIN, OUTPUT);
 pinMode(heater, OUTPUT);
 digitalWrite(heater, HIGH);
 pinMode(humidifier, OUTPUT);
 digitalWrite(humidifier, HIGH);

}

void loop() 

   { 
    BlinkLED();
    GetTandH();
    TempControl();
    HumidityControl();
   }

void BlinkLED()
   {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
   }

void GetTandH()
   {
    humidity = sensor.readHumidity();
    Temperature = sensor.readTemperature(); 
   }

void TempControl()
   {  
     // If heater is currently off, and temperature is low, turn on the heater.
      if(Temperature < heaterOnThreshold)
      {
       digitalWrite(heater, LOW);
      }
     // If heater is currently on, and temperature is high, turn off the heater.
     if(Temperature > heaterOffThreshold)
     {
      digitalWrite(heater, HIGH);
     }
   }

void HumidityControl()
   {
     // If humidifier is currently off, and humidity is low, turn on the humidifier.
     if(humidity < humidifierOnThreshold)
     {
      digitalWrite(humidifier, LOW);
     }
 
     // If humidifier is currently on, and humidity is high, turn off the humidifier.
     if(humidity > humidifierOffThreshold)
     {
      digitalWrite(humidifier, HIGH);
     }
   }
   
 
  

 // THE END

Anita_Bath:
Could you please show me how to do this? Would save me a lot of time :slight_smile:

would you mind posting your code? (or send it to me in a PM)

i don't think it's a matter of how many times you toggle an LED. I think it's a matter of how you keep track of time and schedule events

Delay can be such a DELAY when searching for errors in your code
Delay makes the CPU count-up as fast a the CPU can. This occupies ALL calculating-power of the CPU there is zero left for other things. You should use the function millis() to initiate code-execution in intervals.

something similar to this

    if (currentMillis - previousBlink_Millis >= 1000) 
    {
       previousBlink_Millis += 1000;
       if (digitalRead((LED_BUILTIN) == HIGH)
       { digitalWrite(LED_BUILTIN, LOW); }
       else
       {digitalWrite(LED_BUILTIN, HIGH); }
    }

best regards

Stefan

Hows this look?

//Simple egg incubator temp, humidity and turner control


  // Sensor library
#include "Adafruit_Si7021.h"
Adafruit_Si7021 sensor = Adafruit_Si7021();

  // LCD Library
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

  // Outputs
const int heater = A0;
const int humidifier = 10;
const int turner = A1;

  // Set points for temp and humidity
float heaterOffThreshold = 37.6;
float heaterOnThreshold = 37;
float humidifierOnThreshold = 45;
float humidifierOffThreshold = 48;

  // Temp and humidity varibles
float Temperature;
float humidity;

  // Blinking LED varibles
unsigned long previousBlink_Millis = 0;
unsigned long currentMillis = 0;

  // Turner varibles
int TurnerONOFF = 1;  // use this to turn the egg turner system on or off 0 is off 1 is on
const int turnerInterval = 2; // hours
const int turnerDuration = 25;  // seconds
unsigned long rotationtime;
unsigned long durationB4spin;
unsigned long durationONspin;
byte Turner_State = HIGH; 
unsigned long previousTurner_Millis = 0;

// variables to keep track of the status of 
// the system (0=off,1=on)
int heaterStatus;
int humidifierStatus;
int turnerStatus;

void setup() {
  
  sensor.begin();

 // Set up the LCD
 lcd.begin ();
 // Switch on the backlight.
 // lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
 lcd.setBacklight(HIGH);
 // Clear the display.
 lcd.clear();
 delay(200);
}

 pinMode(LED_BUILTIN, OUTPUT);
 
 pinMode(heater, OUTPUT);
 digitalWrite(heater, HIGH);
 
 pinMode(humidifier, OUTPUT);
 digitalWrite(humidifier, HIGH);
 
 pinMode(turner, OUTPUT);
 digitalWrite(turner, HIGH);

}

void loop() 

   { 
    BlinkLED();
    GetTandH();
    TempControl();
    HumidityControl();
    updateTurnerState();
    SwitchTurnerMotorState();
    PrintToLCD();
    displaySystemStatus();
   }

void BlinkLED()
    

   {
    currentMillis = millis();
    
    if (currentMillis - previousBlink_Millis >= 1000)
    {
       previousBlink_Millis += 1000;
       
       if (digitalRead((LED_BUILTIN) == HIGH))
       { 
        digitalWrite(LED_BUILTIN, LOW);
       }
       else
       {
        digitalWrite(LED_BUILTIN, HIGH); 
       }
    }
    
   }

void GetTandH()
   {
    humidity = sensor.readHumidity();
    Temperature = sensor.readTemperature(); 
   }

void TempControl()
   {  
     // If heater is currently off, and temperature is low, turn on the heater.
      if(Temperature < heaterOnThreshold)
      {
       digitalWrite(heater, LOW);
       heaterStatus = 1;
      }
     // If heater is currently on, and temperature is high, turn off the heater.
     if(Temperature > heaterOffThreshold)
     {
      digitalWrite(heater, HIGH);
      heaterStatus = 0;
     }
   }

void HumidityControl()
   {
     // If humidifier is currently off, and humidity is low, turn on the humidifier.
     if(humidity < humidifierOnThreshold)
     {
      digitalWrite(humidifier, LOW);
      humidifierStatus = 1;
     }
 
     // If humidifier is currently on, and humidity is high, turn off the humidifier.
     if(humidity > humidifierOffThreshold)
     {
      digitalWrite(humidifier, HIGH);
      humidifierStatus = 0;
     }
   }

void updateTurnerState()
  {
   durationB4spin = (turnerInterval*3600000); 
   durationONspin = (turnerDuration*1000);    
    if ((Turner_State == HIGH) && (TurnerONOFF >= 1)) 
    {
    if (currentMillis - previousTurner_Millis >= durationB4spin) 
    {
      Turner_State = LOW;
      turnerStatus = 1;
      previousTurner_Millis += durationB4spin;
    }
  }
  else 
  {
    if (currentMillis - previousTurner_Millis >= durationONspin) 
    {
      Turner_State = HIGH;
      turnerStatus = 0;
      previousTurner_Millis += durationONspin;
    } 
  }    
 }

void SwitchTurnerMotorState(){
  digitalWrite(turner, Turner_State);
}

void PrintToLCD()
 {
  int dp; //number of decimal places to show
   // Show the temperature on the screen
  lcd.setCursor(0,0);
  dp = 2; //by default
  if (Temperature > 99.9) dp = 0; 
  if (Temperature < 0) dp = 0;
  lcd.print("T=");
  lcd.print(Temperature,dp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.print(" ");
  
  // Show the humidity on the screen
  lcd.print("H=");
  lcd.print(humidity,0);
  lcd.print("% ");
 }


void displaySystemStatus(){
 lcd.setCursor(0,1);
 // We need to display the status of the heater, fan,
 // and humidifier which can be ON or OFF. If status is
 // ON, fill in character in front of device name.
 if(heaterStatus == 1) 
 lcd.write(255);
 else lcd.print(" ");
 lcd.print("Heat ");
 if(humidifierStatus == 1)
 lcd.write(255);
 else lcd.print(" ");
 lcd.print("Hum ");
 if(turnerStatus == 1)
 lcd.write(255);
 else lcd.print(" ");
 lcd.print("Turn ");
}
   
 // THE END

Hows this look?

It looks like you haven't found the auto-format tool yet.

TheMemberFormerlyKnownAsAWOL:
It looks like you haven't found the auto-format tool yet.

haha I have heard about it but I have no idea where it is yet. I had to change a few things to get the led to blink.

//Simple egg incubator temp and humidity and turner control


  // Sensor library
#include "Adafruit_Si7021.h"
Adafruit_Si7021 sensor = Adafruit_Si7021();

  // LCD Library
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

  // Outputs
const int heater = A0;
const int humidifier = 10;
const int turner = A1;

  // Set points for temp and humidity
float heaterOffThreshold = 37.6;
float heaterOnThreshold = 37;
float humidifierOnThreshold = 45;
float humidifierOffThreshold = 48;

  // Temp and humidity varibles
float Temperature;
float humidity;

  // Blinking LED varibles
  // These variables store the flash pattern
  // and the current state of the LED
 
int ledState = LOW;             // ledState used to set the LED
unsigned long previousLEDMillis = 0;        // will store last time LED was updated
long OnTime = 250;           // milliseconds of on-time
long OffTime = 750;          // milliseconds of off-time
unsigned long currentMillis;

  // Turner varibles
int TurnerONOFF = 1;  // use this to turn the egg turner system on or off 0 is off 1 is on
const int turnerInterval = 2; // hours
const int turnerDuration = 25;  // seconds
unsigned long rotationtime;
unsigned long durationB4spin;
unsigned long durationONspin;
byte Turner_State = HIGH; 
unsigned long previousTurner_Millis = 0;

// variables to keep track of the status of 
// the system (0=off,1=on)
int heaterStatus;
int humidifierStatus;
int turnerStatus;

void setup() {
  
 // Set up the sensor
  sensor.begin();

 // Set up the LCD
 lcd.begin ();
 // Switch on the backlight.
 // lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
 lcd.setBacklight(HIGH);
 // Clear the display.
 lcd.clear();
 delay(200);


 pinMode(LED_BUILTIN, OUTPUT);
 
 pinMode(heater, OUTPUT);
 digitalWrite(heater, HIGH);
 
 pinMode(humidifier, OUTPUT);
 digitalWrite(humidifier, HIGH);
 
 pinMode(turner, OUTPUT);
 digitalWrite(turner, HIGH);

}

void loop() 

   {
    currentMillis = millis(); 
    BlinkLED();
    GetTandH();
    displaySystemStatus();
    TempControl();
    HumidityControl();
    updateTurnerState();
    SwitchTurnerMotorState();
    PrintToLCD();
   }

void BlinkLED()
 {
  if((ledState == HIGH) && (currentMillis - previousLEDMillis >= OnTime))
  {
    ledState = LOW;  // Turn it off
    previousLEDMillis = currentMillis;  // Remember the time
    digitalWrite(LED_BUILTIN, ledState);  // Update the actual LED
  }
  else if ((ledState == LOW) && (currentMillis - previousLEDMillis >= OffTime))
  {
    ledState = HIGH;  // turn it on
    previousLEDMillis = currentMillis;   // Remember the time
    digitalWrite(LED_BUILTIN, ledState);   // Update the actual LED
  }
 }

void GetTandH()
   {
    humidity = sensor.readHumidity();
    Temperature = sensor.readTemperature();
   }

void displaySystemStatus()
 {
  lcd.setCursor(0,1);
  // We need to display the status of the heater, fan,
  // and humidifier which can be ON or OFF. If status is
  // ON, fill in character in front of device name.
  if(heaterStatus == 1) 
  lcd.write(255);
  else lcd.print(" ");
  lcd.print("Heat ");
  if(humidifierStatus == 1)
  lcd.write(255);
  else lcd.print(" ");
  lcd.print("Hum ");
  if(turnerStatus == 1)
  lcd.write(255);
  else lcd.print(" ");
  lcd.print("Turn ");
 }

void TempControl()
   {  
     // If heater is currently off, and temperature is low, turn on the heater.
      if(Temperature < heaterOnThreshold)
      {
       digitalWrite(heater, LOW);
       heaterStatus = 1;
      }
     // If heater is currently on, and temperature is high, turn off the heater.
     if(Temperature > heaterOffThreshold)
     {
      digitalWrite(heater, HIGH);
      heaterStatus = 0;
     }
   }

void HumidityControl()
   {
     // If humidifier is currently off, and humidity is low, turn on the humidifier.
     if(humidity < humidifierOnThreshold)
     {
      digitalWrite(humidifier, LOW);
      humidifierStatus = 1;
     }
 
     // If humidifier is currently on, and humidity is high, turn off the humidifier.
     if(humidity > humidifierOffThreshold)
     {
      digitalWrite(humidifier, HIGH);
      humidifierStatus = 0;
     }
   }

void updateTurnerState()
  {
   durationB4spin = (turnerInterval*3600000); 
   durationONspin = (turnerDuration*1000);    
    if ((Turner_State == HIGH) && (TurnerONOFF >= 1)) 
    {
    if (currentMillis - previousTurner_Millis >= durationB4spin) 
    {
      Turner_State = LOW;
      turnerStatus = 1;
      previousTurner_Millis += durationB4spin;
    }
  }
  else 
  {
    if (currentMillis - previousTurner_Millis >= durationONspin) 
    {
      Turner_State = HIGH;
      turnerStatus = 0;
      previousTurner_Millis += durationONspin;
    } 
  }    
 }

void SwitchTurnerMotorState()
 {
  digitalWrite(turner, Turner_State);
 }

void PrintToLCD()
 {
  int dp; //number of decimal places to show
   // Show the temperature on the screen
  lcd.setCursor(0,0);
  dp = 2; //by default
  if (Temperature > 99.9) dp = 0; 
  if (Temperature < 0) dp = 0;
  lcd.print("T=");
  lcd.print(Temperature,dp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.print(" ");
  
  // Show the humidity on the screen
  lcd.print("H=");
  lcd.print(humidity,0);
  lcd.print("% ");
 }

   
 // THE END

turnerInterval*3600000 <———<<<< 3600000ul

larryd:
turnerInterval*3600000 <———<<<< 3600000ul

Please explain. Couldn't the 3600000 be an "l" and the 1000 a "u"

Even with the simple sketch it still got hung up after a few hours and the heater was stuck on so it hit 60 degrees :s. gonna have hard boiled eggs soon lol. Ill set up a breadboard and see if it crashes with leds hooked up to it.

unsigned long durationB4spin;
. . .
ul tells the compiler to use unsigned long math, the default is integer math.

  • integer (16 bit) - unsigned number from 0 to 65535
  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295

Suggest you get into the habit of adding ul when dealing with unsigned numbers/constants.

durationB4spin = (turnerInterval3600000ul);
durationONspin = (turnerDuration
1000ul);

larryd:
unsigned long durationB4spin;
. . .
ul tells the compiler to use unsigned long math, the default is integer math.

  • integer (16 bit) - unsigned number from 0 to 65535
  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295

Suggest you get into the habit of adding ul when dealing with unsigned numbers/constants.

durationB4spin = (turnerInterval3600000ul);
durationONspin = (turnerDuration
1000ul);

Is it possible that not having the "ul" would make it get hung up / freeze the loop? If not then what's the consequence since it still ran without it. I did add it though.