Need Help My Code Is Not Working As It intended

int in1 = 2;
int in2 =3;
int pir_sensor = 8;
void setup() {
  // put your setup code here, to run once:
  pinMode(pir_sensor, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int value = digitalRead(pir_sensor);
  if (value == HIGH){
    Serial.println(value);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    delay(2000);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    delay(2000);
  }
  else{
    Serial.println(value);
  }
}

In the above code in1 and in2 are the inputs of motor drive when i place my hand near PIR Sensor its value become 1 and if statement start but it is looping over if system and its value remains 1 for quite some time even removing my hand. Also polarity of motor is not changing…

Hello, ¿Can you post some pictures of the hardware setup (micro, PIR, motors, connections)? ¿What PIR sensor are you using?

The problem is with variable value. Its never changes its state once the if statement is true. Make the following changes.

  bool value = digitalRead(pir_sensor);

  if (value == HIGH){
    value = LOW;
    Serial.println(value);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    delay(2000);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    delay(2000);
  }

If you r coding in arduino ide no need of value = low

Because the program will run line by line
When it come out of if statement it will again read the line" bool value = digitalRead(pir_sensor)" it will make the changes

I hop you get my point

The main problem in the code was the was you difine the variable
In before code they difine as “int”
*Int" can store -32,768 to 32,767 as it’s value

In if statement you r using bool value
Bool can only store 1 or 0 in it. means (HIGH OR LOW) value in it

I hope you understand what i mean

But definitions of HIGH and LOW macros in Arduino.h are:

#define HIGH 0x1
#define LOW 0x0

And in C++ standard: “if either operand is unsigned, the other shall be converted to unsigned.”, then comparison “if (value == HIGH)” should not be a problem.

On the other hand, I suggest to move the declaration of variable ‘value’ in the setup section and to do assignments to ‘value’ in the loop . In order to avoid redefinitions of ‘value’ variable.
Maybe function delay is not the best way to generate PWM signals to control motors. Furtheremore If you are using an H-Bridge to control motor you need a “dead time” between the polarity change.

As per knowledge you use true or false with bool, high and low works well with int as you can even use 1 for high and 0 for low. So the problem was the value of pir sensor was high after the if statement was true and it was never set to low again.