{"id":856,"date":"2016-05-09T02:28:16","date_gmt":"2016-05-09T01:28:16","guid":{"rendered":"http:\/\/wildlab.org\/?page_id=856"},"modified":"2019-05-24T22:23:19","modified_gmt":"2019-05-24T21:23:19","slug":"ultrasonic-radar-arduino","status":"publish","type":"page","link":"https:\/\/wildlab.org\/index.php\/ultrasonic-radar-arduino\/","title":{"rendered":"Ultrasonic Radar &#8211; Arduino project"},"content":{"rendered":"<h2>Ultrasonic Radar;\u00a0Multi-Point<\/h2>\n<p style=\"text-align: justify;\">This ultrasonic radar, or ultrasonic scanner &#8211; may be used for fun and learning Arduino code \u00a0(sketch) by changing parameters. As a bonus, there is Java program which uses Java code: <a href=\"https:\/\/processing.org\/\">Processing<\/a>\u00a0for various tasks and visualization tool. Very powerful tool (okay complicated at first, but anyone can learn how to make own code). Install it on your computer, and you may learn how to visualize data collected by various sensors attached to Arduino, or use it for whatsoever you want. It is free program, and working perfectly. An example of screenshot of Processing visualization is here:<\/p>\n<h2>Ultrasonogram:<\/h2>\n<p><a href=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Multipoint.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"aligncenter wp-image-858 size-large\" src=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Multipoint-1024x575.jpg\" alt=\"ultrasonic\" width=\"474\" height=\"266\" srcset=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Multipoint-1024x575.jpg 1024w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Multipoint-300x168.jpg 300w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Multipoint.jpg 1920w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>Okay, image is not as good as Medical Ultrasound Scanner, but price is not as high as well (read: very, very expensive). \ud83d\ude00<\/p>\n<p><span style=\"font-size: 24px; font-weight: bold;\">Simple to make<\/span><\/p>\n<p style=\"text-align: justify;\">This ultrasonic radar, in\u00a0our\u00a0case &#8211; image we get on the screen, we may call it &#8220;Sonogram&#8221;, or &#8220;Ultrasonogram&#8221; because it is more like medical ultrasonic scanner than military radar. More on wiki about this <a href=\"https:\/\/en.wikipedia.org\/wiki\/Medical_ultrasound\">Sonogram<\/a>. I am interested to get as much as possible points from the reflected ultrasonic waves at 41 kHz from <a href=\"http:\/\/www.banggood.com\/Wholesale-Ultrasonic-Module-HC-SR04-Distance-Measuring-Ranging-Transducer-Sensor-p-40313.html?p=EH10221611330201505Q\">HC-SR04 <\/a>ultrasonic distance sensor. But, instead using &#8220;echo&#8221; pin, we will use analog output from amplified ultrasonic microphone. Milan-CAD:<\/p>\n<p><a href=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Diagram1.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"aligncenter wp-image-857 size-large\" src=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Diagram1-1024x576.jpg\" alt=\"ultrasonic\" width=\"474\" height=\"267\" srcset=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Diagram1-1024x576.jpg 1024w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Diagram1-300x169.jpg 300w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Diagram1.jpg 1920w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>Also, one detail of simple attachment between sensor and <a href=\"http:\/\/www.banggood.com\/TowerPro-T-SG92R-Micro-Digital-Servo-9g-2_5kg-For-RC-Airplane-p-971189.html?p=EH10221611330201505Q\">servo<\/a>:<\/p>\n<p><a href=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Sensor_to_servo.jpg\"><img loading=\"lazy\" class=\"aligncenter size-large wp-image-867\" src=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Sensor_to_servo-1024x576.jpg\" alt=\"ultrasonic\" width=\"474\" height=\"267\" srcset=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Sensor_to_servo-1024x576.jpg 1024w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Sensor_to_servo-300x169.jpg 300w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/Sensor_to_servo.jpg 1920w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>Just dab of hot-glue and job is done. If you change mind, you can &#8220;salvage&#8221; this <a href=\"http:\/\/www.banggood.com\/TowerPro-T-SG92R-Micro-Digital-Servo-9g-2_5kg-For-RC-Airplane-p-971189.html?p=EH10221611330201505Q\">servo<\/a> for other experiments when ultrasound become boring (not so soon, I know myself). \ud83d\ude00<\/p>\n<h2>Not the best artist on drawing<\/h2>\n<p style=\"text-align: justify;\">Out there are many programs for drawing diagrams, but all are complicated and\/or confusing. Spending lot of time figuring out how it works is not the pleasant experience. If someone has good skill to making better drawing, feel free to draw it and back to me, or publish as your own drawing. I will not ask for any copyright rights on drawing, but I will insist on holding title of &#8220;original idea&#8221;. \ud83d\ude00<\/p>\n<h2>Video\u00a0example(now old version) of ultrasonic radar:<\/h2>\n<p><iframe loading=\"lazy\" width=\"474\" height=\"267\" src=\"https:\/\/www.youtube.com\/embed\/4dzux73zCzw?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Arduino Ultrasonic Multi-Point Radar sketch (old, as is in the video above):<\/h2>\n<pre class=\"theme:classic lang:c++ decode:true\">\/\/ Original idea by Milan Karaka\u0161, https:\/\/wildlab.org (C)2016\r\n\/\/ Made wild idea to use analog echo reading - NOT echo pin\r\n\/\/ from HC-SR04 ultrasonic module, just solder one wire at\r\n\/\/ collector of the transistor as is shown on my website:\r\n\/\/ https:\/\/wildlab.org\/index.php\/arduino-ulstrasonic-radar\/\r\n\/\/ This sketch \"burn\" onto your favorite Arduino (uno, nano....)\r\n\/\/ and enjoy in experimenting with ultrasound \"Sonograms\". :o)\r\n\/\/ Pay attention to Serial.begin(115200) because there are lot\r\n\/\/ of data sending in short period, so both: Arduino board and\r\n\/\/ Processing program on your computer can communicate without\r\n\/\/ los of data. Processing you can find here: https:\/\/processing.org\/\r\n\/\/ Bonus notice: Anyone who first make this Radar, please contact\r\n\/\/ me in the comment section below this page:\r\n\/\/ https:\/\/wildlab.org\/index.php\/arduino-ulstrasonic-radar\/\r\n\/\/ And your work will be published as \"Showcase\" for other people \r\n\/\/ as an example. Later I will figure out how to reward you. Thanks. \r\n\r\n#include &lt;Servo.h&gt;\r\n\r\n#define SERVO_PWM_PIN 10\r\n#define ANGLE_BOUNDS 80\r\n#define ANGLE_STEP 1\r\n\r\nint angle = 0;\r\nint dir = 1;\r\nint i,j;\r\nfloat strength[40]; \/\/40 analog readings send to your computer for \"Processing\" program\r\nServo myservo;\r\nint analog_pin = A0; \/\/analog pin A0 on arduino nano, uno, mega....\r\n\r\nvoid setup() \r\n{\r\npinMode(2,OUTPUT);\r\nSerial.begin(115200);\r\nmyservo.attach(SERVO_PWM_PIN);\/\/later will be attached\/detached, but once should start for first time\r\n}\r\n\r\nvoid loop() \r\n{\r\n  getValues();\r\n  Serial.print(-angle, DEC);      \/\/first parameter send to your computer for Processing program\r\n  \/\/ Pay Attention! There is \"-angle\" because some servoes may do it in wrong direction. If servo and image\r\n  \/\/ on your computer does not match, put there \"angle\" instead \"-angle\". This will reverse servo direction                        \r\n  for (i=0;i&lt;40;i++)              \/\/will be good to use variable instead fixed value for number of reading points\r\n   {                              \/\/for later experimenting with number of analog read echo points\r\n     Serial.print(\",\");           \/\/ all data separated by coma (',')\r\n     Serial.print(strength[i],0); \/\/ Atention! This is not for debugging - Processing program will use it\r\n                                  \/\/ for visualisation (reading from COMxx port)\r\n   }\r\n  Serial.println(); \/\/not printing anything, just new row, and back to the first place\r\n  myservo.write(angle + ANGLE_BOUNDS); \/\/servo should move one step\r\n  if (angle &gt;= ANGLE_BOUNDS || angle &lt;= -ANGLE_BOUNDS)\r\n   {\r\n     dir = -dir;\r\n   }\r\n  angle += (dir*ANGLE_STEP);\r\n  delay(100); \/\/alow some time for +5V to settle down, because servo can draw some current and make analog read error\r\n }\r\n\r\nint getValues() \/\/function which read analog echo and return array of analog values\r\n{\r\n  myservo.detach(); \/\/very important! this command disable PWM - which make interference during analog read\r\n  delay(2); \/\/wait additionally 2 mS for RF noise to settle down (first few centimeters)\r\n  digitalWrite(2,1); \/\/this triggers sensor\r\n  delayMicroseconds(1); \/\/although it is set to 1 uS, it gives actually 4 uS, which is sufficient to trigger HC-SR04 ultrasound ...\r\n  digitalWrite(2,0);\/\/this is end of triggering sensor\r\n  delay(2);\/\/lets avoid crosstalk between emitter and receiver in first millisecond or two\r\n  for (j=0;j&lt;40;j++) \/\/lets make array of j elements\r\n   {\r\n    int str= ((analogRead(analog_pin)*5.00)\/1024)*pow(i,2)*5; \/\/ \"dynamic amplification\" by exponent^2 of i *5\r\n                                                              \/\/ Since amplitude of ultrasound fall with square of the distance,\r\n                                                              \/\/ \"pow(i,2)\" is actually i^2 (i squared). You may experiment with\r\n                                                              \/\/ additional amplification by factor of 5 in example above\r\n                                                              \/\/ too much, and noise will be visible, too low, and reflectin will be\r\n                                                              \/\/ weak, yet maybe better \"gray scale\" than too high values\r\n    strength[j]=constrain(str,0,255); \/\/since \"Processing\" program will use max values between 0 and 255, lets make some limits\r\n    delayMicroseconds(50); \/\/if 50 microseconds, this delay alow analog read to do reading properly (ADC converter)\r\n       }\r\n  myservo.attach(SERVO_PWM_PIN); \/\/now let servo PWM signal to continue for next step before back into main loop\r\n  return strength[40]; \/\/ returning array of analog read echo values, you may experiment with this number of points. \r\n}                      \/\/ Just remember to change sketch on Processing so that number of this readings are correct\r\n                       \/\/ Data send to your computer is angle + this number of samples. In Processing use array of 41 numbers\r\n                       \/\/ First number will be from -80 to 80, which indicates angle, and the rest of numbers ranging fom 0 to 255\r\n                       \/\/ representing amplitude of the echoed ultrasound.    \r\n<\/pre>\n<h2>Processing sketch (old one; as in the video above):<\/h2>\n<pre class=\"theme:github lang:c++ decode:true \">\/\/ Original idea by Milan Karaka\u0161, 2016, https:\/\/wildlab.org\r\n\/\/ https:\/\/wildlab.org\/index.php\/arduino-ulstrasonic-radar\/\r\n\/\/ Revision 0.99 - sorry, but not yet cleaned for not used parts\r\n\/\/ some old stuff remained, because had hard time to figure out\r\n\/\/ how to work in Processing. Many notes are in Croatian.\r\n\/\/ Thanks for understanding.\r\n\r\nimport processing.serial.*;\r\n\r\nint SIDE_LENGTH = 1000;\r\nint ANGLE_BOUNDS = 80;\r\nint ANGLE_STEP = 2;\r\nint HISTORY_SIZE = 10;\r\nint POINTS_HISTORY_SIZE = 500;\r\nint MAX_DISTANCE = 100;\r\n\r\nint angle;\r\nint distance;\r\nint[] echoes = new int[40]; \/\/mo\u017eda array treba samo \"int[] echoes\"??? \r\n\/\/Echo[] echoes; \/\/mo\u017eda bi ovo bio pravilniji array?! treba samo dodati to u class dolje\r\n\r\nint radius;\r\nfloat x, y;\r\nfloat leftAngleRad, rightAngleRad;\r\n\r\nfloat[] historyX, historyY;\r\nPoint[] points; \/\/ovo je primjer vi\u0161e to\u010daka, no samo jedna je \"pro\u0161la\" kroz funkciju (angle, -&gt;distance)\r\n\r\nint centerX, centerY;\r\n\r\nString comPortString;\r\nSerial myPort;\r\n\r\nvoid setup() {\r\n  size(1024, 600, P2D);\r\n  noStroke();\r\n  \/\/smooth();\r\n  rectMode(CENTER);\r\nbackground(0);\r\n  radius = SIDE_LENGTH \/ 2;\r\n  centerX = width \/ 2;\r\n  centerY = height;\r\n  angle = 0;\r\n  leftAngleRad = radians(-ANGLE_BOUNDS) - HALF_PI;\r\n  rightAngleRad = radians(ANGLE_BOUNDS) - HALF_PI;\r\n\r\n  historyX = new float[HISTORY_SIZE];\r\n  historyY = new float[HISTORY_SIZE];\r\n  points = new Point[POINTS_HISTORY_SIZE];\r\n\r\n  myPort = new Serial(this, Serial.list()[1], 115200);\r\n  myPort.bufferUntil('\\n'); \/\/ Trigger a SerialEvent on new line\r\n}\r\n\r\n\r\nvoid draw() \/\/ovo je totalno okay, ne diraj\r\n{\r\n  \/\/background(0);\r\n\/\/  drawRadar(); \/\/ovo ispisuje radarski ekran (mre\u017eu), ali ne i to\u010dke\r\n  drawFoundObjects(angle, echoes); \/\/bilo je: drawFoundObjects(angle, distance);\r\n\/\/  drawRadarLine(angle); \/\/mislim da je ovo nepromijenjeno, no... dali to bri\u0161e stare to\u010dke?\r\n}\r\n\r\nvoid drawRadarLine(int angle) \/\/ovo radi dobro i trebat \u0107e donekle\r\n{\r\n  float radian = radians(angle);\r\n  x = radius * sin(radian);\r\n  y = radius * cos(radian);\r\n  float px = centerX + x;\r\n  float py = centerY - y;\r\n  historyX[0] = px;\r\n  historyY[0] = py;\r\n  for (int i=0; i&lt;HISTORY_SIZE; i++) \r\n  {\r\n    stroke(50, 150, 50, 255 - (25*i));\r\n    line(centerX, centerY, historyX[i], historyY[i]);\r\n  }\r\n  shiftHistoryArray();\r\n}\r\n\r\nvoid drawFoundObjects(int angle, int echoes[]) \/\/bilo je: void drawFoundObjects(int angle, int distance) \r\n{\r\n  int n;\r\n\/\/   int distance =500; \/\/ mimicking, imitiram kao da je odjek na \"200\" ne\u010dega\r\n\/\/ovdje je bila formula za test, maknuo dolje jer je nestala ako je broj ve\u0107i od 0\r\nfor ( n=1;n&lt;40;n++)\r\n{\r\nfloat distance = n*12.5; \r\n  if (distance &gt; 0) \r\n  {\r\n    float radian = radians(angle);\r\n    x = distance * sin(radian);\r\n    y = distance * cos(radian);\r\n    int px = (int)(centerX + x);\r\n    int py = (int)(centerY - y);\r\n    points[0] = new Point(px, py); \/\/bilo je: points[0] = new Point(px, py);\r\n  } else \r\n  {\r\n    points[0] = new Point(0, 0);\r\n  }\r\n    for (int i=0; i&lt;POINTS_HISTORY_SIZE; i++) \/\/mislim da je ovo definitivno jeba koja bri\u0161e stare to\u010dke\r\n  {\r\n    Point point = points[i];\/\/bilo je: Point point = points[i];\r\n    if (point != null) \r\n    {\r\n      int x = point.x;\r\n      int y = point.y;  \r\n      if (x==0 &amp;&amp; y==0) continue;\r\n      \/\/int colorAlfa = (int)map(i, 0, POINTS_HISTORY_SIZE, 20, 0); \/\/ovo je izgleda dio koji \"bri\u0161e\" stare to\u010dke, treba maknuti\r\n      \r\n     \/\/  int size = (int)map(i, 0, POINTS_HISTORY_SIZE, 30, 5);\r\n      fill(echoes[n]); \/\/ovdje bi trebao \"sjesti\" paremetar o ja\u010dini odjeka umjesto \"255, -&gt;15\": fill(50, 150, 50, colorAlfa);\r\n      \/\/ali colorAlfa \"poja\u010dava\" stari trag. Ako se to ne \u017eeli, onda treba raditi bez alfe i \"modulirati\" prvi broj\r\n      noStroke();\r\n      ellipse(x, y, 15, 15); \/\/maknuti smanjivanje to\u010daka: ellipse(x, y, size, size);\r\n      \/* temporary comfirmation that \"echoes[]\" are passing through void. YES!!!! SUCCESS!!!\r\n     for (int n=1; n&lt;40; n++)\r\n      {\r\n        text(\"brojevi: \" +  echoes[n],10,17*n);\r\n        println(\"br: \" + echoes[n]); \/\/ovo radi, samo sada treba \"vratiti\" vrijednosti za iscrtavanje to\u010daka\r\n        \/\/ispisivalo je dolje korektne vrijednosti, samo da na\u0111em ...\r\n      } *\/\r\n    }\r\n }\r\n\r\n  \/\/ shiftPointsArray();\r\n}}\r\n\r\nvoid drawRadar() \/\/ovo je izgleda ok, za sada ne diraj\r\n{\r\n  stroke(100);\r\n  noFill();\r\n\r\n  \/\/ part of the circle distance from the center\r\n  for (int i = 0; i &lt;= (SIDE_LENGTH \/ 100); i++) {\r\n    arc(centerX, centerY, 100 * i, 100 * i, leftAngleRad, rightAngleRad);\r\n  }\r\n\r\n  \/\/ angle indicators\r\n  for (int i = 0; i &lt;= (ANGLE_BOUNDS*2\/20); i++) {\r\n    float angle = -ANGLE_BOUNDS + i * 20;\r\n    float radAngle = radians(angle);\r\n    line(centerX, centerY, centerX + radius*sin(radAngle), centerY - radius*cos(radAngle));\r\n  }\r\n}\r\n\r\nvoid shiftHistoryArray() {\r\n\r\n  for (int i = HISTORY_SIZE; i &gt; 1; i--) {\r\n\r\n    historyX[i-1] = historyX[i-2];\r\n    historyY[i-1] = historyY[i-2];\r\n  }\r\n}\r\n\r\nvoid shiftPointsArray(){}\/\/ovo treba izbaciti ZAUVIJEK!!!\r\n\/*{\r\n  for (int i = POINTS_HISTORY_SIZE; i &gt; 1; i--) {\r\n\r\n    Point oldPoint = points[i-2];\r\n    if (oldPoint != null) {\r\n\r\n      Point point = new Point(oldPoint.x, oldPoint.y);\r\n      points[i-1] = point; \/\/ovo nedostaje gore?\r\n    }\r\n  }\r\n}*\/\r\n\r\nvoid serialEvent(Serial cPort) \r\n{\r\n\r\n  comPortString = cPort.readString();\r\n  if (comPortString != null) \r\n  {\r\n\r\n    comPortString=trim(comPortString);\r\n    String[] values = split(comPortString, ',');\r\n    try \r\n    {\r\n      angle = Integer.parseInt(values[0]); \/\/ovo razjebati\r\n      \/\/distance = int(map(Integer.parseInt(values[1]), 1, MAX_DISTANCE, 1, radius)); \/\/ovo treba razjebati i preopraviti\r\n      \/\/ovdje \u0107u po\u010deti razjebavati\r\n      \/\/println(\"brojevi su:\");\r\n      \r\n      for (int n=1; n&lt;40; n++) \/\/nikako \"0\", jer je to kut - sve ostalo su analogne vrijednosti od 0-255\r\n      {\r\n        echoes[n]= Integer.parseInt(values[n]);\r\n        \/\/textSize(12);\r\n        \/\/fill(255);\r\n        \/\/text(\"brojevi: \" +  echoes[n],10,17*n);\r\n        \/\/println(\"br: \" + echoes[n]); \/\/ovo radi, samo sada treba \"vratiti\" vrijednosti za iscrtavanje to\u010daka\r\n        \/\/ispisivalo je dolje korektne vrijednosti, samo da na\u0111em ...\r\n      }\r\n    } \r\n    catch (Exception e) {}\r\n  }\r\n\r\n}\r\n\r\nclass Point {\r\n  int x, y;\r\n\r\n  Point(int xPos, int yPos) {\r\n    x = xPos;\r\n    y = yPos;\r\n  }\r\n\r\n  int getX() {\r\n    return x;\r\n  }\r\n\r\n  int getY() {\r\n    return y;\r\n  }\r\n}\r\n<\/pre>\n<h2>New\u00a0(and better)\u00a0Arduino sketch for ultrasonic radar, some parts optimized:<\/h2>\n<pre class=\"theme:classic lang:c++ decode:true\">\/\/ Original idea by Milan Karaka\u0161, https:\/\/wildlab.org (C)2016\r\n\/\/ Made wild idea to use analog echo reading - NOT echo pin\r\n\/\/ from HC-SR04 ultrasonic module, just solder one wire at\r\n\/\/ collector of the transistor as is shown on my website:\r\n\/\/ https:\/\/wildlab.org\/index.php\/arduino-ulstrasonic-radar\/\r\n\/\/ This sketch \"burn\" onto your favorite Arduino (uno, nano....)\r\n\/\/ and enjoy in experimenting with ultrasound \"Sonograms\". :o)\r\n\/\/ Pay attention to Serial.begin(115200) because there are lot\r\n\/\/ of data sending in short period, so both: Arduino board and\r\n\/\/ Processing program on your computer can communicate without\r\n\/\/ los of data. Processing you can find here: https:\/\/processing.org\/\r\n\/\/ Bonus notice: Anyone who first make this Radar, please contact\r\n\/\/ me in the comment section below this page:\r\n\/\/ https:\/\/wildlab.org\/index.php\/arduino-ulstrasonic-radar\/\r\n\/\/ And your work will be published as \"Showcase\" for other people \r\n\/\/ as an example. Later I will figure out how to reward you. Thanks. \r\n\r\n#include &lt;Servo.h&gt;\r\n\r\n#define SERVO_PWM_PIN 10\r\n#define ANGLE_BOUNDS 80\r\n#define ANGLE_STEP 1\r\n\r\nint angle = 0;\r\nint dir = 1;\r\nint i,j;\r\nfloat strength[80]; \/\/40 analog readings send to your computer for \"Processing\" program\r\nServo myservo;\r\nint analog_pin = A0; \/\/analog pin A0 on arduino nano, uno, mega....\r\n\r\nvoid setup() \r\n{\r\npinMode(2,OUTPUT);\r\nSerial.begin(115200);\r\n\/\/Serial.begin(250000);\r\nmyservo.attach(SERVO_PWM_PIN);\/\/later will be attached\/detached, but once should start for first time\r\n}\r\n\r\nvoid loop() \r\n{\r\n  getValues();\r\n  Serial.print(-angle, DEC);      \/\/first parameter send to your computer for Processing program\r\n  \/\/ Pay Attention! There is \"-angle\" because some servoes may do it in wrong direction. If servo and image\r\n  \/\/ on your computer does not match, put there \"angle\" instead \"-angle\". This will reverse servo direction                        \r\n  for (i=0;i&lt;80;i++)              \/\/will be good to use variable instead fixed value for number of reading points\r\n   {                              \/\/for later experimenting with number of analog read echo points\r\n     Serial.print(\",\");           \/\/ all data separated by coma (',')\r\n     Serial.print(strength[i],0); \/\/ Atention! This is not for debugging - Processing program will use it\r\n                                  \/\/ for visualisation (reading from COMxx port)\r\n   }\r\n  Serial.println(); \/\/not printing anything, just new row, and back to the first place\r\n  myservo.write(angle + ANGLE_BOUNDS); \/\/servo should move one step\r\n  if (angle &gt;= ANGLE_BOUNDS || angle &lt;= -ANGLE_BOUNDS)\r\n   {\r\n     dir = -dir;\r\n   }\r\n  angle += (dir*ANGLE_STEP);\r\n  delay(10); \/\/ alow some time for +5V to settle down, because servo can draw some current and make analog read error\r\n             \/\/ DO NOT go below 10 mS - this value is experimentally get by oscilloscope, else strange \"rays\" may occur on radar\r\n }\r\n\r\nint getValues() \/\/function which read analog echo and return array of analog values\r\n{\r\n  myservo.detach(); \/\/very important! this command disable PWM - which make interference during analog read\r\n  delay(2); \/\/wait additionally 2 mS for RF noise to settle down (first few centimeters)\r\n  digitalWrite(2,1); \/\/this triggers sensor\r\n  delayMicroseconds(1); \/\/although it is set to 1 uS, it gives actually 4 uS, which is sufficient to trigger HC-SR04 ultrasound ...\r\n  digitalWrite(2,0);\/\/this is end of triggering sensor\r\n \/\/ delay(1);\/\/lets avoid crosstalk between emitter and receiver in first millisecond or two\r\n \/\/ NO! let it be: if this delay is added, then whole screen is wrong - missing some components and distorted...\r\n \/\/ As a result of avoiding this delay, \"origin\" of the radar pulses will be with some false reading, but not a problem\r\n  for (j=0;j&lt;80;j++) \/\/lets make array of j elements\r\n   {\r\n \/\/ int str= ((analogRead(analog_pin)*5.00)\/1024)*pow(i,2);   \/\/ too long to execute, also not using constrain below\r\n        strength[j]=(analogRead(analog_pin)*0.0025)*pow(i,2); \/\/ \"dynamic amplification\" by exponent^2 of i *5\r\n                                                              \/\/ Since amplitude of ultrasound fall with square of the distance,\r\n                                                              \/\/ \"pow(i,2)\" is actually i^2 (i squared). You may experiment with\r\n                                                              \/\/ additional amplification by factor of 5 in example above\r\n                                                              \/\/ too much, and noise will be visible, too low, and reflectin will be\r\n                                                              \/\/ weak, yet maybe better \"gray scale\" than too high values\r\n   \/\/strength[j]=constrain(str,0,255); \/\/since \"Processing\" program will use max values between 0 and 255, lets make some limits\r\n                                       \/\/but disabled because it \"stills\" some valuable mathematic time of the MCU\r\n  }\r\n  myservo.attach(SERVO_PWM_PIN); \/\/now let servo PWM signal to continue for next step before back into main loop\r\n  return strength[80]; \/\/ returning array of analog read echo values, you may experiment with this number of points. \r\n}                      \/\/ Just remember to change sketch on Processing so that number of this readings are correct\r\n                       \/\/ Data send to your computer is angle + this number of samples. In Processing use array of 41 numbers\r\n                       \/\/ First number will be from -80 to 80, which indicates angle, and the rest of numbers ranging fom 0 to 255\r\n                       \/\/ representing amplitude of the echoed ultrasound.    \r\n<\/pre>\n<h2>Processing sketch for ultrasonic radar, cleaned (to some extent), and added more points (now 80 instead 40):<\/h2>\n<pre class=\"theme:github lang:c++ decode:true\">\/\/ Original idea by Milan Karaka\u0161, 2016, https:\/\/wildlab.org\r\n\/\/ https:\/\/wildlab.org\/index.php\/arduino-ulstrasonic-radar\/\r\n\/\/ Revision 1.01 - sorry, but not yet cleaned for not used parts\r\n\/\/ some old stuff remained, because had hard time to figure out\r\n\/\/ how to work in Processing. Thanks for understanding.\r\n\/\/ New big screen size; 1920 x 1024 for those who have HD screen\r\n\/\/ Or, down below should be math slightly changed\r\n\r\nimport processing.serial.*;\r\n\r\nint SIDE_LENGTH = 1000;\r\nint ANGLE_BOUNDS = 80;\r\nint ANGLE_STEP = 2;\r\nint HISTORY_SIZE = 10;\r\nint POINTS_HISTORY_SIZE = 500;\r\nint MAX_DISTANCE = 100;\r\n\r\nint angle;\r\nint distance;\r\nint[] echoes = new int[80]; \/\/ Now with more points; 80 instead old 40 \r\n\r\nint radius;\r\nfloat x, y;\r\nfloat leftAngleRad, rightAngleRad;\r\n\r\nfloat[] historyX, historyY;\r\nPoint[] points; \/\/ovo je primjer vi\u0161e to\u010daka, no samo jedna je \"pro\u0161la\" kroz funkciju (angle, -&gt;distance)\r\n\r\nint centerX, centerY;\r\n\r\nString comPortString;\r\nSerial myPort;\r\n\r\nvoid setup() {\r\n  size(1920, 1024, P2D);\/\/ was 1024,600 - for 80 points it is better to have bigger screen\r\n  noStroke();\r\n  \/\/smooth();\r\n  rectMode(CENTER);\r\nbackground(0);\r\n  radius = SIDE_LENGTH \/ 2;\r\n  centerX = width \/ 2;\r\n  centerY = height;\r\n  angle = 0;\r\n  leftAngleRad = radians(-ANGLE_BOUNDS) - HALF_PI;\r\n  rightAngleRad = radians(ANGLE_BOUNDS) - HALF_PI;\r\n\r\n  historyX = new float[HISTORY_SIZE];\r\n  historyY = new float[HISTORY_SIZE];\r\n  points = new Point[POINTS_HISTORY_SIZE];\r\n\r\n  myPort = new Serial(this, Serial.list()[1], 115200);\r\n \/\/  myPort = new Serial(this, Serial.list()[1], 250000); \/\/just tested in debugging process, ignore\r\n  myPort.bufferUntil('\\n'); \/\/ Trigger a SerialEvent on new line\r\n}\r\n\r\n\r\nvoid draw() \/\/ovo je totalno okay, ne diraj\r\n{\r\n  drawFoundObjects(angle, echoes); \/\/was: drawFoundObjects(angle, distance);\r\n}\r\n\r\nvoid drawFoundObjects(int angle, int echoes[]) \/\/was: void drawFoundObjects(int angle, int distance) \r\n{\r\n  int n;\r\nfor ( n=1;n&lt;80;n++)\r\n{\r\n\/\/float distance = n*6.25; \/\/for \"small screen, or one with only 40 points\r\nfloat distance = n*12.5;\/\/ \"zoom\" options   -  much bigger screen\r\n  if (distance &gt; 0) \r\n  {\r\n    float radian = radians(angle);\r\n    x = distance * sin(radian);\r\n    y = distance * cos(radian);\r\n    int px = (int)(centerX + x);\r\n    int py = (int)(centerY - y);\r\n    points[0] = new Point(px, py); \/\/was: points[0] = new Point(px, py);\r\n  } else \r\n  {\r\n    points[0] = new Point(0, 0);\r\n  }\r\n    for (int i=0; i&lt;POINTS_HISTORY_SIZE; i++) \/\/not resolved yet whether it needs to be here or not...\r\n  {\r\n    Point point = points[i];\/\/was: Point point = points[i];\r\n    if (point != null) \r\n    {\r\n      int x = point.x;\r\n      int y = point.y;  \r\n      if (x==0 &amp;&amp; y==0) continue;\r\n  \r\n      \/\/ NEW, \"green\" look of the screen :D\r\n      fill(20,echoes[n],40); \/\/or \"fill(echoes[n])\", or \"fill(255,echoes[n]\", or \"fill(echoes[n],echoes[n])\" - gives various graphic result\r\n      \/\/first number in \"fill(number,alpha) is brightnes of elypse, and second is \"alpha\" whatsoever, different effect\r\n      noStroke();\r\n      ellipse(x, y, 7, 7); \/\/variation on 15,15 if necessary: ellipse(x, y, size, size); \/\/various spot sizes...\r\n    }\r\n  }\r\n\r\n }\r\n}\r\n\r\nvoid drawRadar() \r\n{\r\n  stroke(100);\r\n  noFill();\r\n\r\n  \/\/ part of the circle distance from the center\r\n  for (int i = 0; i &lt;= (SIDE_LENGTH \/ 100); i++) {\r\n    arc(centerX, centerY, 100 * i, 100 * i, leftAngleRad, rightAngleRad);\r\n  }\r\n\r\n  \/\/ angle indicators\r\n  for (int i = 0; i &lt;= (ANGLE_BOUNDS*2\/20); i++) {\r\n    float angle = -ANGLE_BOUNDS + i * 20;\r\n    float radAngle = radians(angle);\r\n    line(centerX, centerY, centerX + radius*sin(radAngle), centerY - radius*cos(radAngle));\r\n  }\r\n}\r\n\r\nvoid serialEvent(Serial cPort) \r\n{\r\n\r\n  comPortString = cPort.readString();\r\n  if (comPortString != null) \r\n  {\r\n\r\n    comPortString=trim(comPortString);\r\n    String[] values = split(comPortString, ',');\r\n    try \r\n    {\r\n      angle = Integer.parseInt(values[0]); \/\/just first element of array is angle, everything else are echoes analog values\r\n      for (int n=1; n&lt;80; n++) \/\/new 80 elements instead 40\r\n      {\r\n        echoes[n]= Integer.parseInt(values[n]);\r\n      }\r\n    } \r\n    catch (Exception e) {}\r\n  }\r\n\r\n}\r\n\r\nclass Point {\r\n  int x, y;\r\n\r\n  Point(int xPos, int yPos) {\r\n    x = xPos;\r\n    y = yPos;\r\n  }\r\n\r\n  int getX() {\r\n    return x;\r\n  }\r\n\r\n  int getY() {\r\n    return y;\r\n  }\r\n}\r\n<\/pre>\n<h2>New, better look; 80 points, and &#8220;Military green&#8221; colors \ud83d\ude00<\/h2>\n<p><a href=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/New-radar.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"aligncenter wp-image-871 size-large\" src=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/New-radar-1024x576.jpg\" alt=\"ultrasound\" width=\"474\" height=\"267\" srcset=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/New-radar-1024x576.jpg 1024w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/New-radar-300x169.jpg 300w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/New-radar.jpg 1920w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<h2>New video \u00a0example of improved program\u00a0on Arduino and Processing:<\/h2>\n<p><iframe loading=\"lazy\" width=\"474\" height=\"267\" src=\"https:\/\/www.youtube.com\/embed\/TlMJZYMQybk?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p style=\"text-align: justify;\">Only one part is wrong due to electronic noise: &#8220;origin&#8221;, or center of rotation is always present. Tried &#8220;delay(2)&#8221;, or two milliseconds in the loop for reading analog data, but this gives distortion and whole screen looks like it will &#8220;sink&#8221; into this central part &#8211; because missing few point rows. Now, it looks okay. Maybe there are some room for further improvements, will see.<\/p>\n<h2 style=\"text-align: justify;\">The reason of the central &#8220;ghost readings&#8221;:<\/h2>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/TheReason.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" class=\"aligncenter wp-image-877 size-large\" src=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/TheReason-1024x576.jpg\" alt=\"ultrasonic\" width=\"474\" height=\"267\" srcset=\"https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/TheReason-1024x576.jpg 1024w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/TheReason-300x169.jpg 300w, https:\/\/wildlab.org\/wp-content\/uploads\/2016\/05\/TheReason.jpg 1920w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Just after ultrasonic frequencies are off, amplifier still has some spike, which looks strange on the radar screen. But, this is not such big problem. Instead &#8220;blanking-out&#8221; at Arduino end, it is maybe better to do it at Processing end on your computer. For me, this is not issue at all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ultrasonic Radar;\u00a0Multi-Point This ultrasonic radar, or ultrasonic scanner &#8211; may be used for fun and learning Arduino code \u00a0(sketch) by changing parameters. As a bonus, there is Java program which uses Java code: Processing\u00a0for various tasks and visualization tool. Very powerful tool (okay complicated at first, but anyone can learn how to make own code). &hellip; <a href=\"https:\/\/wildlab.org\/index.php\/ultrasonic-radar-arduino\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Ultrasonic Radar &#8211; Arduino project<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/pages\/856"}],"collection":[{"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/comments?post=856"}],"version-history":[{"count":24,"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/pages\/856\/revisions"}],"predecessor-version":[{"id":2178,"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/pages\/856\/revisions\/2178"}],"wp:attachment":[{"href":"https:\/\/wildlab.org\/index.php\/wp-json\/wp\/v2\/media?parent=856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}