Paso 7: De código
#define trigPin 7
#define echoPin 6 #define led 13 #define led2 12 #define led3 11 #define led4 10 #define led5 9 #define led6 8 #define buzzer 3
int sound = 250;
void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led5, OUTPUT); pinMode(led6, OUTPUT); pinMode(buzzer, OUTPUT); pinMode(A0, INPUT);
}
void loop() { int sensorValue = analogRead(A0); long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) / 29.1;
if (sensorValue >= 20) {
Si (distancia < = 30) {digitalWrite (led, alto); sonido = 0; if (distance <= 30) { digitalWrite(led, HIGH); sound = 0; } else { digitalWrite(led, LOW); } if (distance < 25) { digitalWrite(led2, HIGH); sound = 0; } else { digitalWrite(led2, LOW); } if (distance < 20) { digitalWrite(led3, HIGH); sound = 0; } else { digitalWrite(led3, LOW); } if (distance < 15) { digitalWrite(led4, HIGH); sound = 0; } else { digitalWrite(led4, LOW); } if (distance < 10) { digitalWrite(led5, HIGH); sound = 290; } else { digitalWrite(led5, LOW); } if (distance < 5) { digitalWrite(led6, HIGH); sound = 300; } else { digitalWrite(led6, LOW); } Else {digitalWrite (led, baja); if (distance > 30 || distance <= 0) { Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone(buzzer, sound); Si (distancia < 25) {digitalWrite (led2, HIGH); sonido = 0; } Else {digitalWrite (led2, LOW); delay(500); } else { digitalWrite(led, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(led6, LOW); noTone(buzzer); } } Si (distancia < 20) {digitalWrite (LED 3, alto); sonido = 0;Else {digitalWrite (LED 3, bajo);Si (distancia < 15) {digitalWrite (led4, HIGH); sonido = 0;Else {digitalWrite (led4, LOW);Si (distancia < 10) {digitalWrite (led5, HIGH); sonido = 290;Else {digitalWrite (led5, LOW);Si (distancia < 5) {digitalWrite (led6, HIGH); sonido = 300;Else {digitalWrite (led6, LOW);
Si (distancia > 30 || distancia < = 0) {Serial.println ("fuera de rango"); noTone(buzzer);
Delay(500);más {digitalWrite (led, baja); digitalWrite (led2, baja) digitalWrite (LED 3, bajo); digitalWrite (led4, baja); digitalWrite (led5, baja); digitalWrite (led6, baja); noTone(buzzer);