Servo motors, ultrasonic sensor, and/or code problem

Hello, so I'm making this project, a ultrasonic sensor that tracks the distance of the area nearby, and it's standing on top of a servo(1) which should always be rotating and when a object is closer than 20 cm, it turns on another servo(2), which makes an object fall(this servo just has to turn one time, amd then it can do whatever it wants :P). It's kind of an alarm. But I'm having problems with coding and I don't know if I'm doing things right with connecting servos and sensors to Arduino, so here's a scheme of the build and the code below, you're free to help me figuring the problem out, and btw, the problem is that none of the servos move or rotate, but the ultrasonic sensor calculates the distance, and even though an object is closer that 20 cm , it still doesn't turn on the servo(2).

Here's the code:

#define ECHOPIN 7
#define TRIGPIN 8
#include <Servo.h>
float distance;

Servo servo;
int pos = 0; //
void setup()
{
Serial.begin(9600);
servo1.attach(9);
servo2.attach(3);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}

void Print (int R , int T)
{
Serial.print(R); Serial.print(", ");
Serial.print(T); Serial.println(".");
delay(100);
}

float Distance () {

digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);

float distance = pulseIn(ECHOPIN, HIGH);
distance = distance / 58;
return (distance);
}

void loop()

{
servo1.write(45);
servo1.attach(9);
delay(400);
for (pos = 45; pos <= 45; pos += 1)
{
servo1.write(pos);
Print(Distance() , pos);
delay(10);

for (pos = 45; pos >= 45; pos -= 1)

servo1.write(pos); Print(Distance() , pos);

delay(10);
}

if (distance < 20)
{
servo2.attach(3);
servo2.write(45);

delay(400);

for (pos = 45; pos <= 45; pos += 45)
{
servo2.write(pos);

Print(Distance() , pos);
delay(10);

for (pos = 45; pos >= 45; pos -= 45)

servo2.write(pos);
Print(Distance() , pos);

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

delay(500);
}

Here's a picture of my project!

Please help!!! :frowning:

 for (pos = 45; pos <= 45; pos += 1)
 {
   servo1.write(pos);
   Print(Distance() , pos);
   delay(10);




   for (pos = 45; pos >= 45; pos -= 1)

You have a nested loop with the same loop control variable.
Good luck figuring that one out.

Please use code tags when posting code.

A servo.attach is normally done just once, in setup - I don't know why you've got them in loop.

My point of this project is that Servo1, is rotating, and should always be, never stop, and on top of it, stands a ultrasonic sensor, which is being rotated by Servo1, and when the sensor measures the area and something is closer than 20 cm, Servo2 will turn on and rotate really fast, and make a soda can fall, and make noise, similar to an alarm, but my teacher said that it should be more complicated, so he said ;"Add another servo...' , and now he won't even help me!!! anyway , the problem is that neither of 2 servos are turning on, but the sensor is still measuring the distance quite well. So please help.

You have a nested loop with the same loop control variable.
Also, your code doesn't have either a "servo1" or a "servo2" object, but you knew that, because the compiler told you.

  if (distance < 20)

Where is "distance" assigned a value?

Also: How are the servos powered? Many require up to an amp when they move. Powering from Arduino 5V can cause the Arduino to reset and cause what seems like erratic behavior.

CrossRoads:
Also: How are the servos powered? Many require up to an amp when they move. Powering from Arduino 5V can cause the Arduino to reset and cause what seems like erratic behavior.

You can't have erratic behavior if the code doesn't compile.