merging code problem

hi guys,
i am really stuck in merging 2 codes, could anyone please help.
the thing is my main code is about up loading data (temperature and humidity; analog pir sensors) to thingspeak (it work just well)

the other part is 2 digital pir sensors which turn on the led when the dark time (combine with photoresistor). this part work well if i run it separately
int led = 13; // the pin that the LED is atteched to
int sensor111 = 2; // the pin that the sensor is atteched to
int sensor222 = 3; // the pin that the sensor is atteched to
int state111 = LOW; // by default, no motion detected
int val11 = 0; // variable to store the sensor status (value)
int val22 = 0; // variable to store the sensor status (value)
const int ldrPin = A0; //the number of the LDR pin

void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor111, INPUT); // initialize sensor as an input
pinMode(sensor222, INPUT); // initialize sensor as an input
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
Serial.begin(9600); // initialize serial
}

void loop(){
val11 = digitalRead(sensor111); // read sensor value
val22 = digitalRead(sensor222); // read sensor value
int ldrStatus = analogRead(ldrPin); //read the status of the LDR value
Serial.println(ldrStatus);
if ((val11 == HIGH || val22 == HIGH) && ldrStatus <=700) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds

if (state111 == LOW) {
Serial.println("Motion detected!");
state111 = HIGH; // update variable state to HIGH
Serial.println( "LDR is DARK, LED is ON");
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds

if (state111 == HIGH){
Serial.println("Motion stopped!");
state111 = LOW; // update variable state to LOW
}
}
}
when i try to merge the code, the light not turn on anymore. when i checked the code: i tried to remove 1 by 1 each function in the void loop. i realised that the Inital() and upload_to_Gateway() is the 2 parts which make the led code not working when combining.
could anyone please help me check and how to fix it.
thank you very much
below is my full code:

final_client.ino (12.1 KB)

A couple of things to help you get more responses:

  1. Read the "How to post here" post at the top of this Forum on how to use code tags when listing your program.

  2. Move your cursor into the Source Code Window of the IDE then press Ctrl-T. This will reformat your code into a common C coding style, making it easier for everyone to read.

Hi, Some suggestions on how to approach this are HERE:

https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches

terryking228:
Hi, Some suggestions on how to approach this are HERE:

CombiningArduinoSketches - ArduinoInfo

yes, that what i did, i just dont know why the inital and Upload_to_Gateway can stop running my PIRphoto function

Why do you think you need to call Inital() on every pass through loop()? The code in that function looks to me like it should be called ONCE.

Only YOU can see what the program actually does, and outputs to the serial port.

  unsigned char send_Buf[100] = {0};
  for(int i = 0; i< (datalength); i++)
  {
    send_Buf[i] = send_Data[i];
  } 
  
  rf95.send(send_Buf, sizeof(send_Buf));

There is absolutely NO reason to copy the array. Just send the send_Data array (with a cast, if necessary). The send() function does not modify the array it is passed.

  Serial.println(dht_data[1],DEC); // Get Decimal part

That is NOT what that code does.

void PIR_data_Upload() 
{
  second++;
  if (second == 10) 
  {
    second = 0;
    minute++;
  }
  if (minute == MINUTE_VALUE) 
  {
    second = 0;
    minute = 0;
    flag_status = 1;
  }
}

WTF? How does that code relate in any way to that function name?

    if (state == HIGH) 
    {
      Serial.println("SENSOR 111 is stopped!");

Rubbish. The sensor is always working.

Besides the pin number read, EXACTLY how do sensor1() and sensor2() differ? There are not 18 digitalReadN() functions for the Uno. Therein lies a clue.

    delay(10000);                // delay 100 milliseconds

The comment is rubbish. Why do you need to do this at all?

  if ((val11 == HIGH || val22 == HIGH) && ldrStatus <=700) {           // check if the sensor is HIGH

Which sensor? It looks like you copied and pasted code that you didn't understand, but tried to modify anyway. If you do understand it, delete or correct the useless comments.

      state111 = HIGH;       // update variable state to HIGH

Are you CERTAIN that that is what the code does? Useless comments insult our intelligence.