Codigo:
// C++ code
//
int Distancia = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
Distancia = 0.01723 * readUltrasonicDistance(6, 5);
Serial.println(Distancia);
delay(300); // Wait for 300 millisecond(s)
if (Distancia < 130) {
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
}
if (Distancia > 158) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);
}
if (Distancia > 130 && Distancia < 158) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
}
No hay comentarios:
Publicar un comentario