Components and supplies
Arduino Mega 2560
Ultrasonic Sensor - HC-SR04 (Generic)
Holder for Ultrasonic Sensor HC-SR04
SG90 Micro-servo motor
USB-A to B Cable
Perma-Proto Breadboard Half Size
Apps and platforms
Processing
MATLAB
Simulink
Project description
Code
Processing code
c_cpp
Copy and paste the code in Processing.
1/* Arduino Radar Project 2 * 3 * Updated version. Fits any screen resolution! 4 * Just change the values in the size() function, 5 * with your screen resolution. 6 * 7 * by Dejan Nedelkovski, updated by Rexhep Mustafovski 8 * 9 */ 10import processing.serial.*; // imports library for serial communication 11import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 12import java.io.IOException; 13Serial myPort; // defines Object Serial 14// defubes variables 15String angle=""; 16String distance=""; 17String data=""; 18String noObject; 19float pixsDistance; 20float Razmer; // 21int iAngle, iDistance; 22int index1=0; 23int index2=0; 24int pravec=1; 25PFont orcFont; 26void setup() { 27 28 29 fullScreen(); // 30 31 Razmer = (height-height*0.21)*0.025; // 32 33 smooth(); 34 myPort = new Serial(this,"COM3", 115200); // starts the serial communication 35 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 36 orcFont = loadFont("OCRAExtended-30.vlw"); 37 38} 39void draw() { 40 41 fill(98,245,31); 42 textFont(orcFont); 43 // simulating motion blur and slow fade of the moving line 44 noStroke(); 45 fill(0,5); 46 rect(0, 0, width, height-height*0.065); 47 48 fill(98,245,31); // green color 49 // calls the functions for drawing the radar 50 drawRadar(); 51 drawLine(); 52 drawObject(); 53 drawText(); 54 55} 56void serialEvent (Serial myPort) { // starts reading data from the Serial Port 57 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 58 data = myPort.readStringUntil('.'); 59 data = data.substring(0,data.length()-1); 60 61 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 62 63 angle= trim(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 64 distance= trim(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 65 // new updated trimed data 66 67 // converts the String variables into Integer 68 iAngle = int(angle); 69 iDistance = int(distance); 70} 71void drawRadar() { 72 pushMatrix(); 73 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 74 noFill(); 75 strokeWeight(2); 76 stroke(98,245,31); 77 // draws the arc lines 78 79 arc(0,0,2*40*Razmer,2*40*Razmer,PI,TWO_PI); // New updated formulas 80 arc(0,0,2*30*Razmer,2*30*Razmer,PI,TWO_PI); 81 arc(0,0,2*20*Razmer,2*20*Razmer,PI,TWO_PI); 82 arc(0,0,2*10*Razmer,2*10*Razmer,PI,TWO_PI); 83 84 85 // draws the angle lines 86 line(-width/2,0,width/2,0); 87 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 88 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 89 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 90 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 91 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 92 line((-width/2)*cos(radians(30)),0,width/2,0); 93 popMatrix(); 94} 95void drawObject() { 96 pushMatrix(); 97 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 98 strokeWeight(9); 99 stroke(255,10,10); // red color 100 101 pixsDistance = iDistance*Razmer; // converts the distance from the sensor from cm to pixels 102 // limiting the range to 40 cms 103 if(iDistance<40){ 104 // draws the object according to the angle and the distance 105 106 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // new updated line 107 108 popMatrix(); 109} 110void drawLine() { 111 pushMatrix(); 112 strokeWeight(9); 113 stroke(30,250,60); 114 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 115 line(0,0,40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // new updated line 116 popMatrix(); 117} 118void drawText() { // draws the texts on the screen 119 120 pushMatrix(); 121 if(iDistance>40) { 122 noObject = "Out of Range"; 123 } 124 else { 125 noObject = "In Range"; 126 } 127 fill(0,0,0); 128 noStroke(); 129 rect(0, height-height*0.0648, width, height); 130 fill(98,245,31); 131 textSize(25); 132 133 text("10cm",width/2+7*Razmer,height-height*0.0833); 134 text("20cm",width/2+17*Razmer,height-height*0.0833); 135 text("30cm",width/2+27*Razmer,height-height*0.0833); 136 text("40cm",width/2+37*Razmer,height-height*0.0833); 137 138 textSize(40); 139 text("Object: " + noObject, width-width*0.875, height-height*0.0277); 140 text("Angle: " + iAngle +" ", width-width*0.48, height-height*0.0277); 141 text("Distance: ", width-width*0.26, height-height*0.0277); 142 if(iDistance<40) { 143 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 144 } 145 textSize(25); 146 fill(98,245,60); 147 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 148 rotate(-radians(-60)); 149 text("30",0,0); 150 resetMatrix(); 151 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 152 rotate(-radians(-30)); 153 text("60",0,0); 154 resetMatrix(); 155 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 156 rotate(radians(0)); 157 text("90",0,0); 158 resetMatrix(); 159 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 160 rotate(radians(-30)); 161 text("120",0,0); 162 resetMatrix(); 163 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 164 rotate(radians(-60)); 165 text("150",0,0); 166 popMatrix(); 167} 168
Simulink model in Matlab
scheme
Appearance of the model in the Simulink simulation tool.
1inary file (no preview
Processing code
c_cpp
Copy and paste the code in Processing.
1/* Arduino Radar Project 2 * 3 * Updated version. Fits any screen resolution! 4 * Just change the values in the size() function, 5 * with your screen resolution. 6 * 7 * by Dejan Nedelkovski, updated by Rexhep Mustafovski 8 * 9 */ 10import processing.serial.*; // imports library for serial communication 11import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 12import java.io.IOException; 13Serial myPort; // defines Object Serial 14// defubes variables 15String angle=""; 16String distance=""; 17String data=""; 18String noObject; 19float pixsDistance; 20float Razmer; // 21int iAngle, iDistance; 22int index1=0; 23int index2=0; 24int pravec=1; 25PFont orcFont; 26void setup() { 27 28 29 fullScreen(); // 30 31 Razmer = (height-height*0.21)*0.025; // 32 33 smooth(); 34 myPort = new Serial(this,"COM3", 115200); // starts the serial communication 35 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 36 orcFont = loadFont("OCRAExtended-30.vlw"); 37 38} 39void draw() { 40 41 fill(98,245,31); 42 textFont(orcFont); 43 // simulating motion blur and slow fade of the moving line 44 noStroke(); 45 fill(0,5); 46 rect(0, 0, width, height-height*0.065); 47 48 fill(98,245,31); // green color 49 // calls the functions for drawing the radar 50 drawRadar(); 51 drawLine(); 52 drawObject(); 53 drawText(); 54 55} 56void serialEvent (Serial myPort) { // starts reading data from the Serial Port 57 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 58 data = myPort.readStringUntil('.'); 59 data = data.substring(0,data.length()-1); 60 61 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 62 63 angle= trim(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 64 distance= trim(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 65 // new updated trimed data 66 67 // converts the String variables into Integer 68 iAngle = int(angle); 69 iDistance = int(distance); 70} 71void drawRadar() { 72 pushMatrix(); 73 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 74 noFill(); 75 strokeWeight(2); 76 stroke(98,245,31); 77 // draws the arc lines 78 79 arc(0,0,2*40*Razmer,2*40*Razmer,PI,TWO_PI); // New updated formulas 80 arc(0,0,2*30*Razmer,2*30*Razmer,PI,TWO_PI); 81 arc(0,0,2*20*Razmer,2*20*Razmer,PI,TWO_PI); 82 arc(0,0,2*10*Razmer,2*10*Razmer,PI,TWO_PI); 83 84 85 // draws the angle lines 86 line(-width/2,0,width/2,0); 87 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 88 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 89 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 90 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 91 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 92 line((-width/2)*cos(radians(30)),0,width/2,0); 93 popMatrix(); 94} 95void drawObject() { 96 pushMatrix(); 97 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 98 strokeWeight(9); 99 stroke(255,10,10); // red color 100 101 pixsDistance = iDistance*Razmer; // converts the distance from the sensor from cm to pixels 102 // limiting the range to 40 cms 103 if(iDistance<40){ 104 // draws the object according to the angle and the distance 105 106 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // new updated line 107 108 popMatrix(); 109} 110void drawLine() { 111 pushMatrix(); 112 strokeWeight(9); 113 stroke(30,250,60); 114 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 115 line(0,0,40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // new updated line 116 popMatrix(); 117} 118void drawText() { // draws the texts on the screen 119 120 pushMatrix(); 121 if(iDistance>40) { 122 noObject = "Out of Range"; 123 } 124 else { 125 noObject = "In Range"; 126 } 127 fill(0,0,0); 128 noStroke(); 129 rect(0, height-height*0.0648, width, height); 130 fill(98,245,31); 131 textSize(25); 132 133 text("10cm",width/2+7*Razmer,height-height*0.0833); 134 text("20cm",width/2+17*Razmer,height-height*0.0833); 135 text("30cm",width/2+27*Razmer,height-height*0.0833); 136 text("40cm",width/2+37*Razmer,height-height*0.0833); 137 138 textSize(40); 139 text("Object: " + noObject, width-width*0.875, height-height*0.0277); 140 text("Angle: " + iAngle +" ", width-width*0.48, height-height*0.0277); 141 text("Distance: ", width-width*0.26, height-height*0.0277); 142 if(iDistance<40) { 143 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 144 } 145 textSize(25); 146 fill(98,245,60); 147 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 148 rotate(-radians(-60)); 149 text("30",0,0); 150 resetMatrix(); 151 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 152 rotate(-radians(-30)); 153 text("60",0,0); 154 resetMatrix(); 155 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 156 rotate(radians(0)); 157 text("90",0,0); 158 resetMatrix(); 159 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 160 rotate(radians(-30)); 161 text("120",0,0); 162 resetMatrix(); 163 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 164 rotate(radians(-60)); 165 text("150",0,0); 166 popMatrix(); 167} 168
Simulink model in Matlab
scheme
Appearance of the model in the Simulink simulation tool.
1inary file (no preview
Downloadable files
SCHEMATICS
Arduino Mega 2560 Sketch of connecting the components.
SCHEMATICS
SCHEMATICS
Arduino Mega 2560 Sketch of connecting the components
SCHEMATICS
SCHEMATICS
Arduino Mega 2560 Sketch of connecting the components.
SCHEMATICS
Comments
Only logged in users can leave comments
Arduino Radar with Simulink | Arduino Project Hub
bhargav2007
2 years ago
Hello Sir, I'm getting problem with radar. As the radar is not turning and showing me angle:0, distance: 0 "In range".The sensor couldn't communicate with the serial transmit as shown below: Error evaluating 'InitFcn' callback of Arduino Serial Transmit block (mask) 'untitled/Serial Transmit'. Callback string is 'codertarget.arduinobase.conflictcheck.validateandregisterBlocks(gcbh);' Serial Port 0 used by the untitled/Serial Transmit block, is also used for External mode. Change the port number used by the block