├── .gitattributes ├── README.md └── Arduino_radar_HC_SR04 ├── Arduino └── Arduino_radar_server_arduino │ └── Arduino_radar_server_arduino.ino └── Processing-code └── Arduino_radar_client_processing └── Arduino_radar_client_processing.pde /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | arduino_radar 2 | ============= 3 | Arduino Code: 4 | https://github.com/faweiz/arduino_radar/blob/master/Arduino_radar_HC_SR04/Arduino/Arduino_radar_server_arduino/Arduino_radar_server_arduino.ino 5 | 6 | /* 7 | www.Faweiz.com/radar 8 | Radar Screen Visualisation for HC-SR04 9 | Sends sensor readings for every degree moved by the servo 10 | values sent to serial port to be picked up by Processing 11 | */ 12 | 13 | 14 | Processing Code: 15 | https://github.com/faweiz/arduino_radar/blob/master/Arduino_radar_HC_SR04/Processing-code/Arduino_radar_client_processing/Arduino_radar_client_processing.pde 16 | 17 | /* 18 | www.Faweiz.com/radar 19 | Radar Screen Visualisation for Sharp HC-SR04 20 | Maps out an area of what the HC-SR04 sees from a top down view. 21 | Takes and displays 2 readings, one left to right and one right to left. 22 | Displays an average of the 2 readings 23 | */ 24 | -------------------------------------------------------------------------------- /Arduino_radar_HC_SR04/Arduino/Arduino_radar_server_arduino/Arduino_radar_server_arduino.ino: -------------------------------------------------------------------------------- 1 | /* 2 | www.Faweiz.com/radar 3 | Radar Screen Visualisation for HC-SR04 4 | Sends sensor readings for every degree moved by the servo 5 | values sent to serial port to be picked up by Processing 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | #define TRIGGER_PIN 2 // Arduino pin 2 tied to trigger pin on the ultrasonic sensor. 12 | #define ECHO_PIN 3 // Arduino pin 3 tied to echo pin on the ultrasonic sensor. 13 | #define MAX_DISTANCE 150 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. 14 | #define SERVO_PWM_PIN 9 //set servo to Arduino's pin 9 15 | 16 | // means -angle .. angle 17 | #define ANGLE_BOUNDS 80 18 | #define ANGLE_STEP 1 19 | 20 | int angle = 0; 21 | 22 | // direction of servo movement 23 | // -1 = back, 1 = forward 24 | int dir = 1; 25 | 26 | Servo myservo; 27 | NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 28 | 29 | void setup() { 30 | Serial.begin(9600); // initialize the serial port: 31 | myservo.attach(SERVO_PWM_PIN); //set servo to Arduino's pin 9 32 | } 33 | 34 | void loop() { 35 | 36 | delay(50); 37 | // we must renormalize to positive values, because angle is from -ANGLE_BOUNDS .. ANGLE_BOUNDS 38 | // and servo value must be positive 39 | myservo.write(angle + ANGLE_BOUNDS); 40 | 41 | // read distance from sensor and send to serial 42 | getDistanceAndSend2Serial(angle); 43 | 44 | // calculate angle 45 | if (angle >= ANGLE_BOUNDS || angle <= -ANGLE_BOUNDS) { 46 | dir = -dir; 47 | } 48 | angle += (dir * ANGLE_STEP); 49 | } 50 | 51 | int getDistanceAndSend2Serial(int angle) { 52 | int cm = sonar.ping_cm(); 53 | Serial.print(angle, DEC); 54 | Serial.print(","); 55 | Serial.println(cm, DEC); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Arduino_radar_HC_SR04/Processing-code/Arduino_radar_client_processing/Arduino_radar_client_processing.pde: -------------------------------------------------------------------------------- 1 | /* 2 | www.Faweiz.com/radar 3 | Radar Screen Visualisation for Sharp HC-SR04 4 | Maps out an area of what the HC-SR04 sees from a top down view. 5 | Takes and displays 2 readings, one left to right and one right to left. 6 | Displays an average of the 2 readings 7 | */ 8 | 9 | import processing.serial.*; 10 | 11 | int SIDE_LENGTH = 1000; 12 | int ANGLE_BOUNDS = 80; 13 | int ANGLE_STEP = 2; 14 | int HISTORY_SIZE = 10; 15 | int POINTS_HISTORY_SIZE = 500; 16 | int MAX_DISTANCE = 50; 17 | 18 | int angle; 19 | int distance; 20 | 21 | /* choose either color or background image */ 22 | color bgcolor = color (255,0,0); 23 | PImage bgimage = loadImage("test.png"); 24 | 25 | int radius; 26 | float x, y; 27 | float leftAngleRad, rightAngleRad; 28 | 29 | float[] historyX, historyY; 30 | Point[] points; 31 | 32 | int centerX, centerY; 33 | 34 | String comPortString; 35 | Serial myPort; 36 | 37 | void setup() { 38 | size(SIDE_LENGTH, SIDE_LENGTH/2, P2D); 39 | noStroke(); 40 | //smooth(); 41 | rectMode(CENTER); 42 | 43 | radius = SIDE_LENGTH / 2; 44 | centerX = width / 2; 45 | centerY = height; 46 | angle = 0; 47 | leftAngleRad = radians(-ANGLE_BOUNDS) - HALF_PI; 48 | rightAngleRad = radians(ANGLE_BOUNDS) - HALF_PI; 49 | 50 | historyX = new float[HISTORY_SIZE]; 51 | historyY = new float[HISTORY_SIZE]; 52 | points = new Point[POINTS_HISTORY_SIZE]; 53 | 54 | myPort = new Serial(this, "COM11", 9600); 55 | myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line 56 | } 57 | 58 | 59 | void draw() { 60 | 61 | /* choose either bgcolor or bgimage */ 62 | 63 | background(bgimage); 64 | 65 | drawRadar(); 66 | drawFoundObjects(angle, distance); 67 | drawRadarLine(angle); 68 | 69 | /* draw the grid lines on the radar every 30 degrees and write their values 180, 210, 240 etc.. */ 70 | for (int i = 0; i <= 6; i++) { 71 | strokeWeight(1); 72 | stroke(0,255,0); 73 | line(radius, radius, radius + cos(radians(180+(30*i)))*SIDE_LENGTH/2, radius + sin(radians(180+(30*i)))*SIDE_LENGTH/2); 74 | fill(255, 255, 255); 75 | noStroke(); 76 | text(Integer.toString(0+(30*i)), radius + cos(radians(180+(30*i)))*SIDE_LENGTH/2, radius + sin(radians(180+(30*i)))*SIDE_LENGTH/2, 25, 50); 77 | } 78 | 79 | /* Write information text and values. */ 80 | noStroke(); 81 | fill(0); 82 | int Degrees=0; 83 | 84 | if (angle>0 & angle <80) 85 | { 86 | Degrees = angle+100; 87 | } 88 | else if (angle<0 & angle >-80) 89 | { 90 | Degrees = angle+80; 91 | } 92 | 93 | fill(0, 300, 0); 94 | text("Degrees: "+Integer.toString(Degrees), 100, 460, 100, 50); text("degree", 200, 460, 100, 50); // use Integet.toString to convert numeric to string as text() only outputs strings 95 | text("Subject Distance: "+Integer.toString(distance), 100, 480, 200, 30); text("mm", 200, 490, 100, 50); // text(string, x, y, width, height) 96 | text("Radar screen code at www.Faweiz.com/radar", 900, 480, 250, 50); 97 | 98 | text("0", 620, 500, 250, 50); 99 | text("50 mm", 600, 420, 250, 50); 100 | 101 | text("100 mm", 600, 320, 250, 50); 102 | 103 | text("150 mm", 600, 220, 250, 50); 104 | 105 | text("200 mm", 600, 120, 250, 50); 106 | 107 | text("250 mm", 600, 040, 250, 50); 108 | 109 | noFill(); 110 | rect(70,60,200,200); 111 | fill(0, 250, 0); 112 | text("Screen Key:", 100, 50, 150, 50); 113 | fill(0,50,0); 114 | rect(30,53,10,10); 115 | text("Far", 115, 70, 150, 50); 116 | fill(0,110,0); 117 | rect(30,73,10,10); 118 | text("Near", 115, 90, 150, 50); 119 | fill(0,170,0); 120 | rect(30,93,10,10); 121 | text("Close", 115, 110, 150, 50); 122 | } 123 | 124 | void drawRadarLine(int angle) { 125 | 126 | float radian = radians(angle); 127 | x = radius * sin(radian); 128 | y = radius * cos(radian); 129 | 130 | float px = centerX + x; 131 | float py = centerY - y; 132 | 133 | historyX[0] = px; 134 | historyY[0] = py; 135 | 136 | int Degrees=0; 137 | if (angle>0 & angle <80) 138 | { 139 | Degrees = angle+100; 140 | } 141 | else if (angle<0 & angle >-80) 142 | { 143 | Degrees = angle+80; 144 | } 145 | 146 | //get rgb color from http://www.rapidtables.com/web/color/RGB_Color.htm 147 | fill(0, 153, 0); 148 | 149 | float b = centerY; 150 | 151 | arc(centerX,centerY,SIDE_LENGTH, SIDE_LENGTH,radians(angle-100),radians(angle-90)); 152 | 153 | shiftHistoryArray(); 154 | } 155 | 156 | void drawFoundObjects(int angle, int distance) { 157 | 158 | if (distance > 0) { 159 | float radian = radians(angle); 160 | x = distance * sin(radian); 161 | y = distance * cos(radian); 162 | 163 | int px = (int)(centerX + x); 164 | int py = (int)(centerY - y); 165 | 166 | points[0] = new Point(px, py); 167 | } 168 | else { 169 | points[0] = new Point(0, 0); 170 | } 171 | for (int i=0;i 1; i--) { 209 | 210 | historyX[i-1] = historyX[i-2]; 211 | historyY[i-1] = historyY[i-2]; 212 | } 213 | } 214 | 215 | void shiftPointsArray() { 216 | 217 | for (int i = POINTS_HISTORY_SIZE; i > 1; i--) { 218 | 219 | Point oldPoint = points[i-2]; 220 | if (oldPoint != null) { 221 | 222 | Point point = new Point(oldPoint.x, oldPoint.y); 223 | points[i-1] = point; 224 | } 225 | } 226 | } 227 | 228 | void serialEvent(Serial cPort) { 229 | 230 | comPortString = cPort.readStringUntil('\n'); 231 | if (comPortString != null) { 232 | 233 | comPortString=trim(comPortString); 234 | String[] values = split(comPortString, ','); 235 | 236 | try { 237 | 238 | angle = Integer.parseInt(values[0]); 239 | distance = int(map(Integer.parseInt(values[1]), 1, MAX_DISTANCE, 1, radius)); 240 | } catch (Exception e) {} 241 | } 242 | } 243 | 244 | class Point { 245 | int x, y; 246 | 247 | Point(int xPos, int yPos) { 248 | x = xPos; 249 | y = yPos; 250 | } 251 | 252 | int getX() { 253 | return x; 254 | } 255 | 256 | int getY() { 257 | return y; 258 | } 259 | } 260 | --------------------------------------------------------------------------------