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
-
Arduino Board
-
Temperature sensor(LM35)
-
FAN(for cooling incubator)
-
LCD(Liquid Crystal Display)
-
Seven segment display(Common Cathode)
-
DC Motor(for turning eggs)-this will rotate clockwise and anticlockwise
-
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);
}
}