#include <Servo.h>
Servo servo1;
Servo servo2;
#define PIN_TRIG 4
#define PIN_ECHO 5
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
servo1.attach(11);
servo2.attach(10);
}
void loop()
{
// Inicia uma nova medição:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Leia o resultado:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distância em CM: ");
Serial.println(duration / 58);
Serial.print("Distância em polegadas: ");
Serial.println(duration / 148);
int distancia = duration / 58;
if (distancia < 20) distancia = 20;
if (distancia > 50) distancia = 50;
delay(1000);
int val = map(distancia, 20, 50, 0, 180);
servo1.write(val);
delay (15);
int distancia2 = duration / 58;
if (distancia2 < 70) distancia2 = 70;
if (distancia2 > 130) distancia2 = 130;
delay(1000);
int val2 = map(distancia2, 70, 130, 0, 180);
servo2.write(val2);
delay (15);
}