I don't know how can I make my program not affected by "delay"

I want to make 4 of my lights to lit up simultaneously but the delay causes other lights to blink once another one was triggered, pls I need ur help :frowning:

#include <SoftwareSerial.h>

int LDR = A0; // select the input pin for the LDR
int LDRvalue = 0; // variable to store the value coming from the sensor
int bulb1 = 12;
int bulb2 = 11;
int bulb3 = 10;
int bulb4 = 9;
int PIR1 = A1;
int PIR2 = A2;
int PIR3 = A3;
int PIR4 = A4;
int PIR1value = LOW;
int PIR2value = LOW;
int PIR3value = LOW;
int PIR4value = LOW;

void setup() { // declare the ledPin as an OUTPUT:

pinMode(bulb1, OUTPUT);
pinMode(bulb2, OUTPUT);
pinMode(bulb3, OUTPUT);
pinMode(bulb4, OUTPUT);
pinMode(PIR1, INPUT);
pinMode(PIR2, INPUT);
pinMode(PIR3, INPUT);
pinMode(PIR4, INPUT);
Serial.begin(9600); }

void loop()

{

Serial.println("Welcome");

LDRvalue = analogRead(LDR);
PIR1value = digitalRead(PIR1);
PIR2value = digitalRead(PIR2);
PIR3value = digitalRead(PIR3);
PIR4value = digitalRead(PIR4);


if (LDRvalue < 350 && PIR1value == HIGH)

{

digitalWrite(bulb1,LOW);
delay(1000);

}

if (LDRvalue < 350 && PIR2value == HIGH)

{

digitalWrite(bulb2,LOW);
delay(1000);

}

if (LDRvalue < 350 && PIR3value == HIGH)

{

digitalWrite(bulb3,LOW);
delay(1000);
}

if (LDRvalue < 350 && PIR4value == HIGH)

{

digitalWrite(bulb4,LOW);
delay(1000);
}



digitalWrite(bulb1,HIGH);
digitalWrite(bulb2,HIGH);
digitalWrite(bulb3,HIGH);
digitalWrite(bulb4,HIGH);

delay (0);

}

Please explain how you want the program to behave.

I want the lights to lit up without affecting other lights. I am using 4 channel relay, when channel 1 conditions were triggered, triggering other channels will make it blink.

One word: BlinkWithoutDelay :wink:

I have seen that but i dont know how can I integrate it with my code

Where is the problem? Do you get the principle behind blink without delay? Probably not, so start out by reading about that, playing with that etc. And when you do understand come back to this project.

You say to disregard the delay()'s but they are precisely the problem.
During a delay() everything stops until it's over and the button can't be read until the program works thru all the delays to get back to the start of loop().

These are all good tutorials on how to manage timing in ways that don't stop other stuff from getting done.
Blink Without Delay https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Demonstration code for several things at the same time Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Multi-tasking the Arduino - Part 1 Overview | Multi-tasking the Arduino - Part 1 | Adafruit Learning System

Using these techniques instead of doing nothing waiting for delays the arduino can do something useful(like reading the button) while it's waiting to change the light.

I want the lights to lit up without affecting other lights. I am using 4 channel relay, when channel 1 conditions were triggered, triggering other channels will make it blink.

If you're powering the 4 relay board from the Arduino, then turning on more than 1 relay would also cause other lights to blink.

(P.S. make sure you're using a separate power supply for your relay board and make sure it's wired correctly)

I’ve tried to integrate the blinkwithnodelay, but I think I am doing it wrong, thanks for your replies BTW. I am really new with this.

 #include <SoftwareSerial.h>

int LDR = A0; 
int LDRvalue = 0; 
int bulb1 = 12;
int bulb2 = 11;
int bulb3 = 10;
int bulb4 = 9;
int PIR1 = A1;
int PIR2 = A2;
int PIR3 = A3;
int PIR4 = A4;
int PIR1value = LOW;
int PIR2value = LOW;
int PIR3value = LOW;
int PIR4value = LOW;
unsigned long prevbulb1=0;
unsigned long prevbulb2=0;
unsigned long prevbulb3=0;
unsigned long prevbulb4=0;
int interbulb = 500;


void setup() {

pinMode(bulb1, OUTPUT);
pinMode(bulb2, OUTPUT);
pinMode(bulb3, OUTPUT);
pinMode(bulb4, OUTPUT);
pinMode(PIR1, INPUT);
pinMode(PIR2, INPUT);
pinMode(PIR3, INPUT);
pinMode(PIR4, INPUT);
Serial.begin(9600); }

void loop()

{

Serial.println("Welcome");

LDRvalue = analogRead(LDR);
PIR1value = digitalRead(PIR1);
PIR2value = digitalRead(PIR2);
PIR3value = digitalRead(PIR3);
PIR4value = digitalRead(PIR4);
unsigned long currentMillis = millis();



if ((unsigned long)(currentMillis - prevbulb1) >= interbulb && LDRvalue < 350 && PIR1value == HIGH)

{

digitalWrite(bulb1,LOW);
prevbulb4 = currentMillis;

}


  
if ((unsigned long)(currentMillis - prevbulb2) >= interbulb && LDRvalue < 350 && PIR2value == HIGH)

{

digitalWrite(bulb2,LOW);
prevbulb3 = currentMillis;

}



if ((unsigned long)(currentMillis - prevbulb3) >= interbulb && LDRvalue < 350 && PIR3value == HIGH)

{

digitalWrite(bulb3,LOW);
prevbulb2 = currentMillis;

}



if ((unsigned long)(currentMillis - prevbulb4) >= interbulb && LDRvalue < 350 && PIR4value == HIGH)

{

digitalWrite(bulb4,LOW);
prevbulb1 = currentMillis;

}


digitalWrite(bulb1,HIGH);
digitalWrite(bulb2,HIGH);
digitalWrite(bulb3,HIGH);
digitalWrite(bulb4,HIGH);


}

Use this then paste your code, it will then display correctly.
Let me rephrase … Arduino’s on board regulator does not yet have “Ludicrous Mode” as does Tesla’s latest model S.

This helps to get ones get attention.

Please use code tags when posting code.

  1. Type
    ** **[code]** **
    .
  2. Paste your code.
  3. Type
    ** **[/code]** **
    .

The result will look like below

your code here

At the end of the loop you write all your bulbs HIGH (I don't know if that is ON or OFF). So a few microseconds after you have made a bulb LOW, it will be set high again.

Can you please give a very clear description of what you want your code to do. Somewhere you state that you want them all to be on (is that HIGH or LOW?). Under which conditions must that happen? What do you want to happen net? They switch off one by one if a certain condition is met? What is that condition?