safwan092

project_0533607317_Proccessing_Code

Oct 20th, 2022
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  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,
  8. * www.HowToMechatronics.com
  9. *
  10. */
  11.  
  12. import processing.serial.*; // imports library for serial communication
  13. import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
  14. import java.io.IOException;
  15.  
  16. Serial myPort; // defines Object Serial
  17. // defubes variables
  18. String angle="";
  19. String distance="";
  20. String data="";
  21. String noObject;
  22. float pixsDistance;
  23. int iAngle, iDistance;
  24. int index1=0;
  25. int index2=0;
  26. PFont orcFont;
  27.  
  28. void setup() {
  29.  
  30. size (1920, 1080); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION***
  31. smooth();
  32. myPort = new Serial(this,"COM14", 9600); // starts the serial communication
  33. myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
  34. orcFont = loadFont("OCRAExtended-30.vlw");
  35. }
  36.  
  37. void draw() {
  38.  
  39. fill(98,245,31);
  40. textFont(orcFont);
  41. // simulating motion blur and slow fade of the moving line
  42. noStroke();
  43. fill(0,4);
  44. rect(0, 0, width, height-height*0.065);
  45.  
  46. fill(98,245,31); // green color
  47. // calls the functions for drawing the radar
  48. drawRadar();
  49. drawLine();
  50. drawObject();
  51. drawText();
  52. }
  53.  
  54. void serialEvent (Serial myPort) { // starts reading data from the Serial Port
  55. // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  56. data = myPort.readStringUntil('.');
  57. data = data.substring(0,data.length()-1);
  58.  
  59. index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
  60. angle= 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
  61. distance= 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
  62.  
  63. // converts the String variables into Integer
  64. iAngle = int(angle);
  65. iDistance = int(distance);
  66. }
  67.  
  68. void drawRadar() {
  69. pushMatrix();
  70. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  71. noFill();
  72. strokeWeight(2);
  73. stroke(98,245,31);
  74. // draws the arc lines
  75. arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
  76. arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
  77. arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
  78. arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
  79. // draws the angle lines
  80. line(-width/2,0,width/2,0);
  81. line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
  82. line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
  83. line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
  84. line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
  85. line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
  86. line((-width/2)*cos(radians(30)),0,width/2,0);
  87. popMatrix();
  88. }
  89.  
  90. void drawObject() {
  91. pushMatrix();
  92. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  93. strokeWeight(9);
  94. stroke(255,10,10); // red color
  95. pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels
  96. // limiting the range to 40 cms
  97. if(iDistance<40){
  98. // draws the object according to the angle and the distance
  99. line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
  100. }
  101. popMatrix();
  102. }
  103.  
  104. void drawLine() {
  105. pushMatrix();
  106. strokeWeight(9);
  107. stroke(30,250,60);
  108. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  109. line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle
  110. popMatrix();
  111. }
  112.  
  113. void drawText() { // draws the texts on the screen
  114.  
  115. pushMatrix();
  116. if(iDistance>40) {
  117. noObject = "Out of Range";
  118. }
  119. else {
  120. noObject = "In Range";
  121. }
  122. fill(0,0,0);
  123. noStroke();
  124. rect(0, height-height*0.0648, width, height);
  125. fill(98,245,31);
  126. textSize(25);
  127.  
  128. text("10cm",width-width*0.3854,height-height*0.0833);
  129. text("20cm",width-width*0.281,height-height*0.0833);
  130. text("30cm",width-width*0.177,height-height*0.0833);
  131. text("40cm",width-width*0.0729,height-height*0.0833);
  132. textSize(40);
  133. text("Object: " + noObject, width-width*0.875, height-height*0.0277);
  134. text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277);
  135. text("Distance: ", width-width*0.26, height-height*0.0277);
  136. if(iDistance<40) {
  137. text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277);
  138. }
  139. textSize(25);
  140. fill(98,245,60);
  141. translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
  142. rotate(-radians(-60));
  143. text("30°",0,0);
  144. resetMatrix();
  145. translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
  146. rotate(-radians(-30));
  147. text("60°",0,0);
  148. resetMatrix();
  149. translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
  150. rotate(radians(0));
  151. text("90°",0,0);
  152. resetMatrix();
  153. translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
  154. rotate(radians(-30));
  155. text("120°",0,0);
  156. resetMatrix();
  157. translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
  158. rotate(radians(-60));
  159. text("150°",0,0);
  160. popMatrix();
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment