if..else HELP

so the sensor , when someone passes makes the led turn on, and when no motion is present the led stays LOW. but when someone passes , it stays on for about 8 sec. i want it to stay on for about 1 or 2 sec.. how do i modify this code to do that.

int pir=3;
int pirState=LOW ;
int led =4;


 void setup()
 
 {
   pinMode (pir ,INPUT);// pir sensor
   pinMode ( led , OUTPUT);//led 
 }
 
 void loop (){

   if (digitalRead(pir) == HIGH) {
  digitalWrite(led, HIGH);
} else {
  digitalWrite(led, LOW);
  
}}

your code loops very fast maintaining the led power as the input is high, you need to modify it to say,

set a flag when the input starts high, turn on led, continue looping.
If input is still high, check time led has been on.
If on to long turn off led and wait for low input to reset.
if on too short, leave on and keep looping.

using millis and a few variables you can store timings and state flags.

It stays on as long as the PIR reads HIGH. You might want to check how you have the PIR wired as to why it's taking so long to go back to LOW. Really, it looks like a hardware problem.

void loop (){

   if (digitalRead(pir) == HIGH) {
  digitalWrite(led, HIGH);
} else {
  digitalWrite(led, LOW);
  
}}

Looks like this was hunt-and-pecked by a blind person. The Tools menu has a really cool feature - Auto format. Give it a try. I think you'll like it. We will, for sure.