El código para que funcione es:
const int EchoPin = 5;
const int TriggerPin = 6;
const int LedPin = 9;
void setup() {
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(9,OUTPUT);
}
int ping(int TriggerPin, int EchoPin) {
long duration, distanceCm;
digitalWrite(TriggerPin, LOW);
delayMicroseconds(4);
digitalWrite(TriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
duration = pulseIn(EchoPin, HIGH);
distanceCm = duration * 10 / 292/ 2;
return distanceCm;
}
void loop() {
int cm = ping(TriggerPin, EchoPin);
Serial.print("Distancia: ");
Serial.println(cm);
delay(500);
digitalWrite(LedPin,HIGH);
if(cm<=20) {
digitalWrite(LedPin,HIGH);
}
else{
digitalWrite(LedPin,LOW);
}
}
const int TriggerPin = 6;
const int LedPin = 9;
void setup() {
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(9,OUTPUT);
}
int ping(int TriggerPin, int EchoPin) {
long duration, distanceCm;
digitalWrite(TriggerPin, LOW);
delayMicroseconds(4);
digitalWrite(TriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
duration = pulseIn(EchoPin, HIGH);
distanceCm = duration * 10 / 292/ 2;
return distanceCm;
}
void loop() {
int cm = ping(TriggerPin, EchoPin);
Serial.print("Distancia: ");
Serial.println(cm);
delay(500);
digitalWrite(LedPin,HIGH);
if(cm<=20) {
digitalWrite(LedPin,HIGH);
}
else{
digitalWrite(LedPin,LOW);
}
}
No hay comentarios:
Publicar un comentario