I’m new. Please help me with this coding

I want to control servo with ultrasonic sensor that when ultrasonic sensor detect something then servo keep running for 1 minute. this is my code

#include <Servo.h>
#define trigPin 12
#define echoPin 11
Servo servo0;

int pos = 70;
int sound = 250;
int i = 0;
int sens;

// Variables will change:
//int servo0 = LOW;

unsigned long previousMillis = 0;

const long interval = 10000;

void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo0.attach(9);
}
void loop()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    if (distance < 69)
    {
      Serial.println("the distance is less than 69");
      for (pos = 70; pos <= 120; pos += 1)
      {
        // in steps of 1 degree
        servo0.write(pos);
        delay(20);
      }
      for (pos = 120; pos >= 70; pos -= 1)
      {
        servo0.write(pos);
        delay(20);
      }
    }
    else
    {
      servo0.write(70);
      Serial.print(distance);
      Serial.println(" cm");
    }
    if (distance = 90 || distance <= 90)
    {
      Serial.println("The distance is 90");
    }
    else
    {
      Serial.print(distance);
      Serial.println(" cm");
    }
    //delay(9);
  }
}

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

On this occasion I have added them for you and have also Auto formatted the code in the IDE

What do you mean

What should the servo do during the minute ?
What does the sketch do now ?

not doing anything now..!

Does the servo work if you run the Sweep example ?

yes

It should run for one minute once detected by ultrasonic

Do you see any output on the Serial monitor ?

What is the significance of the 10 second period timer ?
Where in the code is the 1 minute time period implemented ?

I note that you have a for loop of 50 steps, each of which takes 20 milliseconds and you do it twice which could mess up your overall timing if you do it wrong

no don't see any output

Then the value of distance is never less than 69

Print the value of distance immediately after you calculate it. What values do you get ?

Also, I see this error in your code which will set distance to 90 rather than comparing it with 90

    if (distance = 90 || distance <= 90)

You still haven't said what the servo is supposed to be doing for a whole minute. Servos generally go from one position to another position and it takes a lot less than a minute.

Steve

It should rotate from 70 to 120 and 120 to 70 for one minute

Feedback please on the value of distance when it is measured, fix the bug that I noted and explain how you think the timing works in the sketch

That's the actual code. In this code the servo is rotated once by detecting any object by ultrasonic. and I want to rotate the servo from max to max time once detected through ultrasonic.

#include <Servo.h>
#define trigPin 12
#define echoPin 11
Servo servo0;

int pos = 70;
int sound = 250;
int i = 0;
int sens;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo0.attach(9);
}
void loop(){

long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance < 69) {
Serial.println("the distance is less than 69");
for (pos = 70; pos <= 120; pos += 1) {
// in steps of 1 degree
servo0.write(pos);
delay(20);
}
for (pos = 120; pos >= 70; pos -= 1) {
servo0.write(pos);
delay(20);
}
}
else {
servo0.write(70);
Serial.print(distance);
Serial.println(" cm");

}

if (distance = 90 || distance <= 90){
Serial.println("The distance is 90");

}

else {
Serial.print(distance);
Serial.println(" cm");
}
delay(5);
}

#include <Servo.h>
#define trigPin 12
#define echoPin 11
Servo servo0;

int pos = 70;
int sound = 250;
int i = 0;
int sens;
long duration, distance;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo0.attach(9);
  servo0.write(pos);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance < 69) {
    Serial.println("the distance is less than 69");
    for (pos = 70; pos <= 120; pos += 1) {
      // in steps of 1 degree
      servo0.write(pos);
      delay(20);
    }
    for (pos = 120; pos >= 70; pos--) {
      servo0.write(pos);
      delay(20);
    }
  }
  else servo0.write(70);
  Serial.print(distance);
  Serial.println(" cm");
  delay(50);
}

that's doing the same work like my code

And Serial Port say what?

measuring distance

please read it

where?