How to override one condition over another?

Hi guys. I'm facing a problem with my programming.
Project objective: Smart lighting. 5 Lamp-post with infrared sensors and led.
Right now i'm able to light up the LED to 100% once motion is detected. However the problem now is once lamp 1 detected motion, it light up for 5secs. if in between the 5sec, lamp 2 detected motion, it does not override the code of lamp 1.
Any kind soul is able to guide me one this?

    if(FC1==0)
    {
      schedule1(); //
      FC1=2;
    }
    if(FC2==0)
    {
      schedule2();
      FC2=2;
    }
    if(FC3==0)
    {
      schedule3();
      FC3=2;
    }
    if(FC4==0)
    {
      schedule4();
      FC4=2;
    }
    if(FC5==0)
    {
      schedule5();
      FC5=2;
    }
 if(sensor1==LOW && a==0) //motion detected when sensor1 is LOW
    {   
      analogWrite(LED2,255);
      analogWrite(LED1,255);
     a=1;FC1=1;FC2=1;timeElapsed1=0;
     }
    if(sensor2==LOW && b==0)
    {   
      analogWrite(LED2,255);
      analogWrite(LED1,255);
      analogWrite(LED3,255);
     b=1; bitch=1; FC1=1; FC2=1; FC3=1;timeElapsed2=0;
    }
    if(sensor3==LOW && c==0)
    {   
      analogWrite(LED2,255);
      analogWrite(LED3,255);
      analogWrite(LED4,255);
     c=1;FC2=1; FC3=1; FC4=1;
    }

    if(sensor4==LOW )
    {   
      analogWrite(LED3,255);
      analogWrite(LED4,255);
      analogWrite(LED5,255);
     d=1; FC3=1; FC4=1; FC5=1;
    }
    if(sensor5==LOW )
    {   
      analogWrite(LED4,255);
      analogWrite(LED5,255);
     e=0; FC4=1; FC5=1;
    }
    
///////////////////////////////////////////////////////////////
     if (sensor1==HIGH && a==1 && && sensor2==HIGH && b==0)
    {      
      //timeElapsed1=0; //timer function
      a=0;
      if(sensor1==HIGH && b==0 && sensor2==HIGH)
      {
      FC1=0;FC2=0;
      }

    }
///////////////////////////////////////////////////////////////

     if (sensor2==HIGH && b==1 && (timeElapsed2 >=5000) ) //no motion detected after detected
    {      
      //timeElapsed2=0;
      b=0;
      if(sensor1==HIGH && sensor2==HIGH && sensor3==HIGH)
      {
      FC2=0;FC1=0;FC3=0;
      }
     }
//////////////////////////////////////////////////////////////    

     if (sensor3==HIGH && c==1 && timeElapsed3 >=5000)
    {      
      timeElapsed3=0;
      c=0;FC2=0;FC3=0;FC4=0;
     }
///////////////////////////////////////////////////////////////

     if (sensor4==HIGH && d==1 && timeElapsed4 >=5000)
    {      
      timeElapsed4=0;
      d=0;FC3=0;FC4=0;FC5=0;
     }
///////////////////////////////////////////////////////////////////
     if (sensor5==HIGH&& e==1 && timeElapsed5 >=5000)
     {      
      timeElapsed5=0;
      e=0;FC3=0;FC4=0;FC5=0;
     }

void schedule1()
{
  if(t.hour == 19 && t.min >= 0) //
  {
    analogWrite(LED1, map(70,20,100,0,255));
  }
  else if(t.hour == 20 && t.min >= 0)
  {
    analogWrite(LED1, map(90,20,100,0,255));
  }
  else if(t.hour == 21 && t.min >= 0)
  {
    analogWrite(LED1, map(25,20,100,0,255));
  }
  else if(t.hour == 22 && t.min >= 0)
  {
    analogWrite(LED1, map(70,20,100,0,255));
  }
  else if(t.hour == 23 && t.min >= 0)
  {
    analogWrite(LED1, map(60,20,100,0,255));
  }
  else if(t.hour == 00 && t.min >= 0)
  {
    analogWrite(LED1, map(40,20,100,0,255));
  }
  else if(t.hour == 01 && t.min >= 0)
  {
    analogWrite(LED1, map(30,20,100,0,255));
  }
  else if(t.hour == 02 && t.min >= 0)
  {
    analogWrite(LED1, map(30,20,100,0,255));
  }
    else if(t.hour == 03 && t.min >= 0)
  {
    analogWrite(LED1, map(30,20,100,0,255));
  }
    else if(t.hour == 04 && t.min >= 0)
  {
    analogWrite(LED1, map(40,20,100,0,255));
  }
    else if(t.hour == 05 && t.min >= 0)
  {
    analogWrite(LED1, map(80,20,100,0,255));
  }
    else if(t.hour == 06 && t.min >= 0)
  {
    analogWrite(LED1, map(80,20,100,0,255));
  }
}

A snippet of code like that is useless. Post the complete program

    analogWrite(LED1, map(70,20,100,0,255));

It is dumb to waste the effort to call map() with a constant as the first argument. EVERY call will return the same value. Get your handy-dandy calculator out, and figure out what that value will be.

Sorry for not posting the whole code as its a bit long.

#include <Arduino.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
#include <dht.h>
#include <DS3231.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <elapsedMillis.h>
#include<SoftwareSerial.h>
SoftwareSerial Xbee(1,0);
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
#define DHT22_PIN A8     // DHT 22  (AM2302) - what pin we're connected to
#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
// Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
LiquidCrystal lcd(45, 43, 41, 39, 37, 35);
dht DHT; //DHT 22 
DS3231  rtc(SDA, SCL);
Time t;
elapsedMillis timeElapsed1;    // used by elapsedmilis example
elapsedMillis timeElapsed2;                              // Globally scoped - see comment above
elapsedMillis timeElapsed3;
elapsedMillis timeElapsed4;
elapsedMillis timeElapsed5;
elapsedMillis timeElapsed6;
elapsedMillis TIMEME;
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
int LED4 = 12;
int LED5 = 13;

unsigned char buf[LENG];
int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module

long OnTime=1000;
unsigned long previousMillis=0;
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;
// different intervals for each LED
int intervalLED12 = 5000;
int intervalLED13 = 6000;
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;    

int a,b,c,d,e;
int f,g,h,i,j;
int FC1,FC2,FC3,FC4,FC5;
boolean hello=false;
boolean hell1=false;boolean hell2=false;
int BUTTONPIN=2;
int MOTION1 = A9;
int MOTION2 = A10;
int MOTION3 = A11;
int MOTION4 = A12;
int MOTION5 = A13;

int bitch;
int sensor1;
int sensor2;
int sensor3;
int sensor4;
int sensor5; 

char letter,iphone;
float hum;  //Stores humidity value
float temp; //Stores temperature value

long debouncing_time=15;
volatile unsigned long last_micros;

void setup()
{
  // Setup Serial connection
  Serial.begin(9600);
  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor
  // Initialize the rtc object
  lcd.begin(16, 2);
  rtc.begin();
  attachInterrupt(digitalPinToInterrupt(BUTTONPIN), BUTTONHERE , RISING);
  pinMode(LED1,OUTPUT); analogWrite(LED1,0);
  pinMode(LED2,OUTPUT); analogWrite(LED2,0);
  pinMode(LED3,OUTPUT); analogWrite(LED3,0);
  pinMode(LED4,OUTPUT); analogWrite(LED4,0);
  pinMode(LED5,OUTPUT); analogWrite(LED5,0);
  
  pinMode (MOTION1,INPUT);
  pinMode (MOTION2,INPUT);
  pinMode (MOTION3,INPUT);
  pinMode (MOTION4,INPUT);
  pinMode (MOTION5,INPUT);

  a=0;b=0;c=0;d=0;e=0;
  FC1=0;FC2=0;FC3=0;FC4=0;FC5=0;
  

  // The following lines can be uncommented to set the date and time
  rtc.setDOW(SATURDAY);     // Set Day-of-Week to SUNDAY
  rtc.setTime(07, 01, 50);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(7, 12, 2017);   // Set the date to January 1st, 2014

  if(!tsl.begin())
  {
    /* There was a problem detecting the TSL2561 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
    /* Display some basic information on this sensor */
  displaySensorDetails();
    /* Setup the sensor gain and integration time */
  configureSensor(); 
}
/////////////////////////////lux sensor configuration//////////////////
void configureSensor(void)
{
  tsl.enableAutoRange(true);
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
}
void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
}

void loop()
{
  sensors_event_t event;
  tsl.getEvent(&event);
  int chk = DHT.read22(DHT22_PIN);
  //Read data and store it to variables hum and temp
  hum = DHT.humidity;
  temp= DHT.temperature;
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");
  Serial.print("lux"); Serial.print(event.light);
  
  t= rtc.getTime();
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print("  ");
  // Send time
  Serial.println(rtc.getTimeStr());

//storing the result of the motions sensor into different variables in order to 
//do a condition 
  sensor1 = digitalRead(MOTION1);
  sensor2 = digitalRead(MOTION2);
  sensor3 = digitalRead(MOTION3);
  sensor4 = digitalRead(MOTION4);
  sensor5 = digitalRead(MOTION5);

if(t.hour >= 19 || t.hour < 7) //condition for the timing between 7pm to 7am
{
    if(FC1==0)
    {
      schedule1();
      FC1=2;
    }
    if(FC2==0)
    {
      schedule2();
      FC2=2;
    }
    if(FC3==0)
    {
      schedule3();
      FC3=2;
    }
    if(FC4==0)
    {
      schedule4();
      FC4=2;
    }
    if(FC5==0)
    {
      schedule5();
      FC5=2;
    }

  if (sensor1 == LOW | sensor2== LOW | sensor3 ==LOW | sensor4 == LOW | sensor5 ==0 | FC1==1 | FC2==1 | FC3==1 | FC4==1 | FC5 ==1 | FC1==2 | FC2==2 | FC3==2 | FC4==2 | FC5 ==2)
  { 

    if(sensor1==LOW && a==0)
    {   
      analogWrite(LED2,255);
      analogWrite(LED1,255);
     a=1;FC1=1;FC2=1;timeElapsed1=0;
     }
    if(sensor2==LOW && b==0)
    {   
      analogWrite(LED2,255);
      analogWrite(LED1,255);
      analogWrite(LED3,255);
     b=1; bitch=1; FC1=1; FC2=1; FC3=1;timeElapsed2=0;
    }
    if(sensor3==LOW && c==0)
    {   
      analogWrite(LED2,255);
      analogWrite(LED3,255);
      analogWrite(LED4,255);
     c=1;FC2=1; FC3=1; FC4=1;
    }

    if(sensor4==LOW )
    {   
      analogWrite(LED3,255);
      analogWrite(LED4,255);
      analogWrite(LED5,255);
     d=1; FC3=1; FC4=1; FC5=1;
    }
    if(sensor5==LOW )
    {   
      analogWrite(LED4,255);
      analogWrite(LED5,255);
     e=0; FC4=1; FC5=1;
    }
    

     if(sensor1==HIGH)
     {
     if (sensor1==HIGH && a==1 && ((timeElapsed1 >=10000)>timeElapsed2) && sensor2==HIGH && b==0)
    {      
      a=0;
      FC1=0;FC2=0;
    }

    }


     if (sensor2==HIGH && b==1 && (timeElapsed2 >=10000) ) //no motion detected after detected
    {      
      b=0;hell1=true;
      if(sensor1==HIGH && sensor2==HIGH && sensor3==HIGH)
      {
      FC2=0;FC1=0;FC3=0;
      }
     }


     if (sensor3==HIGH && c==1 && timeElapsed3 >=10000)
    {      
      timeElapsed3=0;
      c=0;FC2=0;FC3=0;FC4=0;
     }


     if (sensor4==HIGH && d==1 && timeElapsed4 >=10000)
    {      
      timeElapsed4=0;
      d=0;FC3=0;FC4=0;FC5=0;
     }

     if (sensor5==HIGH&& e==1 && timeElapsed5 >=10000)
     {      
      timeElapsed5=0;
      e=0;FC3=0;FC4=0;FC5=0;
     }

     

}
}
else //if the timing fall between 7am to 7pm 
{
 if(event.light > 550)
  {
    analogWrite(LED1, map(20,20,100,0,255));
    analogWrite(LED2, map(20,20,100,0,255));
    analogWrite(LED3, map(20,20,100,0,255));
    analogWrite(LED4, map(20,20,100,0,255));
    analogWrite(LED5, map(20,20,100,0,255));
  }
  if(event.light <= 550 && event.light > 450)
  {
    analogWrite(LED1, map(30,20,100,0,255));
    analogWrite(LED2, map(30,20,100,0,255));
    analogWrite(LED3, map(30,20,100,0,255));
    analogWrite(LED4, map(30,20,100,0,255));
    analogWrite(LED5, map(30,20,100,0,255));
  }
  else if(event.light <= 450 && event.light > 200)
  {
    analogWrite(LED1, map(45,20,100,0,255));
    analogWrite(LED2, map(45,20,100,0,255));
    analogWrite(LED3, map(45,20,100,0,255));
    analogWrite(LED4, map(45,20,100,0,255));
    analogWrite(LED5, map(45,20,100,0,255));    
  }
  else if(event.light < 200)
  {
    analogWrite(LED1, map(80,20,100,0,255));
    analogWrite(LED2, map(80,20,100,0,255));
    analogWrite(LED3, map(80,20,100,0,255));
    analogWrite(LED4, map(80,20,100,0,255));
    analogWrite(LED5, map(80,20,100,0,255)); 
  }
}


} //ened of loop
void schedule1()
{
  if(t.hour == 19 && t.min >= 0) //
  {
    analogWrite(LED1, map(70,20,100,0,255));
  }
  else if(t.hour == 20 && t.min >= 0)
  {
    analogWrite(LED1, map(90,20,100,0,255));
  }
  else if(t.hour == 21 && t.min >= 0)
  {
    analogWrite(LED1, map(25,20,100,0,255));
  }
  else if(t.hour == 22 && t.min >= 0)
  {
    analogWrite(LED1, map(70,20,100,0,255));
  }
  else if(t.hour == 23 && t.min >= 0)
  {
    analogWrite(LED1, map(60,20,100,0,255));
  }
  else if(t.hour == 00 && t.min >= 0)
  {
    analogWrite(LED1, map(40,20,100,0,255));
  }
  else if(t.hour == 01 && t.min >= 0)
  {
    analogWrite(LED1, map(30,20,100,0,255));
  }
  else if(t.hour == 02 && t.min >= 0)
  {
    analogWrite(LED1, map(30,20,100,0,255));
  }
    else if(t.hour == 03 && t.min >= 0)
  {
    analogWrite(LED1, map(30,20,100,0,255));
  }
    else if(t.hour == 04 && t.min >= 0)
  {
    analogWrite(LED1, map(40,20,100,0,255));
  }
    else if(t.hour == 05 && t.min >= 0)
  {
    analogWrite(LED1, map(80,20,100,0,255));
  }
    else if(t.hour == 06 && t.min >= 0)
  {
    analogWrite(LED1, map(80,20,100,0,255));
  }
}
void schedule2()
{
  if(t.hour == 19 && t.min >= 0) //
  {
    analogWrite(LED2, map(70,20,100,0,255));
  }
  else if(t.hour == 20 && t.min >= 0)
  {
    analogWrite(LED2, map(90,20,100,0,255));
  }
  else if(t.hour == 21 && t.min >= 0)
  {
    analogWrite(LED2, map(25,20,100,0,255));
  }
  else if(t.hour == 22 && t.min >= 0)
  {
    analogWrite(LED2, map(70,20,100,0,255));
  }
  else if(t.hour == 23 && t.min >= 0)
  {
    analogWrite(LED2, map(60,20,100,0,255));
  }
  else if(t.hour == 00 && t.min >= 0)
  {
    analogWrite(LED2, map(40,20,100,0,255));
  }
  else if(t.hour == 01 && t.min >= 0)
  {
    analogWrite(LED2, map(30,20,100,0,255));
  }
  else if(t.hour == 02 && t.min >= 0)
  {
    analogWrite(LED2, map(30,20,100,0,255));
  }
    else if(t.hour == 03 && t.min >= 0)
  {
    analogWrite(LED2, map(30,20,100,0,255));
  }
    else if(t.hour == 04 && t.min >= 0)
  {
    analogWrite(LED2, map(40,20,100,0,255));
  }
    else if(t.hour == 05 && t.min >= 0)
  {
    analogWrite(LED2, map(80,20,100,0,255));
  }
    else if(t.hour == 06 && t.min >= 0)
  {
    analogWrite(LED2, map(80,20,100,0,255));
  }
}
void schedule3()
{
  if(t.hour == 19 && t.min >= 0) //
  {
    analogWrite(LED3, map(70,20,100,0,255));
  }
  else if(t.hour == 20 && t.min >= 0)
  {
    analogWrite(LED3, map(90,20,100,0,255));
  }
  else if(t.hour == 21 && t.min >= 0)
  {
    analogWrite(LED3, map(25,20,100,0,255));
  }
  else if(t.hour == 22 && t.min >= 0)
  {
    analogWrite(LED3, map(70,20,100,0,255));
  }
  else if(t.hour == 23 && t.min >= 0)
  {
    analogWrite(LED3, map(60,20,100,0,255));
  }
  else if(t.hour == 00 && t.min >= 0)
  {
    analogWrite(LED3, map(40,20,100,0,255));
  }
  else if(t.hour == 01 && t.min >= 0)
  {
    analogWrite(LED3, map(30,20,100,0,255));
  }
  else if(t.hour == 02 && t.min >= 0)
  {
    analogWrite(LED3, map(30,20,100,0,255));
  }
    else if(t.hour == 03 && t.min >= 0)
  {
    analogWrite(LED3, map(30,20,100,0,255));
  }
    else if(t.hour == 04 && t.min >= 0)
  {
    analogWrite(LED3, map(40,20,100,0,255));
  }
    else if(t.hour == 05 && t.min >= 0)
  {
    analogWrite(LED3, map(80,20,100,0,255));
  }
    else if(t.hour == 06 && t.min >= 0)
  {
    analogWrite(LED3, map(80,20,100,0,255));
  }
}
void schedule4()
{
  if(t.hour == 19 && t.min >= 0) //
  {
    analogWrite(LED4, map(70,20,100,0,255));
  }
  else if(t.hour == 20 && t.min >= 0)
  {
    analogWrite(LED4, map(90,20,100,0,255));
  }
  else if(t.hour == 21 && t.min >= 0)
  {
    analogWrite(LED4, map(25,20,100,0,255));
  }
  else if(t.hour == 22 && t.min >= 0)
  {
    analogWrite(LED4, map(70,20,100,0,255));
  }
  else if(t.hour == 23 && t.min >= 0)
  {
    analogWrite(LED4, map(60,20,100,0,255));
  }
  else if(t.hour == 00 && t.min >= 0)
  {
    analogWrite(LED4, map(40,20,100,0,255));
  }
  else if(t.hour == 01 && t.min >= 0)
  {
    analogWrite(LED4, map(30,20,100,0,255));
  }
  else if(t.hour == 02 && t.min >= 0)
  {
    analogWrite(LED4, map(30,20,100,0,255));
  }
    else if(t.hour == 03 && t.min >= 0)
  {
    analogWrite(LED4, map(30,20,100,0,255));
  }
    else if(t.hour == 04 && t.min >= 0)
  {
    analogWrite(LED4, map(40,20,100,0,255));
  }
    else if(t.hour == 05 && t.min >= 0)
  {
    analogWrite(LED4, map(80,20,100,0,255));
  }
    else if(t.hour == 06 && t.min >= 0)
  {
    analogWrite(LED4, map(80,20,100,0,255));
  }
}
void schedule5()
{
  if(t.hour == 19 && t.min >= 0) //
  {
    analogWrite(LED5, map(70,20,100,0,255));
  }
  else if(t.hour == 20 && t.min >= 0)
  {
    analogWrite(LED5, map(90,20,100,0,255));
  }
  else if(t.hour == 21 && t.min >= 0)
  {
    analogWrite(LED5, map(25,20,100,0,255));
  }
  else if(t.hour == 22 && t.min >= 0)
  {
    analogWrite(LED5, map(70,20,100,0,255));
  }
  else if(t.hour == 23 && t.min >= 0)
  {
    analogWrite(LED5, map(60,20,100,0,255));
  }
  else if(t.hour == 00 && t.min >= 0)
  {
    analogWrite(LED5, map(40,20,100,0,255));
  }
  else if(t.hour == 01 && t.min >= 0)
  {
    analogWrite(LED5, map(30,20,100,0,255));
  }
  else if(t.hour == 02 && t.min >= 0)
  {
    analogWrite(LED5, map(30,20,100,0,255));
  }
    else if(t.hour == 03 && t.min >= 0)
  {
    analogWrite(LED5, map(30,20,100,0,255));
  }
    else if(t.hour == 04 && t.min >= 0)
  {
    analogWrite(LED5, map(40,20,100,0,255));
  }
    else if(t.hour == 05 && t.min >= 0)
  {
    analogWrite(LED5, map(80,20,100,0,255));
  }
    else if(t.hour == 06 && t.min >= 0)
  {
    analogWrite(LED5, map(80,20,100,0,255));
  }
}
SoftwareSerial Xbee(1,0);

It is stupid to be doing software serial on the hardware serial pins.

  Serial.begin(9600);

It is IMPOSSIBLE to do software serial on the hardware serial pins while also using them to do software serial.

Since you have a fundamental flaw in your program, it makes no sense to look at any of the rest of the code.

When you have a long program just post your .ino file as an attachment.

You have an incredibly long-winded coding style and very meaningless variable names so I can't figure what you are trying to achieve.

If you make greater use of functions (with meaningful names) it will be easier for you and us to see the big picture. Have a look at Planning and Implementing a Program

You have some very complex IF statements which are easy to get wrong.

You seem to update the LEDs in the function schedule1() and then update them again a little later in loop() in the lines following line 190.

...R

I agree with what Robin2 wrote. The first thing I would do if this was my project is to simplify the code.

Using map() with constants does not make much sense. The line:

map(70, 20, 100, 0, 255)

will be 159 all day and all night, so why not just use 159? Looking at schedule1() it tests if t.min is greater than zero, you could check that first and not do anything if it is.

I might do something like this:

void schedule1()
{
  int ledVal = 0;

  if ( t.min != 0 )
  {

    switch (t.hour)
    {

      case 19:
      case 22:
        ledVal = 159;
        break;

      case 20:
        ledVal = 223;
        break;

      default:
        break;
    }

    analogWrite(LED1, ledVal);

  }

to make it more readable and easier to troubleshoot. If you wanted to print the value written to analogWrite() to the serial monitor you would only have to add one print statement instead of 12.

Some well placed comments might help people understand what the code is supposed to do and steer you toward a solution.

Well, the same thing but easier to read perhaps.

#include <Arduino.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
#include <dht.h>
#include <DS3231.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <elapsedMillis.h>

#include<SoftwareSerial.h>
SoftwareSerial Xbee(1,0);

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
#define DHT22_PIN A8     // DHT 22  (AM2302) - what pin we're connected to
#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
// Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
LiquidCrystal lcd(45, 43, 41, 39, 37, 35);

dht DHT; //DHT 22 
DS3231  rtc(SDA, SCL);
Time t;
elapsedMillis timeElapsed1;    // used by elapsedmilis example
elapsedMillis timeElapsed2;    // Globally scoped - see comment above
elapsedMillis timeElapsed3;
elapsedMillis timeElapsed4;
elapsedMillis timeElapsed5;
elapsedMillis timeElapsed6;
elapsedMillis TIMEME;
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
int LED4 = 12;
int LED5 = 13;

unsigned char buf[LENG];
int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module

long OnTime=1000;
unsigned long previousMillis=0;
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;
// different intervals for each LED
int intervalLED12 = 5000;
int intervalLED13 = 6000;
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;    

int a,b,c,d,e;
int f,g,h,i,j;
int FC1,FC2,FC3,FC4,FC5;
boolean hello=false;
boolean hell1=false;boolean hell2=false;
int BUTTONPIN=2;
int MOTION1 = A9;
int MOTION2 = A10;
int MOTION3 = A11;
int MOTION4 = A12;
int MOTION5 = A13;

int bitch;
int sensor1;
int sensor2;
int sensor3;
int sensor4;
int sensor5; 

char letter,iphone;
float hum;  //Stores humidity value
float temp; //Stores temperature value

long debouncing_time=15;
volatile unsigned long last_micros;

////////////////////////////
void setup(){
  // Setup Serial connection
  Serial.begin(9600);
  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor
  // Initialize the rtc object
  lcd.begin(16, 2);
  rtc.begin();
  attachInterrupt(digitalPinToInterrupt(BUTTONPIN), BUTTONHERE , RISING);

  pinMode(LED1,OUTPUT); analogWrite(LED1,0);
  pinMode(LED2,OUTPUT); analogWrite(LED2,0);
  pinMode(LED3,OUTPUT); analogWrite(LED3,0);
  pinMode(LED4,OUTPUT); analogWrite(LED4,0);
  pinMode(LED5,OUTPUT); analogWrite(LED5,0);
  
  pinMode (MOTION1,INPUT);  pinMode (MOTION2,INPUT);
  pinMode (MOTION3,INPUT);  pinMode (MOTION4,INPUT);
  pinMode (MOTION5,INPUT);

  a=0; b=0; c=0; d=0; e=0;
  FC1=0; FC2=0; FC3=0; FC4=0; FC5=0;

  // The following lines can be uncommented to set the date and time
  rtc.setDOW(SATURDAY);     // Set Day-of-Week to SUNDAY
  rtc.setTime(07, 01, 50);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(7, 12, 2017);   // Set the date to January 1st, 2014

  if(!tsl.begin())  {
    /* There was a problem detecting the TSL2561 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }    
  displaySensorDetails();   /* Display some basic information on this sensor */
  configureSensor();        /* Setup the sensor gain and integration time */
}
/////////////////////////////lux sensor configuration//////////////////
void configureSensor(void){
  tsl.enableAutoRange(true);
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
}
void displaySensorDetails(void){
  sensor_t sensor;
  tsl.getSensor(&sensor);
}
////////////////////////////
void loop(){
  sensors_event_t event;
  tsl.getEvent(&event);
  int chk = DHT.read22(DHT22_PIN);
  //Read data and store it to variables hum and temp
  hum = DHT.humidity;
  temp= DHT.temperature;
  //Print temp and humidity values to serial monitor

  Serial.print("Humidity: "); Serial.print(hum);
  Serial.print(" %, Temp: "); Serial.print(temp);
  Serial.println(" Celsius"); Serial.print("lux"); Serial.print(event.light);
  
  t= rtc.getTime();  
  Serial.print(rtc.getDOWStr()); Serial.print(" ");    // Send Day-of-Week
  Serial.print(rtc.getDateStr()); Serial.print("  ");   // Send date 
  Serial.println(rtc.getTimeStr());                     // Send time

//storing the result of the motions sensor into different variables in order to do a condition 
  sensor1 = digitalRead(MOTION1);  sensor2 = digitalRead(MOTION2);
  sensor3 = digitalRead(MOTION3);  sensor4 = digitalRead(MOTION4);
  sensor5 = digitalRead(MOTION5);

if(t.hour >= 19 || t.hour < 7) {//condition for the timing between 7pm to 7am
    if(FC1==0)    { schedule1();      FC1=2; }
    if(FC2==0)    { schedule2();      FC2=2; }
    if(FC3==0)    { schedule3();      FC3=2; }
    if(FC4==0)    { schedule4();      FC4=2; }
    if(FC5==0)    { schedule5();      FC5=2; }
    if (sensor1 == LOW | sensor2== LOW | sensor3 ==LOW | sensor4 == LOW | sensor5 ==0 | FC1==1 | FC2==1 | FC3==1 | FC4==1 | FC5 ==1 | FC1==2 | FC2==2 | FC3==2 | FC4==2 | FC5 ==2)  { 
      if(sensor1==LOW && a==0)    {   
        analogWrite(LED2,255);
        analogWrite(LED1,255);
        a=1; FC1=1; FC2=1; timeElapsed1=0;
      }
      if(sensor2==LOW && b==0)    {   
        analogWrite(LED2,255);
        analogWrite(LED1,255);
        analogWrite(LED3,255);
        b=1; bitch=1; FC1=1; FC2=1; FC3=1;timeElapsed2=0;
      }
      if(sensor3==LOW && c==0)    {   
        analogWrite(LED2,255);
        analogWrite(LED3,255);
        analogWrite(LED4,255);
        c=1;FC2=1; FC3=1; FC4=1;
      }

      if(sensor4==LOW )    {   
        analogWrite(LED3,255);
        analogWrite(LED4,255);
        analogWrite(LED5,255);
        d=1; FC3=1; FC4=1; FC5=1;
      }
      if(sensor5==LOW )    {   
        analogWrite(LED4,255);
        analogWrite(LED5,255);
        e=0; FC4=1; FC5=1;
      }
      if(sensor1==HIGH)     {
        if (sensor1==HIGH && a==1 && ((timeElapsed1 >=10000)>timeElapsed2) && sensor2==HIGH && b==0)    {      
          a=0;
          FC1=0;FC2=0;
        }
      }
      if (sensor2==HIGH && b==1 && (timeElapsed2 >=10000) ){  //no motion detected after detected          
        b=0; hell1=true;
        if(sensor1==HIGH && sensor2==HIGH && sensor3==HIGH) { FC2=0;FC1=0;FC3=0; }
      }
      if (sensor3==HIGH && c==1 && timeElapsed3 >=10000) {timeElapsed3=0; c=0;FC2=0;FC3=0;FC4=0;}
      if (sensor4==HIGH && d==1 && timeElapsed4 >=10000) {timeElapsed4=0; d=0;FC3=0;FC4=0;FC5=0;}
      if (sensor5==HIGH&& e==1 && timeElapsed5 >=10000)  {timeElapsed5=0; e=0;FC3=0;FC4=0;FC5=0;}
    }
  } else { //if the timing fall between 7am to 7pm 
    if(event.light > 550)  {
      analogWrite(LED1, map(20,20,100,0,255));
      analogWrite(LED2, map(20,20,100,0,255));
      analogWrite(LED3, map(20,20,100,0,255));
      analogWrite(LED4, map(20,20,100,0,255));
      analogWrite(LED5, map(20,20,100,0,255));
    }
    if(event.light <= 550 && event.light > 450) {
      analogWrite(LED1, map(30,20,100,0,255));
      analogWrite(LED2, map(30,20,100,0,255));
      analogWrite(LED3, map(30,20,100,0,255));
      analogWrite(LED4, map(30,20,100,0,255));
      analogWrite(LED5, map(30,20,100,0,255));
    } else if(event.light <= 450 && event.light > 200)  {
      analogWrite(LED1, map(45,20,100,0,255));
      analogWrite(LED2, map(45,20,100,0,255));
      analogWrite(LED3, map(45,20,100,0,255));
      analogWrite(LED4, map(45,20,100,0,255));
      analogWrite(LED5, map(45,20,100,0,255));    
    } else if(event.light < 200) {
      analogWrite(LED1, map(80,20,100,0,255));
      analogWrite(LED2, map(80,20,100,0,255));
      analogWrite(LED3, map(80,20,100,0,255));
      analogWrite(LED4, map(80,20,100,0,255));
      analogWrite(LED5, map(80,20,100,0,255)); 
    }
  }
} //ened of loop

As per other people comments,
Working code starts with easy to read code.

Make sure that you indent things correctly to make it easier to follow flow.
Also as you are not adverse to having multiple instructions on the same line
Use the same formatting thru out and try to have as much of the program in one screen.
the less you have to scroll up and down the less confusion.

But nothing much wrong apart from things others have mentioned.

But rethink that big long if condition.
Should that be single | or Double || ?.

Sorry guys for the confusing and messy codes. I'm still learning in the process. Please pardon my work. Here is the modified version of my codes. Right now i'm stuck with the controlling of sensor.
Objective to achieve for the infrared sensor:
for instance: infared sensor from lamp 1 detects movements, LED 1 & 2 should light up for 20seconds. in within the 20seconds, if infared sensor from lamp 2 detect movements, by right it should reset the timer of LED 1 & 2 and let it run for another 20 seconds as well as LED 3. but i'm not very sure how the algorithm will be like. anyone kind soul can show some guidance ?

system.ino (14.9 KB)

Ok, one last major tip.
You can shrink the length of your code by at least 50% by using arrays for you sensors, leds and timers.
It will also become cleaner and more readable

AdenOng:
Here is the modified version of my codes.

It's been a long time.

Tell us exactly what your latest code actually does and what you want it to do that is different.

for instance: infared sensor from lamp 1 detects movements, LED 1 & 2 should light up for 20seconds. in within the 20seconds, if infared sensor from lamp 2 detect movements, by right it should reset the timer of LED 1 & 2 and let it run for another 20 seconds as well as LED 3.

I suspect you could write that a lot more clearly - with one event or decision on each line - then you (and we) could perhaps see the solution easily.

...R

Hi lastchancename,
is it possible to show me rough template on how to use array to do? thank you

Hi Robin,
i would like to use millis() function to set the 20secs once infared detect movement.
however i still thinking of how to override the timer (to reset it and count up to 20secs) when
lamp 2 sensor sense motion 10secs after lamp1 sensor sense motion.

i would like to use millis() function to set the 20secs once infared detect movement.
however i still thinking of how to override the timer (to reset it and count up to 20secs) when
lamp 2 sensor sense motion 10secs after lamp1 sensor sense motion.

The whole point of using millis() for timing is that it allows you to do other things, such as reading inputs and taking action, during the timing period. The action could involve stopping the timing or extending it if that is what you want to do.

Have you read Using millis() for timing. A beginners guide and Several things at the same time

Before we go too far writing code examples, please understand it is unlikely we will help you do it ‘wrongly’.
There may be better ways with optimisation, but we’ll usually try to steer you into understanding what you’re trying to achieve,
.
Seriously, the first code(OP) is a tortuous mush of repeated blocks, and the schedule() functions are almost identical.
You could look at making schedule() accept parameters, then rebuild all the stuff up top to call schedule(x); (or whatever you choose)

With this direction and use of arrays[], i’d guess the whole sketch could be shrunk down to 30-odd lines. But you have to want to do it, then we’ll help you tune it up. Make a start.
If it’s too hard, the work on a smaller piece... get that working effficiently, and we can move forwardfrom that.

P.S. it’s good to see the flowchart, as it means you have looked at compartmentalising the logic. That’s a good place to do the same within schedule(). All the instances are nearly identical - certain,y they can be folded into a single flexible block.