Código para el arduino
#include <Servo.h>
int cm = 0;
int posicion_1 = 90;
int posicion_2 = -90;
Servo miServo;
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin; and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
void setup()
{
pinMode(7, INPUT);
miServo.attach(12);
Serial.begin(9600);
}
void loop()
{
cm = 0.01723 * readUltrasonicDistance(7);
Serial.print(cm);
Serial.println("cm");
delay(100);
int distancia = cm;
if (distancia < 100){
miServo.write(posicion_1);
}else{
miServo.write(posicion_2);
}
}
int cm = 0;
int posicion_1 = 90;
int posicion_2 = -90;
Servo miServo;
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin; and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
void setup()
{
pinMode(7, INPUT);
miServo.attach(12);
Serial.begin(9600);
}
void loop()
{
cm = 0.01723 * readUltrasonicDistance(7);
Serial.print(cm);
Serial.println("cm");
delay(100);
int distancia = cm;
if (distancia < 100){
miServo.write(posicion_1);
}else{
miServo.write(posicion_2);
}
}
No hay comentarios:
Publicar un comentario