2 Servos, 2 Ultra-Sonic Sensors

Hi everyone,
I was wondering if someone had a code which has 2 ultrasonic sensors and two servos.

The first sensor should move the first servo when hand is placed in front of it when the hand is removed the servo should go back to the original position.

I need the same thing done for the second servo and second ultra-sonic sensor.
(I am currently using an arduino nano)

Thanks heaps,
Alex

PS: Here is my code have worked on below, it only uses one servo and one Ultrasonic sensor - You could modify this if need be. Thanks

#include <Servo.h>
Servo servo1;
int trigPin1 = 0;
int echoPin1 = 1;

long distance;
long duration;

void setup()
{
servo1.attach(13);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}

void loop() {
ultra();
servo1.write(0);
if(distance <= 15){
servo1.write(45);
}
}

void ultra(){
digitalWrite(trigPin1, LOW);
delayMicroseconds(10);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = duration*0.034/2;

}

How is that code working for you?

groundFungus:
How is that code working for you?

The code works when I put my hand in front of the sensor, it moves the servo. When I pull my hand away from the sensor the servo goes back to its neutral position. I need this code to work for two separate servos and two separate sensors. One sensor per servo.

Just duplicate what you did for one sensor servo pair for the other sensor servo pair.

If you wrote that code then you understand what it's doing now. So it should take you about 15 minutes to add a servo2 and ultrasonic2 i.e. a second sensor and servo.

Give it a go and if you have any trouble post your attempt and explain the problems.

Steve

Also here

slipstick:
If you wrote that code then you understand what it’s doing now. So it should take you about 15 minutes to add a servo2 and ultrasonic2 i.e. a second sensor and servo.

Give it a go and if you have any trouble post your attempt and explain the problems.

Steve

Hi Steve,
I gave it a shot however I am getting an error (‘ultra’ was not declared in this scope) which appears in the void loop. If you have any ideas would you mind sending them through?

Thanks heaps!

Alex

Here is the code:

#include <Servo.h>
Servo servo1;
int trigPin1 = 0;
int echoPin1 = 1;
Servo servo2;
int trigPin2 = 2;
int echoPin2 = 3;

long distance;
long duration;

void setup()
{
servo1.attach(13);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
servo2.attach(13);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

void loop() {
ultra();
servo1.write(0);
if(distance <= 15){
servo1.write(45);

ultra();
servo2.write(0);
if(distance <= 15){
servo2.write(45);
}
}

void ultra(){
digitalWrite(trigPin1, LOW);
delayMicroseconds(10);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = duration*0.034/2;

digitalWrite(trigPin2, LOW);
delayMicroseconds(10);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance = duration*0.034/2;
}

Check your braces. { }

Why have you got two servos attached to one pin?

What’s the point of a function that takes two readings, but overwrites the first?

Also, try not to use pins 0 and 1 for simple I/O - they have a far more important use.

Please remember to use code tags when posting code

Goog try but you've missed a few things. You have more than just that one error. But they are all caused by your {} being in a mess. If you use Auto Format (Ctrl-T) in the IDE it will help you see what is wrong. Check that each { has a corresponding }.

And then you really should not use pins 0 and 1 for your sensor. They are needed for hardware serial (debugging) and program loading so you'll need to unplug the sensor every time you need to load a changed program. There are plenty of other pins.

You can't have both servos attached to pin 13. And there's no point having 2 sensors if you only have one set of variables (e.g. duration/distance) to put their results into. That might have worked if you had separate functions for each sensor e.g. ultra() and ultra2() but not when you always read both of them in the same function.

Steve

slipstick:
You can’t have both servos attached to pin 13.

You can, but you probably don’t want to.

TheMemberFormerlyKnownAsAWOL:
You can, but you probably don't want to.

Always a pleasure to meet another member of Pedants R Us!

Steve

In some social groups, the word “pedant” is pejorative, but in engineering, it’s a virtue.