Can someone help me please? pir program

Basically I want} else {to only run once and do nothing, until it returns to true again. . How could I do this?

const int led = 13;
const int sensor = 3;
bool sensor_value ;
bool state = false;

void setup() {

pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);

}
void loop() {

sensor_value = digitalRead(sensor);

if (sensor_value == true) {
state = true;
Serial.println("Motion Detected!");
delay(6000);

}
if (state == true) {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
else

digitalWrite(led, LOW);

delay(5000);
if (sensor_value == false) {
state = false;
Serial.println("Motion Ended!");
}
}

God bless you all.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Put all the code in setup, is the usual reply.

Hi @marquito06

in this code, else run 1 time only.

const int led = 13;
const int sensor = 3;
bool sensor_value ;
bool state = false;
bool flag = true;
//---------------------------------------------
void setup() {
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}
//---------------------------------------------
void loop() {
  sensor_value = digitalRead(sensor);
  if (sensor_value == true)
  {
    state = true;
    Serial.println("Motion Detected!");
    delay(6000);
  }
  if (state == true)
  {
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
  }

  else
  {
    if (flag == true)
    {
      digitalWrite(led, LOW);
      flag = false;
    }
  }

  delay(5000);
  if (sensor_value == false)
  {
    state = false;
    Serial.println("Motion Ended!");
  }
}

Hi @ruilviana , thank you so much for helping me I have tried the one you just send me but }else{ keeps running constantly. What I could do?

Thank you very much, sorry I have not explained my problem well. I want it to only run once when it goes from true to false, and do nothing, until it returns to true again.

Hi
How can you claim that the else is running other times?
It can explain better and in detail the desired functioning of your code.

Yes, basically I want }else{ to only run once and do nothing until it returns to state true to state false again.

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

Okey, thank you and sorry

Thanks so much.

Any time.