Arduino code with a LED and a Motion PIR

HI, i dont have the matérial (it will come soon) and i wanted to know if this code is good. I want to when the Motion module capture a mouvement it turn on the led, and when it capture a mouvement again, it turn the led off.

int led = 13;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
  else {
      digitalWrite(led, LOW); // turn LED OFF
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW

i wanted to know if this code is good.

No. Look at the state change detection example. You want to know when the state changes from no motion detected to motion detected, and act at that point.

Turning the light on when there is motion, to keep someone from stumbling around in the dark is a good idea.

Turning the light off for the next person doesn't seem too bright.

Perhaps you could explain why you have those requirements.