



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




// Incluye la librería Servo #include <Servo.h> // Defines Tirg y Echo del Ultrasonic Sensor const int trigPin = 10 ; const int echoPin = 11 ; // Define el pin del buzzer const int buzzerPin = 9 ; // Variables para la duración y distancia long duration; int distance; Servo myServo; // Crea una variable servo void setup() { pinMode(trigPin, OUTPUT); // Configura el trigPin como salida pinMode(echoPin, INPUT); // Configura el echoPin como entrada pinMode(buzzerPin, OUTPUT); // Configura el buzzerPin como salida Serial.begin( 9600 ); myServo.attach( 12 ); // Define el pin del servo } void loop() { // Rotación del servo de 15 a 165 grados for (int i = 15 ; i <= 165 ; i++) { myServo.write(i); delay( 30 ); distance = calculateDistance(); // Calcular distancia Serial.print(i); // Envía el ángulo actual al puerto serial Serial.print(","); Serial.print(distance); // Envía la distancia al puerto serial Serial.print("."); // Si se detecta un objeto, activa el buzzer if (distance > 0 && distance < 40 ) { // Rango de detección de hasta 40 cm digitalWrite(buzzerPin, HIGH); // Enciende el buzzer } else { digitalWrite(buzzerPin, LOW); // Apaga el buzzer
// Repetición for (int i = 165 ; i > 15 ; i--) { myServo.write(i); delay( 30 ); distance = calculateDistance(); Serial.print(i); Serial.print(","); Serial.print(distance); Serial.print("."); // Si se detecta un objeto, activa el buzzer if (distance > 0 && distance < 40 ) { // Rango de detección de hasta 40 cm digitalWrite(buzzerPin, HIGH); // Enciende el buzzer } else { digitalWrite(buzzerPin, LOW); // Apaga el buzzer } } } // Calcular distancia int calculateDistance() { digitalWrite(trigPin, LOW); delayMicroseconds( 2 ); // Configura trigPin en HIGH por 10 microsegundos digitalWrite(trigPin, HIGH); delayMicroseconds( 10 ); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // Leer el echoPin, retorna la duración de la onda ultrasónica distance = duration * 0.034 / 2 ; // Limitar el rango máximo a 40 cm if (distance > 40 ) { distance = 40 ; // Ajusta distancia máxima a 40 cm } return distance; }
drawLine(); drawObject(); drawText(); } void serialEvent (Serial myPort) { // starts reading data from the Serial Port // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". data = myPort.readStringUntil('.'); data = data.substring(0,data.length()-1); index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance // converts the String variables into Integer iAngle = int(angle); iDistance = int(distance); } void drawRadar() { pushMatrix(); translate(width/2,height-height0.074); // moves the starting coordinats to new location noFill(); strokeWeight(2); stroke(98,245,31); // draws the arc lines arc(0,0,(width-width0.0625),(width-width0.0625),PI,TWO_PI); arc(0,0,(width-width0.27),(width-width0.27),PI,TWO_PI); arc(0,0,(width-width0.479),(width-width0.479),PI,TWO_PI); arc(0,0,(width-width0.687),(width-width0.687),PI,TWO_PI); // draws the angle lines line(-width/2,0,width/2,0); line(0,0,(-width/2)cos(radians(30)),(-width/2)sin(radians(30))); line(0,0,(-width/2)cos(radians(60)),(-width/2)sin(radians(60))); line(0,0,(-width/2)cos(radians(90)),(-width/2)*sin(radians(90)));
line(0,0,(-width/2)cos(radians(120)),(-width/2)sin(radians(120))); line(0,0,(-width/2)cos(radians(150)),(-width/2)sin(radians(150))); line((-width/2)cos(radians(30)),0,width/2,0); popMatrix(); } void drawObject() { pushMatrix(); translate(width/2,height-height0.074); // moves the starting coordinats to new location strokeWeight(9); stroke(255,10,10); // red color pixsDistance = iDistance((height-height0.1666)0.025); // covers the distance from the sensor from cm to pixels // limiting the range to 40 cms if(iDistance<40){ // draws the object according to the angle and the distance line(pixsDistancecos(radians(iAngle)),-pixsDistancesin(radians(iAngle)),(width- width0.505)cos(radians(iAngle)),-(width-width0.505)sin(radians(iAngle))); } popMatrix(); } void drawLine() { pushMatrix(); strokeWeight(9); stroke(30,250,60); translate(width/2,height-height0.074); // moves the starting coordinats to new location line(0,0,(height-height0.12)cos(radians(iAngle)),-(height- height0.12)sin(radians(iAngle))); // draws the line according to the angle popMatrix(); } void drawText() { // draws the texts on the screen pushMatrix(); if(iDistance>40) { noObject = "Out of Range"; } else { noObject = "In Range";
text("120°",0,0); resetMatrix(); translate((width-width0.5104)+width/2cos(radians(150)),(height-height0.0574)-width/ 2sin(radians(150))); rotate(radians(-60)); text("150°",0,0); popMatrix(); }