Hi, I m an architect and pretty new to Arduino and coding.
I am trying to make a prototype where I want to control 4 servo motors with data from 4 ultrasound sensors respectively. I have already searched for this online and found a solution with 1 sensor and 1 servo.
I have zero expertise in coding. Can someone help me with the code?
Post the code you have.
A common newbie mistake with ultrasound sensors is to ping them too frequently.
Hello
To double functionality it is wise to look how objects are handled in C++.
i am using the following code, i did not write it, collected from net
#include <Servo.h>
#define trigPin 11
#define echoPin 8
Servo servo;
int sound = 250;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(3);
}
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 < 10) {
Serial.println("the distance is less than 10");
servo.write(90);
delay(1500);
}
else {
servo.write(0);
}
if (distance > 60 || distance <= 0){
Serial.println("The distance is more than 60");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination
use code tags ![]()
to publish your sketch.
The simple, but clumsy way to add 3 more servos is to duplicate he code for the single servo using different pins for the other 3 servos and sensors
The smarter way is to use arrays to hold the 4 sets of pins and to iterate through them with a for loop
i dnt have enough knowledge to understand the "array" thing, but what about this clumsy one
#include <Servo.h>
#define trigPin 11
#define echoPin 8
Servo servo;
int sound = 250;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(3);
}
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 < 10) {
Serial.println("the distance is less than 10");
servo.write(90);
delay(1500);
}
else {
servo.write(0);
}
if (distance > 60 || distance <= 0){
Serial.println("The distance is more than 60");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
#include <Servo.h>
#define trigPin2 21
#define echoPin2 28
Servo servo2;
int sound2 = 250;
void setup2() {
Serial.begin (9600);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
servo2.attach(23);
}
void loop2() {
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 < 10) {
Serial.println("the distance is less than 10");
servo.write(90);
delay(1500);
}
else {
servo.write(0);
}
if (distance > 60 || distance <= 0){
Serial.println("The distance is more than 60");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination
As to the code itself. it has multiple problems
For a start you can only have one setup() and loop() function and you cannot have multiple global variables with the same name and you only need to #include the Servo library once
Note that I said
rather than duplicating the whole code in the sketch
So, one set of global variables declared at the start of the sketch, #include the library once, create the servo instances each with different names, attach the servos in setup() then in loop() 4 sets of code to read each sensor and move the associated servo
thanks for the reply, bt I am too new to understand all that you said, can u show an example please?
Here is an altered version of your code for one servo with comments added indicating how to adapt it for 4 servos. It compiles but is untested
#include <Servo.h>
Servo servo0; //declare 3 more servos with suitable names such as servo1 etc
const byte trigPin0 = 11; //add 3 more sets of pin declarations with suitable names
const byte echoPin0 = 8;
const byte servoPin0 = 3;
long duration, distance;
void setup()
{
Serial.begin (9600);
pinMode(trigPin0, OUTPUT);
pinMode(echoPin0, INPUT);
servo0.attach(servoPin0); //attach 3 more servos
}
void loop()
{
//repeat the code in loop() for 3 more servos using previously declared servo names and pins
digitalWrite(trigPin0, LOW);
delayMicroseconds(2);
digitalWrite(trigPin0, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin0, LOW);
duration = pulseIn(echoPin0, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo0.write(90);
delay(1500);
}
else
{
servo0.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
As I said, clumsy. The version using arrays will be neater, but get this working first
We also need to discuss before too long how the servos are powered
thanks a ton dude, i'll check and get back to you asap
@hasannmann, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with or advice on your project ![]()
i used the following code, i get this message " 'trigPin11' was not declared in this scope "
here's the code
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
const byte trigPin1 = 11;
const byte echoPin1 = 13;
const byte servoPin1 = 17;
const byte trigPin2 = 21;
const byte echoPin2 = 23;
const byte servoPin2 = 27;
const byte trigPin3 = 31;
const byte echoPin3 = 33;
const byte servoPin3 = 37;
const byte trigPin4 = 41;
const byte echoPin4 = 43;
const byte servoPin4 = 47;
long duration, distance;
void setup()
{
Serial.begin (9600);
pinMode(trigPin11, OUTPUT);
pinMode(echoPin13, INPUT);
servo1.attach(servoPin17);
pinMode(trigPin21, OUTPUT);
pinMode(echoPin23, INPUT);
servo2.attach(servoPin27);
pinMode(trigPin31, OUTPUT);
pinMode(echoPin33, INPUT);
servo3.attach(servoPin37);
pinMode(trigPin41, OUTPUT);
pinMode(echoPin43, INPUT);
servo4.attach(servoPin47);
}
void loop()
{
digitalWrite(trigPin11, LOW);
delayMicroseconds(2);
digitalWrite(trigPin11, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin11, LOW);
duration = pulseIn(echoPin13, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo1.write(90);
delay(1500);
}
else
{
servo1.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
digitalWrite(trigPin21, LOW);
delayMicroseconds(2);
digitalWrite(trigPin21, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin21, LOW);
duration = pulseIn(echoPin23, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo2.write(90);
delay(1500);
}
else
{
servo2.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
digitalWrite(trigPin31, LOW);
delayMicroseconds(2);
digitalWrite(trigPin31, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin31, LOW);
duration = pulseIn(echoPin33, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo3.write(90);
delay(1500);
}
else
{
servo3.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
digitalWrite(trigPin41, LOW);
delayMicroseconds(2);
digitalWrite(trigPin41, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin41, LOW);
duration = pulseIn(echoPin43, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo4.write(90);
delay(1500);
}
else
{
servo4.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
You have trig pins declared with names trigPin1,trigPin2, trigPin3 and trigPin4 but you are trying to use a variable named trigPin11
Either change the names in the declarations or change the names where you use the names as declared now
And a whole lot more other variables.
Look at the first few lines starting
const byte trigPin1 = 11;
and then use those variable names in your code
Also, all your prints refer to "distance 0" - that's going to make debugging difficult.
should i start from "distance 1"
I was keeping that as a secret so that @hasannmann could develop his/her debugging skills
For reasons that @anon73444976 will understand, I suggest that you name all of the variables with a suffix of 0, 1, 2 or 3 and not the pin number. This will make it easier to understand the conversion of the code to use arrays later
take a look, this one gives "done compiling"
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
const byte trigPin1 = 11;
const byte echoPin1 = 13;
const byte servoPin1 = 17;
const byte trigPin2 = 21;
const byte echoPin2 = 23;
const byte servoPin2 = 27;
const byte trigPin3 = 31;
const byte echoPin3 = 33;
const byte servoPin3 = 37;
const byte trigPin4 = 41;
const byte echoPin4 = 43;
const byte servoPin4 = 47;
long duration, distance;
void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
servo1.attach(servoPin1);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
servo2.attach(servoPin2);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
servo3.attach(servoPin3);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
servo4.attach(servoPin4);
}
void loop()
{
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo1.write(90);
delay(1500);
}
else
{
servo1.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo2.write(90);
delay(1500);
}
else
{
servo2.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration = pulseIn(echoPin3, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo3.write(90);
delay(1500);
}
else
{
servo3.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
digitalWrite(trigPin4, LOW);
delayMicroseconds(2);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
duration = pulseIn(echoPin4, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{
Serial.println("distance 0 is less than 10");
servo4.write(90);
delay(1500);
}
else
{
servo4.write(0);
}
if (distance > 60 || distance <= 0)
{
Serial.println("distance 0 is more than 60");
}
else
{
Serial.print("distance 0 : ");
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
It compiles but as @anon73444976 pointed out your print statements all say the same thing, probably because you did not change them after copy/paste. Change the prints to refer to the servo being reported on rather than all of them referring to number 0
Have you tried uploading and using the sketch with actual servos and sensors ?
As I have said previously you need to consider how the servos are powered. How are they powered at the moment ?