Need to know about functions

Okay, Hi. Its my first time using this forum and i'm a 12 year old. i have basic knowledge of arduino and am making a project for a competition. So i need answer to 2 things;

  1. Can someone please check my code and tell me if it would hypothetically work?
    My project is a smart dustbin which uses an arduino mega,ultrasonic sensor and a pir sensor. it basically seeks out garbage and it has 3d printed arms which pick up the garbage. and also please suggest any corrections. here's the code
#include <Servo.h>
Servo armek;
Servo armdo;
long duration2, distance2, BackSensor, FrontSensor;
char data = 0;
int sensor = 2;
int state = 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 flag = 0;
long duration, dist, average;
long aver[3];
int trigPin1 = 5;
int echoPin1 = 6;
int trigPin2 = 7;
int echoPin2 = 8;
void Forward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
}

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 measure() {
  digitalWrite(10, HIGH);
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(15);
  digitalWrite(trigPin1, LOW);
  pinMode(echoPin1, INPUT);
  duration = pulseIn(echoPin1, HIGH);
  dist = (duration / 2) / 29.1;  //obtain distance
}

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 Empty()
{
  

void loop() {

  SonarSensor(trigPin1, echoPin1);
  FrontSensor = distance2;
  SonarSensor(trigPin2, echoPin2);
  BackSensor = distance2;

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


  for (int i = 0; i <= 2; i++) {
    measure();
    aver[i] = dist;
    delay(10);
  }
  dist = (aver[0] + aver[1] + aver[2]) / 3;
  while (dist > 10) {
    Forward();
  }
  if (dist == 10)
    StopWheel();
  Down();
  Pick();
  Up();
  Free();
  Left();
  Forward();
  if (Serial.read()== "Motion detected!")
  {
    Empty();

  Serial.print(dist);


  if (Serial.available() > 0) // Send data only when you receive data:
  {
    data = Serial.read();      //Read the incoming data and store it into variable data
    Serial.print(data);        //Print Value inside data in Serial monitor
    Serial.print("\n");        //New line
    if (data == '1')           //Checks whether value of data is equal to 1
      for (int i = 0; i > 3; i++) {
        Left();
        delay (500);
        Right();
        delay (500);
      }
  } if (data == '0')
  {
    StopWheel();
    StopArm();
  }

  val = digitalRead(sensor);
  if (val == HIGH)
  {
    delay(1000);
    Right();
    delay(1000);
    Forward();
    delay(1000);
    Left();
    delay (1000);
    Forward();
    delay(1000);
    Left();
    delay(1000);
    Forward();
    delay(1000);
    Right();
    delay(1000);
    Forward();
  }

  if (state == LOW) {
    Serial.println("Motion detected!");
    state = HIGH;
  }
}
else {
  while (dist > 10) {
    Forward();
  }
}

if (state == HIGH) {
  Serial.println("Motion stopped!");
  state = LOW;       // update variable state to LOW
}
}
}
  1. Also, this was my original question, How do we declare an empty function. i need it coz when my pir detects a human, the us should stop for a while, until the dustbin turns around the human. Please answer and Thank You.

If by "work" you mean run, then no.
It won't even compile.

if (Serial.read()== "Motion detected!") Serial.read returns a single character, not a string.

Break your problem down into easily-digestible chunks, and don't go on to the next until you have thoroughly tested the previous one.
You've got far too much code, too soon.

And use pin names.

How do we declare an empty function. i need it coz when my pir detects a human, the us should stop for a while

Do you mean that you want the Arduino to do absolutely nothing for a period of time ?

As to not compiling, what is this all about ?

void Empty()
{
  

void loop() {

Auto format your code in the IDE and the problem may be clearer.

A further suggestion.

Always put { and } on their own line and always use them round conditional code blocks such as while even if there is only one statement to be executed

sir first of all, thank you for replying so quick. i cannot reply as quick because of a 5 minute limit.
uk helibob i do not want arduino to do absolutely nothing. i just need the us sensor to stop functioning for a while. also i did not clearly understand your comment about curly braces around while loops. can u please elaborate? and yeah i missed a curly brace. i added it and now it compiles.
awol so how do i solve my problem? i want the us to stop running when there is this message on the serial monitor. please help!

i do not want arduino to do absolutely nothing. i just need the us sensor to stop functioning for a while.

OK. Make running the code you want to control dependant on a boolean variable and only run it when the boolean is true. Change the boolean from false to true to let it run and back to false to stop it. That leaves the question as to how to let it run for a period without stopping the Arduino from doing other things. The answer is to use millis() for timing.

Save the millis() value at the time that the start action happens and set the boolean to true to allow the controlled code to execute. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then set the boolean to false to stop the controlled code executing. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

As to curly braces around code block, consider this portion of code

while (digitalRead(aPin) == HIGH)
  Serial.println("Pin is HIGH");
  Serial.println("another message");

and this one

while (digitalRead(aPin) == HIGH)
  {
    Serial.println("Pin is HIGH");
    Serial.println("another message");
  }

In each one which message or messages will be printed whilst the pin is HIGH ?

Thank you for explaining the curly braces and the boolean variable idea. But I did not understand the millis() part. Sorry if i'm bothering you but I really didn't understand. Thanks!

But I did not understand the millis() part.

Start by reading Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.