int randNumber;
const int EchoPin = 4;
const int TriggerPin = 5;
int estado=0;
int cont=0;
int estAnt=0;
void setup() {
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
randomSeed(analogRead(0));
digitalWrite(6, HIGH);
delay(200);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(200);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
delay(200);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(200);
digitalWrite(9, LOW);
}
void loop() {
int cm = ping(TriggerPin, EchoPin);
if(cm<=100){
if(estado == estAnt){
randNumber = random(6, 10);
digitalWrite(randNumber, HIGH);
delay(1000);
digitalWrite(randNumber, LOW);
estado = estAnt + 1;
}
}
else {
estAnt = estado;
}
}
int ping(int TriggerPin, int EchoPin) {
long duration, distanceCm;
digitalWrite(TriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(TriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
duration = pulseIn(EchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
distanceCm = duration * 10 / 292/ 2; //convertimos a distancia, en cm
return distanceCm;
}