Paso 5:
#include < Servo.h >
Servo myservo;
Servo myservo2;
const int pingPin = 50;
const int pingPin2 = 52;
void setup() {}
Serial.Begin(9600);
myservo.Attach(10);
myservo2.Attach(11);
}
void loop()
{
larga duración, pulgadas, cm;
pinMode (pingPin2, salida);
digitalWrite (pingPin2, bajo);
delayMicroseconds(2);
digitalWrite (pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite (pingPin2, bajo);
pinMode (pingPin, entrada);
duración = pulseIn (pingPin, HIGH);
pulgadas = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Si (pulgadas < 5) {}
myservo.writeMicroseconds(2000);
myservo2.writeMicroseconds(2000);
}
Si {} (pulgadas > 5)
myservo.writeMicroseconds(1000);
myservo2.writeMicroseconds(1000);
}
Serial.Print(inches);
Serial.Print ("in");
Serial.Print(cm);
Serial.Print("cm");
Serial.println();
Delay(100);
}
microsecondsToInches(long microseconds) largo
{
volver microsegundos / 74 / 2;
}
microsecondsToCentimeters(long microseconds) largo
{
volver microsegundos / 29 / 2;
}