Code Stuck In Loops

So basically, I am very new to arduino and have been creating a nerf gun that, using an ultrasonic sensor, powers up its motors, pushes its dart with a servo, then powers down the motors again.

Except the motors just speed up and the servo keeps moving back and forth.

Any help would be appreciated, here is my code:

#include <Servo.h> 
#include <NewPing.h>-====
bool targeted = false;
int ledPin =(13); // Add the onboard LED on pin 13.
int trigPin = (10); // Add the Trig pin on pin 10.
int echoPin = (9); // Add the ECHO pin on pin 9.
int gunPin = (4); //nerf gun power up
int duration, distance; // Add types 'duration' and 'distance'.
int servoPin = 3; 
// Create a servo object 
Servo Servo1; 
void setup()
{
pinMode (ledPin, OUTPUT); //
pinMode (trigPin, OUTPUT);// the TRIG pin will send the ultrasonic wave.
pinMode (echoPin, INPUT); // The ECHO pin will recieve the rebounded wavetype.
Servo1.attach(servoPin);
pinMode (gunPin, OUTPUT);
}
void loop()
{
distance = (1000);
digitalWrite (ledPin, LOW); // Here, LOW means off and HIGH means on.
digitalWrite (gunPin, LOW);
digitalWrite (trigPin, HIGH);
delay(50);
digitalWrite (trigPin, LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
if(distance <=150) // If the sensor detects an obstacle less than 30 cm in distance, the LED will start to blink.
digitalWrite (ledPin, HIGH);
shoot32();
delay(50);
if(distance >=30)// If no obstacle is there within 30 cm, the Led should turn off.
digitalWrite (ledPin, LOW);
digitalWrite (gunPin, LOW);
delay(50);
Serial.print("cm"); 
Serial.println(distance);
}
int shoot(){
digitalWrite(gunPin, HIGH);
delay(1000);
Servo1.write(0); 
delay(1000); 
// Make servo go to 90 degrees 
Servo1.write(180); 
delay(1000); 
digitalWrite(gunPin, LOW);
}

The code you posted does not compile

No shoot32() function for a start

Hello Maxlzac

Are you sure that your "if" statements are correctly written?

If ther were more comments in your program, it should be easier to understand your desired logic.

Regards,
bidouilleelec

I suspect.that the code after your if statement is missing some { and }
This will only conditionally execute the digitalWrite but will always execute shoot32(). That is, once it exists.

if(distance <=150) // If the sensor detects an obstacle less than 30 cm in distance, the LED will start to blink.
digitalWrite (ledPin, HIGH);
shoot32();