#include <Servo.h>
Servo motor1;
Servo motor2;
Servo motor3;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int pos_x = 100;
int pos_y = 120;
int pos_z = 130;
const int pingPin1 = 13; // Trigger Pin of Ultrasonic Sensor 1
const int echoPin1 = 12; // Echo Pin of Ultrasonic Sensor 1
const int pingPin2 = 11; // Trigger Pin of Ultrasonic Sensor 2
const int echoPin2 = 10; // Echo Pin of Ultrasonic Sensor 2
const int pingPin3 = 9; // Trigger Pin of Ultrasonic Sensor 3
const int echoPin3 = 8; // Echo Pin of Ultrasonic Sensor 3
void setup() {
// put your setup code here, to run once:
motor1.attach(6);
motor2.attach(3);
motor3.attach(5);
}
void loop() {
// put your main code here, to run repeatedly:
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
for(pos1 = 0; pos1 <= pos_x; pos1++) {
motor1.write(pos1);
delay(15);
}
for(pos2 = 0; pos2 <= pos_y; pos2++) {
motor2.write(pos2);
delay(15);
}
for(pos3 = 0; pos3 <= pos_z; pos3++) {
motor3.write(pos3);
delay(15);
}
}