Arduino project problem with Servo, ultrasonic sensor and with code.

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 myservo;
int pos = 0; //
void setup()
{
** Serial.begin(9600);**
** myservo.attach(9);**
** myservo.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()
{
** myservo.write(45);**
** myservo.attach(9);**
** delay(400);**
** for (pos = 45; pos <= 45; pos += 1)**
** {**
** myservo.write(pos);**
** Print(Distance() , pos);**
** delay(10);**
** for (pos = 45; pos >= 45; pos -= 1)**
** myservo.write(pos); Print(Distance() , pos);**
** delay(10);**
** }**
** if (distance < 20)**
** {**
** myservo.write(45);**
** myservo.attach(3);**
** delay(400);**
** for (pos = 45; pos <= 45; pos += 45)**
** {**
** myservo.write(pos);**
** Print(Distance() , pos);**
** delay(10);**
** for (pos = 45; pos >= 45; pos -= 45)**
** myservo.write(pos);**
** Print(Distance() , pos);**
** delay(10);**
** }**
** }**
** else**
** {**
** Serial.print(distance);**
** Serial.println(" cm");**
** }**

** delay(500);**
}

And the pics are attached to this post, please download them to see the scheme and the build of it :slight_smile:

and I’m 14 years old

Thanks for your help :slight_smile:

Your servos have the same name...

myservo.attach(9);
myservo.attach(3);

You might want to call them servo1 and servo2 as you did in your post, then just leave them attached.

That will make debugging muuuuuch simpler.

JimboZA:
Your servos have the same name…

myservo.attach(9);

myservo.attach(3);





You might want to call them servo1 and servo2 as you did in your post, then just leave them attached.

That will make debugging muuuuuch simpler.

I tried here’s the code, but it gives me an error:

#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);
}

Error:

Arduino: 1.6.5 (Windows 8.1), Board: “Arduino/Genuino Uno”

sketch_feb19a.ino: In function ‘void setup()’:
sketch_feb19a:11: error: ‘servo1’ was not declared in this scope
sketch_feb19a:12: error: ‘servo2’ was not declared in this scope
sketch_feb19a.ino: In function ‘void loop()’:
sketch_feb19a:43: error: ‘servo1’ was not declared in this scope
sketch_feb19a:66: error: ‘servo2’ was not declared in this scope
‘servo1’ was not declared in this scope

You need a Servo servo1 and Servo servo2 at the top. You have only Servo servo.

I did it, now I don't have any errors, but the servos do not do their job :(