Egg Incubator

here is the sketch

// include the library code needed
#include <LiquidCrystal.h>
#include <SHT1x.h>

// initialize the lcd with the numbers of the interface pins
LiquidCrystal lcd(12, 13, 7, 4, 8, 19);

//initialize the SHT1x sensor
#define dataPin  1                             //data connection to pin 1
#define clockPin 11                            //clock connection to pin 11 
SHT1x sht1x(dataPin, clockPin);                //instantiate SHT1x sensor

// constants will not change
const int fanpin = 9;                         //fan connected to digital pin 9 
const int lampin = 10;                        //lamp connected to pin 10
    //const int buttonplus = 2;               //button spare with interupt!
const int dialpin = A1;                       //potentiometer at analog 1
const int button_menu = 3;                    //button for menu at pin 3
//variables will change
int i=0;                                     //used for taking samples, logger  
int fanval = 400;                            //to store the value of the potentiometer
int tempval_min = 60;                        //to store the min value for the lamp pwm   
int tempval_max = 180;                       //to store the max value for the lamp pwm 
int tempval_avg = 100;                       //to store the avg value for the lamp pwm 
int tempval = 100;                           //to store the value for the lamp pwm 
int stepval = 0;                             //to store the value for stepping lights 
float fan_step;                              //to store the value for stepping fan 
float valplus = 35.7;                        //variable to store max allowed temp
float valminus = 35.2;                       //variable to store min allowed temp
float temp_c;                                //float number to measure temperature with library
float humidity;                              //float number to measure humidity with library
float new_temp_minus;                        //for calculation of min temp after user input
float new_temp_plus;                         //for calculation of max temp after user input
float diff;                                  //for calculation of temp limits after user input
float valplus_final;                         //for calculation of max temp after user input
float valminus_final;                        //for calculation of min temp after user input
float temp_data[] = {0,0,0,0,0,0};           //array to store temp samples, logger
float hum_data[] = {0,0,0,0,0,0};            //array to store hum samples, logger 
float sum_temp = 0;                          //for calculation of all samples taken, logger
float avg_temp = 0;                          //for calculation and display, logger
float sum_hum = 0;                           //for calculation of all samples taken, logger
float avg_hum = 0;                           //for calculation and display, logger

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long prevMils_1 = 0;                            //used for the delay loop, TO NOT USE delay()
long prevMils_2 = 0;                            //used for the delay loop, TO NOT USE delay()
long interval_1 = 5000;                         //delay for check and adjust temp, 5sec
long interval_2 = 600000;                       //delay for taking samples, 10 min, display last hour avg


void setup()
{
  pinMode(fanpin, OUTPUT);                //set pin 9 as output
  pinMode(lampin, OUTPUT);                //set pin 10 as output
  pinMode(button_menu, INPUT);            //set pin 3 as input
  //pinMode(buttonplus, INPUT);           //set pin 2 as input
  
  
  lcd.begin(16, 2);                       // set up the LCD's number of rows and columns
  lcd.clear();
  lcd.print("ReklaDance");                // display a message to the LCD
  delay(500);                      
  lcd.setCursor(0, 1);                    // set the cursor to column 0, line 1
  lcd.print("     Electronics");          // display a message to the LCD.
  delay(3000);
  lcd.clear();
  lcd.print("Setting up...");             // display a message to the LCD
  delay(1000);                      
  lcd.setCursor(0, 1);                    // set the cursor to column 0, line 1
  lcd.print("Ready Nikolas!");            // display a message to the LCD.
  delay(500);
}

void loop()
{
    
  //turn fan on
  analogWrite(fanpin, fanval / 4);       // analogRead value go from 0 to 1023, analogWrite value from 0 to 255

  //turn lamp on 
  analogWrite(lampin, tempval);           //about 45% duty cycle, at startup        
  
  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  humidity = sht1x.readHumidity();
  
  valplus_final = new_temp_plus + diff;        //calculation of the final value   
  valminus_final = new_temp_minus;             //exageration
  
  // read the state of the menu button 
   if (digitalRead(button_menu) == HIGH) 
   {
    delay(200);
    displaymenu();                             //call to the menu routine
   }
   
  //use local variable for not using the delay() function
  unsigned long curMils_1 = millis();             //check to see if the difference between the current time and 
  if (curMils_1 - prevMils_1 > interval_1) {      //last time you checked is bigger than the interval set
    prevMils_1 = curMils_1;                       //save the last time you checked
    
      //loop to set the temperature betweem user input 
      if (temp_c<valminus_final)                  //check if temp is lower than,say 37.2
      {
        tempval=tempval_max;                      //set to about 100% duty cycle for pwm lamp
      }
      else if(temp_c>valplus_final)               //check if temp is higher than, say 37.7
      {
        tempval=tempval_min;                      //set to about 50% duty cycle for pwm lamp
      }  
      else                                        //temp is between, say 37.2 and 37.7
      {
        tempval=tempval_avg;                      //set to about 75% duty cycle for pwm lamp
      }
    }
    
    //use local variable for not using the delay() function
  unsigned long curMils_2 = millis();             //check to see if the difference between the current time and 
  if (curMils_2 - prevMils_2 > interval_2) {      //last time you checked is bigger than the interval set
    prevMils_2 = curMils_2;                       //save the last time you checked
  
  //LOGGER, without the need of any external hardware
  if (i<=5)                                       //loop for taking samples
      {
      temp_data[i] = 0;                           //set all samples for temperature to zero
      hum_data[i] = 0;                            //set all samples for humidity to zero
      delay(100);
      temp_data[i] = temp_c;                      //6 samples for temperature
      hum_data[i] = humidity;                     //6 samples for humidity
      i = i++;
      }
      if (i>5)                                    //loop to make sure that we wont
      {                                           //write any sample outside the specified array
        i=0;
      }
    }
    
  //display the values
  lcd.clear();
  lcd.setCursor(0, 0);                            // set the cursor to column 0, line 0
  lcd.print("T:");                                // display the temperature to the lcd
  lcd.print(temp_c, 1);               
  lcd.print("C");  
  lcd.print("    ");
  lcd.print(valminus_final, 0);                  //display the user input temp levels
  lcd.print("-");
  lcd.print(valplus_final, 0);
  lcd.setCursor(0, 1);                           // set the cursor to column 0, line 1 
  lcd.print("H:");                               // display the humidity to the lcd
  lcd.print(humidity);                  
  lcd.print("%");  
  lcd.print("   L");                             //display the user input light step
  lcd.print(stepval);
  lcd.print("-F");                               //display the user input fan step
  lcd.print(fan_step);
}