How to combine 2 or more arduino codes

Just want some help, need to combine these 2 codes. I already try to copy and paste the 3 in one page but there are some errors in the code.

The fist is a 5528 Photoresistor Light Sensor

onst int pinPhoto = A2;
int raw = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pinPhoto, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
raw = analogRead(pinPhoto);
Serial.println(raw);
delay(500);
}

The Second one DHT11 Temperature and Humidity sensor

include <dht.h>

#define dht_apin A1 // Analog Pin sensor is connected to

dht DHT;

void setup(){

Serial.begin(9600);

delay(500);//Delay to let system boot

Serial.println(“DHT11 Humidity & temperature Sensor\n\n”);

delay(1000);//Wait before accessing Sensor

}//end “setup()”

void loop(){

//Start of Program

DHT.read11(dht_apin);

Serial.print("Current humidity = ");

Serial.print(DHT.humidity);

Serial.print("% ");

Serial.print("temperature = ");

Serial.print(DHT.temperature);

Serial.println("C ");

delay(5000);//Wait 5 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}// end loop()

The last FC28 ground humidity sensor

#define WATERPIN 2
#define READSOILPIN A0

// higher number is more dry
#define MAXDRYNESS 700

#define WATERDELAY 750
#define WATERPOSTDELAY 5000

void setup()
{
Serial.begin(9600);
pinMode(READSOILPIN, INPUT);
pinMode(WATERPIN, OUTPUT);
}

void loop()
{
int SensorValue = analogRead(READSOILPIN); //take a sample
Serial.print(SensorValue); Serial.print(" - ");

if(SensorValue >= MAXDRYNESS)
{
// if the soil is too dry start watering for 3/4 a second then
// wait for 5 seconds before monitoring again
Serial.println(“Soil dry start watering”);
digitalWrite(WATERPIN, HIGH);
delay(WATERDELAY);
digitalWrite(WATERPIN, LOW);
delay(WATERPOSTDELAY);
}

delay(50);
}

What do you want the combined program to do ?

Do you care that the timing of the sensor readings will not be the same as now if you simply combine the programs ?

Please post what you tried so that we can give advice.

UKHeliBob:
What do you want the combined program to do ?

Do you care that the timing of the sensor readings will not be the same as now if you simply combine the programs ?

Please post what you tried so that we can give advice.

The timing is no problem, I just want the code to work. The proyect is a auto sustainable plant, with a light sensor to turn on a lamp, a groung humidity sensor to know when to water the plant and the temperature sensor to turn on a fan. I just want those 3 codes to work to give me on screen the results.

What have you tried?
Show us your code.
Tell us what problems you had with it

Please remember to use code tags when posting code

We just paste the 3 codes together, never did a change on the codes to merge them.

AWOL:
What have you tried?
Show us your code.

const int pinPhoto = A2;
int raw = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pinPhoto, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
raw = analogRead(pinPhoto);
Serial.println(raw);
delay(500);
}

include <dht.h>

#define dht_apin A1 // Analog Pin sensor is connected to

dht DHT;

void setup(){

Serial.begin(9600);

delay(500);//Delay to let system boot

Serial.println(“DHT11 Humidity & temperature Sensor\n\n”);

delay(1000);//Wait before accessing Sensor

}//end “setup()”

void loop(){

//Start of Program

DHT.read11(dht_apin);

Serial.print("Current humidity = ");

Serial.print(DHT.humidity);

Serial.print("% ");

Serial.print("temperature = ");

Serial.print(DHT.temperature);

Serial.println("C ");

delay(5000);//Wait 5 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}// end loop()

#define WATERPIN 2
#define READSOILPIN A0

// higher number is more dry
#define MAXDRYNESS 700

#define WATERDELAY 750
#define WATERPOSTDELAY 5000

void setup()
{
Serial.begin(9600);
pinMode(READSOILPIN, INPUT);
pinMode(WATERPIN, OUTPUT);
}

void loop()
{
int SensorValue = analogRead(READSOILPIN); //take a sample
Serial.print(SensorValue); Serial.print(" - ");

if(SensorValue >= MAXDRYNESS)
{
// if the soil is too dry start watering for 3/4 a second then
// wait for 5 seconds before monitoring again
Serial.println(“Soil dry start watering”);
digitalWrite(WATERPIN, HIGH);
delay(WATERDELAY);
digitalWrite(WATERPIN, LOW);
delay(WATERPOSTDELAY);
}

delay(50);
}

We just paste the 3 codes together, never did a change on the codes to merge them.

And did that compile? Of course not. You can have ONE setup() and ONE loop() function. You must decide what that ONE setup() function does and what the ONE loop() function does.

Good Evening, I badly need your analytical and programming skills, newbie here. I dont know how to combine these two codes I got from different sources and decided to put it here for you guys to help me. And I also want to verify where to connect the water pump in accordance with your ideas. I dont really know where to connect it if its either to the RTC Module or to the Soil Moisture.

Materials I Used:
-Arduino Uno
-Relay
-12V Water Pump
-Soil Moisture Sensor
-RTC Module
-Jumper Wires

Codes:
(Soil Moisture)
int ACWATERPUMP = 13;
int sensor = 8;
int val;
void setup() {
pinMode(13,OUTPUT);
pinMode(8,INPUT);
}

void loop() {
val = digitalRead(8 );
if(val == LOW)
{
digitalWrite(13,LOW);
}
else
{
digitalWrite(13,HIGH);
}
delay(400);
}

(DS3231 RTC Module)
#include <DS3231.h>

int Relay = 4;

DS3231 rtc(SDA, SCL);
Time t;

const int OnHour = 22; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 48;
const int OffHour = 22; //SET TIME TO OFF RELAY
const int OffMin = 49;

void setup() {
Serial.begin(115200);
rtc.begin();
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}

void loop() {
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), “);
Serial.print(t.min);
Serial.print(” minute(s)");
Serial.println(" ");
delay (1000);

if(t.hour == OnHour && t.min == OnMin){
digitalWrite(Relay,HIGH);
Serial.println(“PUMP ON”);
}

else if(t.hour == OffHour && t.min == OffMin){
digitalWrite(Relay,LOW);
Serial.println(“PUMP OFF”);
}
}

Code untested but combined.

#include <DS3231.h>

int Relay = 4;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 22; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 48;
const int OffHour = 22; //SET TIME TO OFF RELAY
const int OffMin = 49;

int ACWATERPUMP = 13;
int sensor = 8;
int val;


void setup()
{
  Serial.begin(115200);

  pinMode(13, OUTPUT);
  pinMode(8, INPUT);
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);

  rtc.begin();

}

void loop()
{
  val = digitalRead(8 );
  if (val == LOW)
  {
    digitalWrite(13, LOW);
  }
  else
  {
    digitalWrite(13, HIGH);
  }
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);

  if (t.hour == OnHour && t.min == OnMin) 
  {
    digitalWrite(Relay, HIGH);
    Serial.println("PUMP ON");
  }

  else if (t.hour == OffHour && t.min == OffMin) 
  {
    digitalWrite(Relay, LOW);
    Serial.println("PUMP OFF");
  }

  delay(400);
}

There's no error to the combined code you made, sir. Thank you so much. But one last thing does the water pump connected to the RTC module?

bonniepogi:
There's no error to the combined code you made, sir. Thank you so much. But one last thing does the water pump connected to the RTC module?

Sorry, I reread your questions.

As for the water pump, think about it.... An RTC is a real time clock. Why would you hook up a pump to it?

Perhaps you need to step back and fully explain what it is you are trying to do.

I will take a guess.

I assume you want to see if the soil is dry.

If it is, you want to turn the pump on. If my guess is correct, you would not hook the pump up to RTC or soil sensor. You would see that it is dry and turn on the pump.

Romonaga:
Sorry, I reread your questions.

As for the water pump, think about it.... An RTC is a real time clock. Why would you hook up a pump to it?

Perhaps you need to step back and fully explain what it is you are trying to do.

I will take a guess.

I assume you want to see if the soil is dry.

If it is, you want to turn the pump on. If my guess is correct, you would not hook the pump up to RTC or soil sensor. You would see that it is dry and turn on the pump.

The main fuction that my device wanted to do is to water the plants every 5:30 am and when water sensor is already wet it will automatically turn off. Then water again at 5:30 pm. So this is what i am trying to say.

I've deleted your other cross post @bonniepogi.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks for your cooperation.

pert:
I've deleted your other cross post @bonniepogi.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks for your cooperation.

Oooohhh, sorry, my bad. Will not do it again.