// Jorge Javier from different codes 2021 Fab Academy # define led 13 int trigger = 12; int echo = 13; float tiempo_de_espera,distancia; // floating distance. void setup() { Serial.begin (9600); // serial communication pinMode (trigger, OUTPUT); // pin 12 as exit pinMode (echo, INPUT); // pin 13 as entrance pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); } void loop() { digitalWrite (trigger,LOW); // set low pin 13 for 2 microseconds delayMicroseconds(2); digitalWrite (trigger, HIGH);// set high pin 13 for 10 microseconds; delayMicroseconds (10); // sonar for 13 seconds digitalWrite (trigger, LOW); // set low again pin 11 tiempo_de_espera = pulseIn (echo,HIGH); // pulseIn, recoge la señal del sonido que emite el trigger /*La función pulseIn espera la aparición de un pulso en una entrada y mide su duración, dando como resultado la duración medida El primer parámetro (ECHO) es el pin sobre el que se realizará la medición. Y el segundo parámetro (HIGH) indica si el pulso a esperar será un 1 (HIGH) o un 0 (LOW). */ distancia =(tiempo_de_espera/2)/29.15; // formula para hallar la distancia Serial.print (distancia); // print distance in cm Serial.println ("cm"); delay (300); if (distancia>=40 && distancia <=60){ digitalWrite (2,1); digitalWrite (3,1); digitalWrite (4,1); digitalWrite (5,1); digitalWrite (6,1); digitalWrite (7,1); digitalWrite (8,1); digitalWrite (9,1); } if (distancia>=36 && distancia <=50){ digitalWrite (2,1); digitalWrite (3,0); digitalWrite (4,0); digitalWrite (5,0); digitalWrite (6,0); digitalWrite (7,0); digitalWrite (8,0); digitalWrite (9,0); } if (distancia>=32 && distancia <=45){ digitalWrite (2,1); digitalWrite (3,1); digitalWrite (4,0); digitalWrite (5,0); digitalWrite (6,0); digitalWrite (7,0); digitalWrite (8,0); digitalWrite (9,0); } if (distancia>=28 && distancia <=40){ digitalWrite (2,0); digitalWrite (3,1); digitalWrite (4,1); digitalWrite (5,0); digitalWrite (6,0); digitalWrite (7,0); digitalWrite (8,0); digitalWrite (9,0); } if (distancia>=24 && distancia <=35){ digitalWrite (2,0); digitalWrite (3,0); digitalWrite (4,1); digitalWrite (5,1); digitalWrite (6,0); digitalWrite (7,0); digitalWrite (8,0); digitalWrite (9,0); } if (distancia>=20 && distancia <=30){ digitalWrite (2,0); digitalWrite (3,0); digitalWrite (4,0); digitalWrite (5,1); digitalWrite (6,1); digitalWrite (7,0); digitalWrite (8,0); digitalWrite (9,0); } if (distancia>=16 && distancia <=25){ digitalWrite (2,0); digitalWrite (3,0); digitalWrite (4,0); digitalWrite (5,0); digitalWrite (6,1); digitalWrite (7,1); digitalWrite (8,0); digitalWrite (9,0); } if (distancia>=12 && distancia <=20){ digitalWrite (2,0); digitalWrite (3,0); digitalWrite (4,0); digitalWrite (5,0); digitalWrite (6,0); digitalWrite (7,1); digitalWrite (8,1); digitalWrite (9,0); } if (distancia>=8 && distancia <=15){ digitalWrite (2,0); digitalWrite (3,0); digitalWrite (4,0); digitalWrite (5,0); digitalWrite (6,0); digitalWrite (7,0); digitalWrite (8,1); digitalWrite (9,1); } }