Hey, I'm new to arduino (just started last week) and I don't really know if this is the right subcategory or not so sorry beforehand. I'm currently making my school project that's due in less than 3 days so any help is greatly appreciated.
I'm using arduino uno board, breadboard, ultrasonic sensor, and mg996r motor servo and the problem is it only works after reset or if I unplug then plug back the arduino to my laptop. It'd move once or twice then stop moving at all. At first I thought there was a problem on the arduino but the sensor seems to work fine. The thing is my motor servo was working normally the day before so I have no clue what happened. I checked the wiring and all, even remade the whole thing for precaution.
Also, the servo sometimes made noises and sometimes not at all, it'd also vibrate sometimes. I changed the servo using sg90 instead but the same issue follows. I changed arduino board and same thing happened.
Here's the code I use, it's from the video I followed
#include <Servo.h>
Servo servo;
int angle = 10;
// defines pins numbers
const int trigPin = 12;
const int echoPin = 11;
// defines variables
long duration;
int distance;
void setup() {
servo.attach(8);
servo.write(angle);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(10);
if(distance<30)
{
servo.write(160);
delay(4000);
}
else
{
servo.write(0);
}
}
If the issue can be solved quickly, I'm also planning to add lcd with i2c so any tips are appreciated. Sorry if my question sucks, english isn't my first language.

