Arduino Radar
Simple Arduino Radar Project With Arduino IDE & Processing
Components and supplies
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
SG90 Micro-servo motor
1
Jumper wires (generic)
1
Breadboard (generic)
1
Arduino UNO
Tools and machines
1
Multitool, Screwdriver
1
Hot glue gun (generic)
Apps and platforms
1
Processing
1
Arduino IDE
Project description
Code
Arduino And Processing Code
c_cpp
Upload The code to arduino and run the processing code in processing pde i have uploaded both the code in same column extract both the code where you see ****************** lines
1//feel free to contact 2//www.youtube.com/ZenoModiff 3//sreeramzeno@gmail.com 4 5 6#include <Servo.h>. 7const int trigPin = 10; 8const int echoPin = 11; 9long duration; 10int distance; 11Servo myServo; 12void setup() { 13 pinMode(trigPin, OUTPUT); 14 pinMode(echoPin, INPUT); 15 Serial.begin(9600); 16 myServo.attach(12); 17} 18void loop() { 19 20 for(int i=15;i<=165;i++){ 21 myServo.write(i); 22 delay(30); 23 distance = calculateDistance(); 24 Serial.print(i); 25 Serial.print(","); 26 Serial.print(distance); 27 Serial.print("."); 28 } 29 30 for(int i=165;i>15;i--){ 31 myServo.write(i); 32 delay(30); 33 distance = calculateDistance(); 34 Serial.print(i); 35 Serial.print(","); 36 Serial.print(distance); 37 Serial.print("."); 38 } 39} 40 41int calculateDistance(){ 42 43 digitalWrite(trigPin, LOW); 44 delayMicroseconds(2); 45 46 digitalWrite(trigPin, HIGH); 47 delayMicroseconds(10); 48 digitalWrite(trigPin, LOW); 49 duration = pulseIn(echoPin, HIGH); 50 distance= duration*0.034/2; 51 return distance; 52} 53 54 55*********************************************************************** 56//THIS IS THE PROCESSING CODE 57//CUT AND COPY CODE FROM LINES 60-199 58 59import processing.serial.*; 60import java.awt.event.KeyEvent; 61import java.io.IOException; 62Serial myPort; 63// defubes variables 64String angle=""; 65String distance=""; 66String data=""; 67String noObject; 68float pixsDistance; 69int iAngle, iDistance; 70int index1=0; 71int index2=0; 72PFont orcFont; 73void setup() { 74 75 size (1200, 700); 76 smooth(); 77 myPort = new Serial(this,"COM4", 9600); // change your comport as in the arduino ide 78 myPort.bufferUntil('.'); 79} 80void draw() { 81 82 fill(98,245,31); 83 84 noStroke(); 85 fill(0,4); 86 rect(0, 0, width, height-height*0.065); 87 88 fill(98,245,31); 89 90 drawRadar(); 91 drawLine(); 92 drawObject(); 93 drawText(); 94} 95void serialEvent (Serial myPort) { 96 97 data = myPort.readStringUntil('.'); 98 data = data.substring(0,data.length()-1); 99 100 index1 = data.indexOf(","); 101 angle= data.substring(0, index1); 102 distance= data.substring(index1+1, data.length()); 103 104 // converts the String variables into Integer 105 iAngle = int(angle); 106 iDistance = int(distance); 107} 108void drawRadar() { 109 pushMatrix(); 110 translate(width/2,height-height*0.074); 111 noFill(); 112 strokeWeight(2); 113 stroke(98,245,31); 114 // draws the arc lines 115 arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI); 116 arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI); 117 arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI); 118 arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI); 119 // draws the angle lines 120 line(-width/2,0,width/2,0); 121 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 122 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 123 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 124 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 125 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 126 line((-width/2)*cos(radians(30)),0,width/2,0); 127 popMatrix(); 128} 129void drawObject() { 130 pushMatrix(); 131 translate(width/2,height-height*0.074); 132 strokeWeight(9); 133 stroke(255,10,10); // red color 134 pixsDistance = iDistance*((height-height*0.1666)*0.025); 135 // limiting the range to 40 cms 136 if(iDistance<40){ 137 // draws the object according to the angle and the distance 138 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle))); 139 } 140 popMatrix(); 141} 142void drawLine() { 143 pushMatrix(); 144 strokeWeight(9); 145 stroke(30,250,60); 146 translate(width/2,height-height*0.074); 147 line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); 148 popMatrix(); 149} 150void drawText() { 151 152 pushMatrix(); 153 if(iDistance>40) { 154 noObject = "Out of Range"; 155 } 156 else { 157 noObject = "In Range"; 158 } 159 fill(0,0,0); 160 noStroke(); 161 rect(0, height-height*0.0648, width, height); 162 fill(98,245,31); 163 textSize(25); 164 165 text("10cm",width-width*0.3854,height-height*0.0833); 166 text("20cm",width-width*0.281,height-height*0.0833); 167 text("30cm",width-width*0.177,height-height*0.0833); 168 text("40cm",width-width*0.0729,height-height*0.0833); 169 textSize(40); 170 text("Zeno_Modiff", width-width*0.875, height-height*0.0277); 171 text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277); 172 text("Distance: ", width-width*0.26, height-height*0.0277); 173 if(iDistance<40) { 174 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 175 } 176 textSize(25); 177 fill(98,245,60); 178 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 179 rotate(-radians(-60)); 180 text("30°",0,0); 181 resetMatrix(); 182 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 183 rotate(-radians(-30)); 184 text("60°",0,0); 185 resetMatrix(); 186 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 187 rotate(radians(0)); 188 text("90°",0,0); 189 resetMatrix(); 190 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 191 rotate(radians(-30)); 192 text("120°",0,0); 193 resetMatrix(); 194 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 195 rotate(radians(-60)); 196 text("150°",0,0); 197 popMatrix(); 198} 199
Downloadable files
Schematics
Arduino Radar Schematics
Schematics

Schematics
Arduino Radar Schematics
Schematics

Comments
Only logged in users can leave comments