HC-SR04 and servo motor for my technical work

Hello everyone!
I'm a beginner in the Arduino world and have chosen a project with the HC-SR04 and servo motor for my technical work as an experimental part.
However, after many attempts, I can't really reach my goal.
My idea was that the servomotor would rotate 90° when you hold an object in front of the sensor. If you take the object away again, the motor should turn back or close.
Could someone help me here?
I'd like to add my ideas here...
Warm greetings

#include <Servo.h>
#include <stdio.h>
Servo servo1;

long zeit=0;
long entfernung1 = 0;
long entfernung2 = 0;
int zu = 5;
int auf = 90;
int anzahl = 2;

void setup() {
  // put your setup code here, to run once:
# define TRIGGER 3 
# define ECHO 2
Serial.begin(115200);
pinMode(TRIGGER,OUTPUT);
pinMode(9,OUTPUT);
servo1.attach(9);
servo1.write(zu);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i=0; i <= anzahl-1; i++){
    zeit=0;
    noInterrupts();                    
    digitalWrite(TRIGGER, LOW);
    delayMicroseconds(13);
    digitalWrite(TRIGGER, HIGH);        
    delayMicroseconds(20);
    digitalWrite(TRIGGER, LOW);
    zeit = pulseIn(ECHO, HIGH);         
    interrupts();
  
for (int i=0; i <= anzahl -1; i++)
{
  servo1.write(zu);
  entfernung2 = entfernung1 / anzahl;
  Serial.println(entfernung2);
  if (entfernung2 > 9550){
    digitalWrite(9, HIGH);
    Serial.println("open");
    servo1.write(auf);
    delay(3000);
    digitalWrite(9, LOW);
    servo1.write(zu);
    delay(1000);
  }
}Vorformatierter Text

Where is the closing

}

Those should be with the rest of the global declarations. Correct and repost the new code.
This

and this

are not needed.
That

and that

will cause responsiveness issues.

This for loop

is missing the }.

Oh I forgot but added now.

Faulty logic, here. How would you detect removing the object if the HC-SR04 was already moved completely out of range? Or are you implying the object kept moving with the sensor?

I thought that if you put your hand in front of the sensor, for example, the sensor opens the servo and if you remove it, the servo closes again.

You can have it do that.

Oh, I thought you were doing like so many and moving the sensor with the servo.

I don´t know. But is it right like this?

#include <Servo.h>
#include <stdio.h>
Servo servo1;

long zeit=0;
long entfernung1 = 0;
long entfernung2 = 0;
int zu = 5;
int auf = 90;
int anzahl = 2;

# define TRIGGER 3 
# define ECHO 2

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
pinMode(TRIGGER,OUTPUT);
pinMode(9,OUTPUT);
servo1.attach(9);
servo1.write(zu);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i=0; i <= anzahl-1; i++){
    zeit=0;                   
    digitalWrite(TRIGGER, LOW);
    delayMicroseconds(13);
    digitalWrite(TRIGGER, HIGH);        
    delayMicroseconds(20);
    digitalWrite(TRIGGER, LOW);
    zeit = pulseIn(ECHO, HIGH);         
  } 
for (int i=0; i <= anzahl -1; i++)
{
  servo1.write(zu);
  entfernung2 = entfernung1 / anzahl;
  Serial.println(entfernung2);
  if (entfernung2 > 9550){
    digitalWrite(9, HIGH);
    Serial.println("open");
    servo1.write(auf);
    digitalWrite(9, LOW);
    servo1.write(zu);
  }
}
}

Test it :sunglasses:

And the "TX" and "L" Led light up.

In any case, thank you for your help so far! Thx

Don´t work :joy: :smiling_face_with_tear: :smiling_face_with_tear:

What works?

What doesn’t work ?

What works?: Nothing
What doesn´t work?: Everything
:expressionless:

I don´t know but the "TX" LED light up

Does the sweep example program (that comes with the servo library) work ?

Yes, it works.

Do the same with the example sketch for the distance sensor.

It works too.

You have servo1.attach(9); <-------<<< connect to pin 9

Later you have:

      digitalWrite(9, HIGH);
. . . 
      digitalWrite(9, LOW);

What's your thinking here ? :thinking: