Need help with programming, help needed to rectify errors

so what i wanna do is use us sensor hc-sr04 to detect waste nearby and then go near it. but the code is not working and i dont know why. please help quick
here's my code

#include <Servo.h>
Servo armek;
Servo armdo;
long duration2, distance2, BackSensor, FrontSensor;
char data = 0;
int dist1 = 0;
int dist2 = 0;
int sensor = 2;
int state1 = LOW;
int val = 0;
int detect ;
int motor1Pin1 = 22;
int motor1Pin2 = 23;
int motor2Pin1 = 24;
int motor2Pin2 = 25;
int motor3Pin1 = 28;
int motor3Pin2 = 29;
int motor4Pin1 = 30;
int motor4Pin2 = 31;
int state2;
int trigPin1 = 5;
int echoPin1 = 6;
int trigPin2 = 7;
int echoPin2 = 8;
int var1;
int myvar;


void Forward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, HIGH);
}

void Reverse() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
}

void Left() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

void Right() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
}

void Pick() {
  armek.write(150);
  armdo.write(150);
}

void Free () {
  armek.write(0);
  armdo.write(0);
}


void Up() {
  digitalWrite(motor3Pin1, HIGH);
  digitalWrite(motor3Pin2, LOW);
  digitalWrite(motor4Pin1, HIGH);
  digitalWrite(motor4Pin2, LOW);
}

void Down() {
  digitalWrite(motor3Pin1, LOW);
  digitalWrite(motor3Pin2, HIGH);
  digitalWrite(motor4Pin1, LOW);
  digitalWrite(motor4Pin2, HIGH);
}

void StopWheel() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

void StopArm() {
  digitalWrite(motor3Pin1, LOW);
  digitalWrite(motor3Pin2, LOW);
  digitalWrite(motor4Pin1, LOW);
  digitalWrite(motor4Pin2, LOW);
}
void setup()
{
  pinMode(sensor, INPUT);
  pinMode(motor1Pin1, OUTPUT), pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT), pinMode(motor2Pin2, OUTPUT);
  pinMode(motor3Pin1, OUTPUT), pinMode(motor3Pin2, OUTPUT);
  pinMode(motor4Pin1, OUTPUT), pinMode(motor4Pin2, OUTPUT);
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
}


void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration2 = pulseIn(echoPin, HIGH);
  distance2 = (duration2 / 2) / 29.1;
}

void loop() {

  var1 == true;

  if (var1 == true)
  {
    SonarSensor(trigPin1, echoPin1);
    FrontSensor = dist1;
    SonarSensor(trigPin2, echoPin2);
    BackSensor = dist2;

    Serial.println(FrontSensor);
    Serial.print(" - ");
    Serial.println(BackSensor);
    Serial.print(" - ");

    if (dist2 > 5) {
      Left();
      Left();
    }

    while (dist1 > 2) {
      Forward();
    }
    if (dist1 == 2) {
      StopWheel();
      Down();

      Pick();

      Up();

      Free();

      Left();

      Forward();

    }


    val = digitalRead(sensor);
    if (val == HIGH)
    {
      Right();

      Forward();

      Left();

      Forward();

      Left();

      Forward();

      Right();

      Forward();
    }
  }
  if (state1 == LOW) {
    myvar == true;
  }

  if (myvar == true) {
    Serial.println("Motion detected!");
    state1 = HIGH;
  }

  else {
    while (dist1 > 2) {
      Forward();
    }
  }

  if (state1 == HIGH) {
    Serial.println("Motion stopped!");
    state1 = LOW;       // update variable state1 to LOW
  }
}[\code]

"the code is not working" is not a helpful description of your problem. You'll be much more likely to get help here if you explain exactly what you mean by that.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

int dist1;
int dist2;
    SonarSensor(trigPin1, echoPin1);
    FrontSensor = dist1;
    SonarSensor(trigPin2, echoPin2);
    BackSensor = dist2;

But the value of dist1 and dist2 is not changed anywhere in the program.

When you have problems with if or while try printing the value of variables that are being tested. Are they what you expect ?

I also notice

void loop()
{
  var1 == true;
  if (var1 == true)

Why attempt to set a variable to a value then immediately test whether it has that value ? Note that I said attempt. Did you mean to use = rather than == ? If not, then what were you trying to do ?

i have re uploaded my program.please check it. also to uk helibob, i used a Boolean variable to run this portion of code when var1 is true. i dont know whether to use = or ==. i want to stop running this ultrasonic code when the pir sensor detects a human and you can find this code in the myvar==true. please rectify my errors

You use a single = for an assignment, a double = for a compare (e.g. if).

Please do not update posts after you had replies. Instead, add e.g. new code to a new post.

i have re uploaded my program

So now you have changed the code that I commented on and from which I copied some sections which means that my comments do not make sense. That is not a very friendly thing to do.

As far as I can see you have made no significant changes
dist1 and dist2 are still never changed by the program

If you want a block of code to run under some circumstances then you must use an if or while

if (this == true)
  {
    //run the code here
  }
while (this == true)
  {
     //run the code here
  }

uk helibob im sorry i didnt know im new here. how do i change dist1 and dist2 please help

    SonarSensor(trigPin1, echoPin1);
    FrontSensor = dist1;

Seems to be an attempt to call the SonarSensor() function and assign the distance measured to dist1

The function sets the global (available anywhere in the program) distance2 variable to the distance measured. So you could set FrontSensor and BackSensor to distance2 after calling the function with the appropriate parameters

Note that you still need to fix the logic at the start of the loop() function

thank you uk helibob. but i didnt understand what logic you were talking about

This logic

void loop()
{
  var1 == true;
  if (var1 == true)

You are comparing var1 with true and ignoring the result. What is the first line meant to do ?

thanks, i fixed it. i understood that == is used to compare in if or while loops. and = is used for assignment.