incubator help

Hello everyone,

Am new in programming and arduino but I believe in sharing ideas and I accept correction if am wrong.

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.

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);  
}
}

A few questions first ;

  1. Is your hardware all connected up to the Arduino ?

  2. Have you tried each of the above programs separately with your hardware ?

  3. Can you explain, in a paragraph or 2, what the motors are supposed to do and how often ?

Just glancing at the code, it seems fairly reasonable to put them all in one. Depending on your answers to the above, I might recommend melding programs 1 and 2 first and getting them to work w/the hardware before adding #3 into the mix.

first of all, am using proteus to test the software part. proteus responding very positive as well as i put them in hardware they respond very fine but this response is on separate blocks that means three blocks corresponding to the three programs i have posted

but now am trying to combine them together but its refusing in proteus as well as hardware,

for motor:

chicken eggs suppose to be turned at around three times within 24hrs,the benefits of turning eggs prevent yolk sticking on one side of the shell.so i have two pins from arduino pin9 and pin10 which help me to come up with clockwise and anticlockwise direction with the help of two relays.if pin 9 is Low and pin 10 is High,motor will rotate clockwise. if pin 9 is high and pin10 is low , motor will rotate anticlockwise. if both pins 9 and 10 are low,motor will stop.

Generally motor will rotate clockwise for about 3 seconds then stays off for 8 hrs then rotate anticlockwise for another 3 seconds then stays off for other 8hrs.so within 24hrs i will done with three times rotation.however i will stop this kind of turning at day 18 of hatching.so there in my code i use millis() to monitor timing upto 18 days then to stop rotation but in my third code you will motor will stop after 20 second for testing purpose

i agree with your recommendation of melding programs 1 and 2 first and getting them to work before adding the third one

thanks Mee_n_Mac for your quick response

Let’s look at this part of the 2’nd program;

while (millis() - startTime < interval); //wait 1 minute

The code is stuck running the while() loop until enough msecs have passed (= interval). Then it ‘escapes’ and runs the rest of the program to update the elapsed time display. A new value for startTime is then stored and the whole process repeats. Because the code is stuck wasting time in that while() loop, other things can’t be done.

What if the loop() portion of the code were rewritten this way ?

void loop() {
  if (millis() > runTime1 + interval1) {
    //update the counters counter1 is the day count
    counter1++;
    if (counter1 > 10) {
      counter1 = 0;
      //counter2 is the 10s of days count
      counter2++;
      //the below is just be be sure code doesn't FU display
      if (counter2 > 10) {
        counter2 = 0;
      }
    }
    //now send data to SRs and display
    shiftOut(DATA, CLK, MSBFIRST, ~digitTwo[counter2]); // digitTwo
    shiftOut(DATA, CLK, MSBFIRST, ~digitOne[counter1]); // digitOne
    digitalWrite(LATCH, HIGH);
    delay(1);
    digitalWrite(LATCH, LOW);
    //store new time
    runTime1 = millis();
  }
  //put other code for other timed actions below
  if (millis() > runTime2 + interval2) {
    //more code here
  }
  if (millis() > runTime3 + interval3) {
    //more code here
  }
}

Note that you’ll need to declare and initialize some new variables but now the loop() loops very quickly and only when the interval has passed does the display update. This means other timed actions (getting temp data, fan control, etc) can run at their own separate intervals.