360 RADAR using 2 ultrasonic sensor
360 radar
Components and supplies
1
Jumper wires (generic)
2
Ultrasonic Sensor - HC-SR04 (Generic)
1
Arduino UNO
1
SG90 Micro-servo motor
Tools and machines
1
Hot glue gun (generic)
Apps and platforms
1
Arduino IDE
1
Processing
Project description
Code
Arduino code
arduino
1// https://www.youtube.com/channel/UCaXI2PcsTlH5g0et67kdD6g // 2// 360 RADAR using 2 ultrasonic sensor // 3// By MOHD SOHAIL // 4 5 6#include <HCSR04.h> 7#include <Servo.h> 8 9UltraSonicDistanceSensor distanceSensor(6, 7); //Create the 1st sensor object 10UltraSonicDistanceSensor distanceSensor2(5, 4); //Create the 2nd sensor object 11Servo servoMotor; //Create the Servo object 12 13int delayTime = 5; //Delay value to wait for the servo to reach the 1 angle difference 14long d; //Distance from 1st sensor calculated 15long d2; //Distance from 2nd sensor calculated 16 17void setup() { 18 Serial.begin(9600); //Initialize the Serial communication at 9600 bauds 19 20 servoMotor.attach(2); //Attach the servo to the Digital PWM pin 2 21 servoMotor.write(180); //Rotate the servo to the 180? 22 delay(1000); //Wait for the servo to reach 180? 23 servoMotor.write(0); //Rotate the servo to the 0? 24 delay(1000); //Wait for the servo to reach 0? 25 26} 27 28void loop() { 29 for (int i = 1; i < 180; i++) { //Move the Servo 180 degrees forward 30 readSensors(); //Read the sensors 31 Serial.print(i); //Print the angle 32 Serial.print(","); //Print a "," 33 Serial.print(d); //Print the 1st distance 34 Serial.print(","); //Print a "," 35 Serial.println(d2); //Print the 2nd distance with end line 36 servoMotor.write(i); //Set the sensor at the angle 37 delay(delayTime); //Wait for the servo to reach i? 38 } 39 for (int i = 180; i > 1; i--) { //Move the Servo 180 degrees backward 40 readSensors(); //Read the sensors 41 Serial.print(i); //Print the angle 42 Serial.print(","); //Print a "," 43 Serial.print(d); //Print the 1st distance 44 Serial.print(","); //Print a "," 45 Serial.println(d2); //Print the 2nd distance with end line 46 servoMotor.write(i); //Set the sensor at the angle 47 delay(delayTime); //Wait for the servo to reach i? 48 } 49} 50 51void readSensors() { 52 d = distanceSensor.measureDistanceCm(); 53 d2 = distanceSensor2.measureDistanceCm(); 54}
Processing code
java
1import processing.serial.*; 2import static javax.swing.JOptionPane.*; 3 4Serial myPort; // The serial port 5String serialin; 6int data[] = new int[360]; 7PFont f; 8 9final boolean debug = true; 10 11void setup() { 12 String COMx, COMlist = ""; 13 size(1280, 720); 14 f = createFont("Verdana", 32, true); // Arial, 16 point, anti-aliasing on 15 textFont(f, 20); 16 frameRate(60); 17 for (int i = 0; i < 360; i++) { 18 data[i] = 0; 19 } 20 try { 21 if (debug) printArray(Serial.list()); 22 int i = Serial.list().length; 23 if (i != 0) { 24 if (i >= 2) { 25 // need to check which port the inst uses - 26 // for now we'll just let the user decide 27 for (int j = 0; j < i; ) { 28 COMlist += char(j+'a') + " = " + Serial.list()[j]; 29 if (++j < i) COMlist += ", "; 30 } 31 COMx = showInputDialog("Which COM port is correct? (a,b,..):\ 32"+COMlist); 33 if (COMx == null) exit(); 34 if (COMx.isEmpty()) exit(); 35 i = int(COMx.toLowerCase().charAt(0) - 'a') + 1; 36 } 37 String portName = Serial.list()[i-1]; 38 if (debug) println(portName); 39 myPort = new Serial(this, portName, 9600); // change baud rate to your liking 40 myPort.bufferUntil('\n'); // buffer until CR/LF appears, but not required.. 41 } else { 42 showMessageDialog(frame, "Device is not connected to the PC"); 43 exit(); 44 } 45 } 46 catch (Exception e) 47 { //Print the type of error 48 showMessageDialog(frame, "COM port is not available (may\ 49be in use by another program)"); 50 println("Error:", e); 51 exit(); 52 } 53} 54 55 56void draw() { 57 background(26, 26, 36, 200); 58 textSize(18); 59 stroke(255, 255, 255, 150); 60 fill(255, 50, 200, 200); 61 text("Arduino RADAR 2D Visualization", 20, 710); 62 text(hour(), 1050, 710); 63 text(":", 1075, 710); 64 text(minute(), 1085, 710); 65 text(":", 1110, 710); 66 text(second(), 1120, 710); 67 fill(36, 255, 100, 200); 68 strokeWeight(3); 69 circle(640, 360, 600); 70 circle(640, 360, 500); 71 circle(640, 360, 400); 72 circle(640, 360, 300); 73 circle(640, 360, 200); 74 circle(640, 360, 100); 75 76 for (int i = 0; i < 360; i++) { 77 fill(255, 10, 255, 200); 78 stroke(50, 10, 255, 150); 79 point(float(640) + (map_values(data[i]))*cos(radians(i)), float(360) + (map_values(data[i]))*sin(radians(i))); 80 } 81 82 while (myPort.available() > 0) { 83 serialin = myPort.readStringUntil(10); 84 try { 85 String serialdata[] = splitTokens(serialin, ","); 86 if (serialdata[0] != null) { 87 serialdata[0] = trim(serialdata[0]); 88 serialdata[1] = trim(serialdata[1]); 89 serialdata[2] = trim(serialdata[2]); 90 91 int i = int(serialdata[0]); 92 data[179-i] = int(serialdata[1]); 93 data[(179-i)+180] = int(serialdata[2]); 94 } 95 } 96 catch (java.lang.RuntimeException e) { 97 } 98 } 99} 100 101float map_values(float x) { 102 float in_min = 0, in_max = 200, out_min = 0, out_max = 700; 103 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 104}
Processing code
java
1import processing.serial.*; 2import static javax.swing.JOptionPane.*; 3 4Serial myPort; // The serial port 5String serialin; 6int data[] = new int[360]; 7PFont f; 8 9final boolean debug = true; 10 11void setup() { 12 String COMx, COMlist = ""; 13 size(1280, 720); 14 f = createFont("Verdana", 32, true); // Arial, 16 point, anti-aliasing on 15 textFont(f, 20); 16 frameRate(60); 17 for (int i = 0; i < 360; i++) { 18 data[i] = 0; 19 } 20 try { 21 if (debug) printArray(Serial.list()); 22 int i = Serial.list().length; 23 if (i != 0) { 24 if (i >= 2) { 25 // need to check which port the inst uses - 26 // for now we'll just let the user decide 27 for (int j = 0; j < i; ) { 28 COMlist += char(j+'a') + " = " + Serial.list()[j]; 29 if (++j < i) COMlist += ", "; 30 } 31 COMx = showInputDialog("Which COM port is correct? (a,b,..):\ 32"+COMlist); 33 if (COMx == null) exit(); 34 if (COMx.isEmpty()) exit(); 35 i = int(COMx.toLowerCase().charAt(0) - 'a') + 1; 36 } 37 String portName = Serial.list()[i-1]; 38 if (debug) println(portName); 39 myPort = new Serial(this, portName, 9600); // change baud rate to your liking 40 myPort.bufferUntil('\ 41'); // buffer until CR/LF appears, but not required.. 42 } else { 43 showMessageDialog(frame, "Device is not connected to the PC"); 44 exit(); 45 } 46 } 47 catch (Exception e) 48 { //Print the type of error 49 showMessageDialog(frame, "COM port is not available (may\ 50be in use by another program)"); 51 println("Error:", e); 52 exit(); 53 } 54} 55 56 57void draw() { 58 background(26, 26, 36, 200); 59 textSize(18); 60 stroke(255, 255, 255, 150); 61 fill(255, 50, 200, 200); 62 text("Arduino RADAR 2D Visualization", 20, 710); 63 text(hour(), 1050, 710); 64 text(":", 1075, 710); 65 text(minute(), 1085, 710); 66 text(":", 1110, 710); 67 text(second(), 1120, 710); 68 fill(36, 255, 100, 200); 69 strokeWeight(3); 70 circle(640, 360, 600); 71 circle(640, 360, 500); 72 circle(640, 360, 400); 73 circle(640, 360, 300); 74 circle(640, 360, 200); 75 circle(640, 360, 100); 76 77 for (int i = 0; i < 360; i++) { 78 fill(255, 10, 255, 200); 79 stroke(50, 10, 255, 150); 80 point(float(640) + (map_values(data[i]))*cos(radians(i)), float(360) + (map_values(data[i]))*sin(radians(i))); 81 } 82 83 while (myPort.available() > 0) { 84 serialin = myPort.readStringUntil(10); 85 try { 86 String serialdata[] = splitTokens(serialin, ","); 87 if (serialdata[0] != null) { 88 serialdata[0] = trim(serialdata[0]); 89 serialdata[1] = trim(serialdata[1]); 90 serialdata[2] = trim(serialdata[2]); 91 92 int i = int(serialdata[0]); 93 data[179-i] = int(serialdata[1]); 94 data[(179-i)+180] = int(serialdata[2]); 95 } 96 } 97 catch (java.lang.RuntimeException e) { 98 } 99 } 100} 101 102float map_values(float x) { 103 float in_min = 0, in_max = 200, out_min = 0, out_max = 700; 104 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 105}
Downloadable files
Circuit Diagram
Circuit Diagram

Circuit Diagram
Circuit Diagram

Comments
Only logged in users can leave comments