Components and supplies
Arduino UNO
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
SG90 Micro-servo motor
Solderless Breadboard Full Size
Tools and machines
Soldering Iron Kit, SolderPro 150
Hot glue gun (generic)
Apps and platforms
Processing
Arduino IDE
Project description
Code
Sonar code for processing IDE
java
1import processing.serial.*; 2Serial myPort; 3 4String ang=""; 5String distance=""; 6String data=""; 7 8int angle, dist; 9 10void setup() { 11 size (2000,800); 12 myPort = new Serial(this,"COM3", 9600); 13 myPort.bufferUntil('.'); 14 background(0); 15} 16 17void draw() { 18 fill(0,5); 19 noStroke(); 20 rect(0, 0, width, height*0.93); 21 noStroke(); 22 fill(0,255); 23 rect(0,height*0.93,width,height); 24 drawRadar(); 25 drawLine(); 26 drawObject(); 27 drawText(); 28} 29 30 31void serialEvent (Serial myPort) { 32 data = myPort.readStringUntil('.'); 33 data = data.substring(0,data.length()-1); 34 int index1 = data.indexOf(","); 35 ang= data.substring(0, index1); 36 distance= data.substring(index1+1, data.length()); 37 angle = int(ang); 38 dist = int(distance); 39 System.out.println(angle); 40} 41 42void drawRadar(){ 43 pushMatrix(); 44 noFill(); 45 strokeWeight(0.5); 46 stroke(10,255,10); 47 translate(width/2,height-height*0.06); 48 line(-width/2,0,width/2,0); 49 arc(0,0,(width*0.5),(width*0.5),PI,TWO_PI); 50 arc(0,0,(width*0.25),(width*0.25),PI,TWO_PI); 51 arc(0,0,(width*0.75),(width*0.75),PI,TWO_PI); 52 arc(0,0,(width*0.95),(width*0.95),PI,TWO_PI); 53 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 54 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 55 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 56 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 57 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 58 stroke(175,255,175); 59 line(0,0,(-width/2)*cos(radians(15)),(-width/2)*sin(radians(15))); 60 line(0,0,(-width/2)*cos(radians(45)),(-width/2)*sin(radians(45))); 61 line(0,0,(-width/2)*cos(radians(75)),(-width/2)*sin(radians(75))); 62 line(0,0,(-width/2)*cos(radians(105)),(-width/2)*sin(radians(105))); 63 line(0,0,(-width/2)*cos(radians(135)),(-width/2)*sin(radians(135))); 64 line(0,0,(-width/2)*cos(radians(165)),(-width/2)*sin(radians(165))); 65 popMatrix(); 66} 67 68void drawLine() { 69 pushMatrix(); 70 strokeWeight(9); 71 stroke(0,255,0); 72 translate(width/2,height-height*0.06); 73 line(0,0,(width/2)*cos(radians(angle)),(-width/2)*sin(radians(angle))); 74 popMatrix(); 75} 76 77void drawObject() { 78 pushMatrix(); 79 strokeWeight(9); 80 stroke(255,0,0); 81 translate(width/2,height-height*0.06); 82 float pixleDist = (dist/40.0)*(width/2.0); 83 float pd=(width/2)-pixleDist; 84 float x=-pixleDist*cos(radians(angle)); 85 float y=-pixleDist*sin(radians(angle)); 86 if(dist<=40){ 87 line(-x,y,-x+(pd*cos(radians(angle))),y-(pd*sin(radians(angle)))); 88 } 89 popMatrix(); 90} 91 92void drawText(){ 93 pushMatrix(); 94 fill(100,200,255); 95 textSize(25); 96 text("10cm",(width/2)+(width*0.115),height*0.93); 97 text("20cm",(width/2)+(width*0.24),height*0.93); 98 text("30cm",(width/2)+(width*0.365),height*0.93); 99 text("40cm",(width/2)+(width*0.45),height*0.93); 100 if(dist<=40) { 101 text("Distance :"+dist,width*0.7,height*0.99); 102 } 103 translate(width/2,height-height*0.06); 104 textSize(25); 105 text(" 30",(width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 106 text(" 60",(width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 107 text("90",(width/2)*cos(radians(91)),(-width/2)*sin(radians(90))); 108 text("120",(width/2)*cos(radians(123)),(-width/2)*sin(radians(118))); 109 text("150",(width/2)*cos(radians(160)),(-width/2)*sin(radians(150))); 110 popMatrix(); 111} 112
Sonar code for arduino IDE
c_cpp
1#include <Servo.h> 2const int trigPin=12; 3const int echoPin=11; 4long duration; 5int distance; 6Servo s1; 7 8void setup() { 9 Serial.begin(9600); 10 pinMode(trigPin,OUTPUT); 11 pinMode(echoPin,INPUT); 12 s1.attach(9); 13} 14 15void loop(){ 16 for(int i=0;i<180;i=i+1){ 17 s1.write(i); 18 delay(30); 19 distance = calDist(); 20 Serial.print(i); 21 Serial.print(","); 22 Serial.print(distance); 23 Serial.print("."); 24 } 25 for(int i=180;i>0;i=i-1){ 26 s1.write(i); 27 delay(30); 28 distance = calDist(); 29 Serial.print(i); 30 Serial.print(","); 31 Serial.print(distance); 32 Serial.print("."); 33 } 34} 35 36 int calDist(){ 37 digitalWrite(trigPin, LOW); 38 delayMicroseconds(2); 39 digitalWrite(trigPin, HIGH); 40 delayMicroseconds(10); 41 digitalWrite(trigPin, LOW); 42 duration = pulseIn(echoPin, HIGH); 43 distance= duration*0.034/2; 44 return distance; 45} 46
Sonar code for arduino IDE
c_cpp
1#include <Servo.h> 2const int trigPin=12; 3const int echoPin=11; 4long duration; 5int distance; 6Servo s1; 7 8void setup() { 9 Serial.begin(9600); 10 pinMode(trigPin,OUTPUT); 11 pinMode(echoPin,INPUT); 12 s1.attach(9); 13} 14 15void loop(){ 16 for(int i=0;i<180;i=i+1){ 17 s1.write(i); 18 delay(30); 19 distance = calDist(); 20 Serial.print(i); 21 Serial.print(","); 22 Serial.print(distance); 23 Serial.print("."); 24 } 25 for(int i=180;i>0;i=i-1){ 26 s1.write(i); 27 delay(30); 28 distance = calDist(); 29 Serial.print(i); 30 Serial.print(","); 31 Serial.print(distance); 32 Serial.print("."); 33 } 34} 35 36 int calDist(){ 37 digitalWrite(trigPin, LOW); 38 delayMicroseconds(2); 39 digitalWrite(trigPin, HIGH); 40 delayMicroseconds(10); 41 digitalWrite(trigPin, LOW); 42 duration = pulseIn(echoPin, HIGH); 43 distance= duration*0.034/2; 44 return distance; 45} 46
Downloadable files
Scheamatics for sonar
Scheamatics for sonar
Scheamatics for sonar
Scheamatics for sonar
Comments
Only logged in users can leave comments