incubator help

Hello everyone,

Am building chicken egg incubator using arduino codes but I come up to stuck somewhere for the weeks now, my issue is how to combine arduino sketches to come up with one code.

I have three separate arduino sketches (codes) that I need to combine them together to come up with one code. The three codes are:

1st Code : this contains codes for temperature sensor, fan, and LCD

2nd Code: this contains codes for counting that is seven segment display

3rd Code: this contains codes for DC motor

I will be glad if I get assistance on how I can combine them especially if i start putting them together one by one. The codes might not be on its best but just understand that am new in programming and am trying to learn so quickly. Please find my three sketches.

Note: don’t bother about the temperature values I put in my codes, those one are just for testing

Please have a clue of what am doing in incubator project before you assist me. In my project I have the following

  1. Arduino Board
  2. Temperature sensor(LM35)
  3. FAN(for cooling incubator)
  4. LCD(Liquid Crystal Display)
  5. Seven segment display(Common Cathode)
  6. DC Motor(for turning eggs)-this will rotate clockwise and anticlockwise
  7. Shift registers (74HC595)
    This is chicken egg incubator will hatch for 21 days, I want to control temperature at 37.5 degree centigrade for the whole 21 days as well as humidity at 75% for 18 days, 90% for last 3 days of hatching.
    Liquid crystal display for showing temperature and humidity that will be sensed by temperature and humidity sensor
    Eggs are suppose to be turned for the for 18 days and stop turning them for the remaining 3 days, so here motor will help me to do this task. Motor will be turned clockwise and anticlockwise for 3 times per day.
    For the Seven segment display, I want it to display number of days from day 0 to day 21. So this will increment by one after every 24hrs
    Shift register: 74hc595 will help me to reduce number of output from arduino, this is serial in parallel out (SIPO). For example two seven segment display needs 16 pins from arduino so with help of 74HC 595 I will take only three pins from arduino to 74hc595. 74HC595 will give out 16 outputs to seven segment display

first code:

#include <ShiftLCD.h>
ShiftLCD lcd(2, 4, 3);
 
 int tempPin = A0;   // the output pin of LM35
int fan = 11;       // the pin where fan is
int led = 8;        // led pin
int temp;
int tempMin = 30;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
 
void setup() {
  
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16, 2);
 
}


int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}
 
void loop() {
   temp = readTemp();     // get the temperature

      
      if(temp < tempMin){    // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);       
                               }
  
   
   if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
   
   if(temp > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }
   
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(50);
   lcd.clear();   
}

second code:

#define LATCH 12
#define CLK 13
#define DATA 11

//This is the dec value of each number stored in an array by index num

int i,j;
int digitOne[10]= {192,249,164,176,153,146,130,248,128,24};
int digitTwo[10]= {192,249,164,176,153,146,130,248,128,24};

unsigned long interval=500;

void setup(){
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loop(){
  for(int i=0; i<10; i++){
    for(int j=0; j<10; j++){
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLK, MSBFIRST, ~digitTwo[j]); // digitTwo
      shiftOut(DATA, CLK, MSBFIRST, ~digitOne[i]); // digitOne
      digitalWrite(LATCH, HIGH);
      unsigned long startTime = millis();
      while (millis() - startTime < interval); //wait 1 minute
//i will change later from 1 minute to 24hrs
}
  }
}

third code:

int PinOne =  9;
int PinTwo =  10;

int PinOneState =  LOW;
int PinTwoState =  LOW;

unsigned long previousTime1=0;
unsigned long previousTime2 = 0;

unsigned long intervalON = 6000;
unsigned long long intervalOFF = 2000;
unsigned long stopTurning=20000;

void setup() {
  pinMode(PinOne, OUTPUT); 
  pinMode(PinTwo, OUTPUT); 
  digitalWrite(PinOne, PinOneState);
  digitalWrite(PinTwo, PinTwoState);
  
}

void loop() {

  unsigned long currentTime1=millis();
  
  if (currentTime1 - previousTime1 < stopTurning){
  
  unsigned long currentTime2 = millis();
  
  if ((PinOneState == LOW) && (PinTwoState == LOW)){
    if (currentTime2 - previousTime2 > intervalOFF){//and enough time has passed
      digitalWrite(PinOne, LOW);//CLOCKWISE
      digitalWrite(PinTwo, HIGH);
      PinOneState = LOW;//store its current state
      PinTwoState = HIGH;
      previousTime2 = currentTime2;//update the time of this new event
    }
  }  else if((PinOneState == LOW) && (PinTwoState == HIGH)){
        if (currentTime2 - previousTime2 > intervalON){
      digitalWrite(PinOne,HIGH);//STOP
      digitalWrite(PinTwo, HIGH);
      PinOneState = HIGH;
      PinTwoState = HIGH;
      previousTime2 = currentTime2;
    }
  }
  
   else if((PinOneState == HIGH) && (PinTwoState == HIGH)){
    if (currentTime2 - previousTime2 > intervalOFF){
      digitalWrite(PinOne,HIGH);//ANTCLOCKWISE
      digitalWrite(PinTwo, LOW);
      PinOneState = HIGH;
      PinTwoState = LOW;
      previousTime2 = currentTime2;
    }
  }
  
    else if((PinOneState == HIGH) && (PinTwoState == LOW)){
    if (currentTime2 - previousTime2 > intervalON){
      digitalWrite(PinOne,LOW);//STOP
      digitalWrite(PinTwo, LOW);
      PinOneState = LOW;
      PinTwoState = LOW;
      previousTime2 = currentTime2;
    }
  }
  } else{
  
        digitalWrite(PinOne ,LOW);
        digitalWrite(PinTwo ,LOW);  
}
}

thanks
walter

Where is the 4th code, where you tried to put them together? What did that code actually do? What was it supposed to do?

In other words, what are the requirements for the final sketch?

Obviously, you have some pin conflicts. The first thing you should do is modify the individual sketches to resolve them.

Quite some time ago I built an incubator for chicken eggs. Unfortunately I don't have my final sketch available any more.

From what I recall though, your humidity settings look way too high. This could lead to sticky chicks and probably reduce your hatch rate.

i started combining first code and the third code,which works very oky. Just to remind you first code is the code for temperature sensor, fan, and LCD in which they all responding oky as well as third code is the code for DC motor which is also working very well…so generall the combination of first code and third code has no problem and there is no pin conflicts.

combination of first and third code:

#include <ShiftLCD.h>
ShiftLCD lcd(2, 4, 3);
 
 int tempPin = A0;   // the output pin of LM35
int fan = 7;       // the pin where fan is
int led = 8;        // led pin
int temp;
int tempMin = 30;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;


int PinOne =  9;
int PinTwo =  10;

int PinOneState =  LOW;
int PinTwoState =  LOW;

unsigned long previousTime1=0;
unsigned long previousTime2 = 0;

unsigned long intervalON = 6000;
unsigned long long intervalOFF = 2000;
unsigned long stopTurning=20000;


void setup(){
  setupFirstCode();
  setupThirdCode();

}

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}


void loop(){
  
  loopFirstCode();
  loopThirdCode();
  

}

void setupFirstCode() {
  
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16, 2);
 
}

 
void loopFirstCode() {
   temp = readTemp();     // get the temperature
   if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);       
   } 
   if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
   
   if(temp > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }
   
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(50);
   lcd.clear();   
}


void setupThirdCode() {
  pinMode(PinOne, OUTPUT); 
  pinMode(PinTwo, OUTPUT); 
  digitalWrite(PinOne, PinOneState);
  digitalWrite(PinTwo, PinTwoState);
  
}

void loopThirdCode() {

  unsigned long currentTime1=millis();
  
  if (currentTime1 - previousTime1 < stopTurning){
  
  unsigned long currentTime2 = millis();
  
  if ((PinOneState == LOW) && (PinTwoState == LOW)){
    if (currentTime2 - previousTime2 > intervalOFF){//and enough time has passed
      digitalWrite(PinOne, LOW);//CLOCKWISE
      digitalWrite(PinTwo, HIGH);
      PinOneState = LOW;//store its current state
      PinTwoState = HIGH;
      previousTime2 = currentTime2;//update the time of this new event
    }
  }  else if((PinOneState == LOW) && (PinTwoState == HIGH)){
        if (currentTime2 - previousTime2 > intervalON){
      digitalWrite(PinOne,HIGH);//STOP
      digitalWrite(PinTwo, HIGH);
      PinOneState = HIGH;
      PinTwoState = HIGH;
      previousTime2 = currentTime2;
    }
  }
  
   else if((PinOneState == HIGH) && (PinTwoState == HIGH)){
    if (currentTime2 - previousTime2 > intervalOFF){
      digitalWrite(PinOne,HIGH);//ANTCLOCKWISE
      digitalWrite(PinTwo, LOW);
      PinOneState = HIGH;
      PinTwoState = LOW;
      previousTime2 = currentTime2;
    }
  }
  
    else if((PinOneState == HIGH) && (PinTwoState == LOW)){
    if (currentTime2 - previousTime2 > intervalON){
      digitalWrite(PinOne,LOW);//STOP
      digitalWrite(PinTwo, LOW);
      PinOneState = LOW;
      PinTwoState = LOW;
      previousTime2 = currentTime2;
    }
  }
  } else{
  
        digitalWrite(PinOne ,LOW);
        digitalWrite(PinTwo ,LOW);  
}
}

the issue is to add the second code to come up with the whole program, i tried to its i think the main problem is the “good coding”
however individually,second count exactly what i wanted.but one introducing other programs it refuses…
just to remind:second code is the one with seven segment display which i want it to count.

here is the combination of all three sketch which brings no response

#define LATCH 12
#define CLK 13
#define DATA 11
#include <ShiftLCD.h>
ShiftLCD lcd(2, 4, 3);
 
int tempPin = A0;   // the output pin of LM35
int fan = 7;       // the pin where fan is
int led = 8;        // led pin
int temp;
int tempMin = 30;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;


//This is the dec value of each number stored in an array by index num

int i,j;
int digitOne[10]= {192,249,164,176,153,146,130,248,128,24};
int digitTwo[10]= {192,249,164,176,153,146,130,248,128,24};
unsigned long interval=500;


int PinOne =  9;
int PinTwo =  10;

int PinOneState =  LOW;
int PinTwoState =  LOW;

unsigned long previousTime1=0;
unsigned long previousTime2 = 0;

unsigned long intervalON = 6000;
unsigned long long intervalOFF = 2000;
unsigned long stopTurning=20000;


void setup(){

  setupFirstCode();
  setupSecondCode();
  setupThirdCode();
                    }

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}


void loop(){
  
  loopFirstCode();
  loopSecondCode();
  loopThirdCode();

}

 
void setupFirstCode() {
  
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16, 2);
 
}

 
void loopFirstCode() {
   temp = readTemp();     // get the temperature
   if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);       
   } 
   if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
   
   if(temp > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }
   
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(50);
   lcd.clear();   
}



void setupSecondCode() {
  pinMode(PinOne, OUTPUT); 
  pinMode(PinTwo, OUTPUT); 
  digitalWrite(PinOne, PinOneState);
  digitalWrite(PinTwo, PinTwoState);
  
}

void loopSecondCode() {

  unsigned long currentTime1=millis();
  
  if (currentTime1 - previousTime1 < stopTurning){
  
  unsigned long currentTime2 = millis();
  
  if ((PinOneState == LOW) && (PinTwoState == LOW)){
    if (currentTime2 - previousTime2 > intervalOFF){//and enough time has passed
      digitalWrite(PinOne, LOW);//CLOCKWISE
      digitalWrite(PinTwo, HIGH);
      PinOneState = LOW;//store its current state
      PinTwoState = HIGH;
      previousTime2 = currentTime2;//update the time of this new event
    }
  }  else if((PinOneState == LOW) && (PinTwoState == HIGH)){
        if (currentTime2 - previousTime2 > intervalON){
      digitalWrite(PinOne,HIGH);//STOP
      digitalWrite(PinTwo, HIGH);
      PinOneState = HIGH;
      PinTwoState = HIGH;
      previousTime2 = currentTime2;
    }
  }
  
   else if((PinOneState == HIGH) && (PinTwoState == HIGH)){
    if (currentTime2 - previousTime2 > intervalOFF){
      digitalWrite(PinOne,HIGH);//ANTCLOCKWISE
      digitalWrite(PinTwo, LOW);
      PinOneState = HIGH;
      PinTwoState = LOW;
      previousTime2 = currentTime2;
    }
  }
  
    else if((PinOneState == HIGH) && (PinTwoState == LOW)){
    if (currentTime2 - previousTime2 > intervalON){
      digitalWrite(PinOne,LOW);//STOP
      digitalWrite(PinTwo, LOW);
      PinOneState = LOW;
      PinTwoState = LOW;
      previousTime2 = currentTime2;
    }
  }
  } else{
  
        digitalWrite(PinOne ,LOW);
        digitalWrite(PinTwo ,LOW);  
}
}

 

void setupThirdCode(){
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loopThirdCode(){
  for(int i=0; i<10; i++){
    for(int j=0; j<10; j++){
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLK, MSBFIRST, ~digitTwo[j]); // digitTwo
      shiftOut(DATA, CLK, MSBFIRST, ~digitOne[i]); // digitOne
      digitalWrite(LATCH, HIGH);
      unsigned long startTime = millis();
      while (millis() - startTime < interval); //wait 1 minute days
    }
  }
}

thanks KenF and PaulS for your quick response

None of your setup functions has Serial.begin(). None of your loop functions has any Serial.print() statements. How do you know what the code is doing, or where it is going wrong?

am loading my arduino sketches in Proteus ISIS7 professional as simulator for my incubator project

am loading my arduino sketches in Proteus ISIS7 professional as simulator for my incubator project

Down the road, the Chevy dealer might be able to help you with your Ford.

who are chevy dealer?

walterkim, can u fulfill your incubator code?