Components and supplies
9G Micro Servo
220 Ohm Resistor
Arduino UNO
Ultrasound sensor HC-SR04
Buzzer
Jumper wire
USB 2.0 Cable Type A/B
Red LEDs
Slide Switch
Green LEDs
1k Resistor
Apps and platforms
Arduino IDE
Processing 3
Project description
Code
Radar script for Processing
java
1import processing.serial.*; 2import java.awt.event.KeyEvent; 3import java.io.IOException; 4 5Serial myPort; 6 7String angle = ""; 8String distance = ""; 9String data = ""; 10 11String noObject; 12 13float pixsDistance; 14 15int iAngle, iDistance; 16 17int index1 = 0; 18int index2 = 0; 19 20PFont orcFont; 21 22 23void setup() { 24 size(1920, 1080); // !!! change these values to your screen's resolution !!! 25 smooth(); 26 myPort = new Serial(this, "COM6", 9600); // !!! CHANGE THE SECOND PARAMETER TO YOUR CUSTOM COM PORT !!! 27 myPort.bufferUntil('.'); // actually reading: angle,distance. 28} 29 30void draw() { 31 fill(98, 245, 31); 32 33 noStroke(); 34 35 fill(0, 4); 36 rect(0, 0, width, height-height * 0.065); 37 38 fill(98, 245, 31); // green 39 40 drawRadar(); 41 drawLine(); 42 drawObject(); 43 drawText(); 44} 45 46void serialEvent(Serial myPort) { // reading data from the Serial Port 47 data = myPort.readStringUntil('.'); 48 data = data.substring(0, data.length() - 1); 49 50 index1 = data.indexOf(","); 51 angle= data.substring(0, index1); 52 distance= data.substring(index1 + 1, data.length()); 53 54 iAngle = int(angle); 55 iDistance = int(distance); 56} 57 58void drawRadar() { 59 pushMatrix(); 60 translate(width / 2, height - height * 0.074); 61 noFill(); 62 strokeWeight(2); 63 stroke(98, 245, 31); 64 65 // drawing the arc lines 66 arc(0, 0, (width - width * 0.0625), (width - width * 0.0625), PI, TWO_PI); 67 arc(0, 0, (width - width * 0.27), (width - width * 0.27), PI, TWO_PI); 68 arc(0, 0, (width - width * 0.479), (width - width * 0.479), PI, TWO_PI); 69 arc(0, 0, (width - width * 0.687), (width - width * 0.687), PI, TWO_PI); 70 71 // drawing the angle lines 72 line(-width / 2, 0, width / 2, 0); 73 line(0, 0, (-width / 2) * cos(radians(30)), (-width / 2) * sin(radians(30))); 74 line(0, 0, (-width / 2) * cos(radians(60)), (-width / 2) * sin(radians(60))); 75 line(0, 0, (-width / 2) * cos(radians(90)), (-width / 2) * sin(radians(90))); 76 line(0, 0, (-width / 2) * cos(radians(120)), (-width / 2) * sin(radians(120))); 77 line(0, 0, (-width / 2) * cos(radians(150)), (-width / 2) * sin(radians(150))); 78 line((-width / 2) * cos(radians(30)), 0, width / 2, 0); 79 popMatrix(); 80} 81 82void drawObject() { 83 pushMatrix(); 84 translate(width / 2, height - height * 0.074); 85 strokeWeight(9); 86 stroke(255, 10, 10); // red 87 pixsDistance = iDistance*((height - height * 0.1666) * 0.025); // converting the distance from cm to pixels 88 89 if (iDistance<40) { // limiting the range to 40 cm 90 line(pixsDistance * cos(radians(iAngle)), -pixsDistance * sin(radians(iAngle)), (width - width * 0.505) * cos(radians(iAngle)), -(width - width * 0.505) * sin(radians(iAngle))); 91 } 92 popMatrix(); 93} 94 95void drawLine() { 96 pushMatrix(); 97 strokeWeight(9); 98 stroke(30, 250, 60); 99 translate(width / 2, height - height * 0.074); 100 line(0, 0, (height - height * 0.12) * cos(radians(iAngle)), -(height - height * 0.12) * sin(radians(iAngle))); 101 popMatrix(); 102} 103 104// drawing the text on the screen 105void drawText() { 106 pushMatrix(); 107 if (iDistance>40) { 108 noObject = "Out of Range"; 109 } else { 110 noObject = "In Range"; 111 } 112 fill(0, 0, 0); 113 noStroke(); 114 rect(0, height - height * 0.0648, width, height); 115 fill(98, 245, 31); 116 textSize(25); 117 118 text("10cm", width - width * 0.3854, height - height * 0.0833); 119 text("20cm", width - width * 0.281, height - height * 0.0833); 120 text("30cm", width - width * 0.177, height - height * 0.0833); 121 text("40cm", width - width * 0.0729, height - height * 0.0833); 122 textSize(40); 123 text("Angle: " + iAngle + " °", width - width * 0.48, height - height * 0.0277); 124 text("Distance: ", width - width * 0.26, height - height * 0.0277); 125 126 if (iDistance<40) { 127 text(" " + iDistance + " cm", width - width * 0.225, height - height * 0.0277); 128 fill(255, 10, 10); 129 text("WARNING!", width - width * 0.875, height - height * 0.0277); 130 } 131 132 textSize(25); 133 fill(98, 245, 60); 134 translate((width - width * 0.4994) + width / 2 * cos(radians(30)), (height - height * 0.0907) - width / 2 * sin(radians(30))); 135 rotate(-radians(-60)); 136 text("30°", 0, 0); 137 resetMatrix(); 138 translate((width - width * 0.503) + width / 2 * cos(radians(60)), (height - height * 0.0888) - width / 2 * sin(radians(60))); 139 rotate(-radians(-30)); 140 text("60°", 0, 0); 141 resetMatrix(); 142 translate((width - width * 0.507) + width / 2 * cos(radians(90)), (height - height * 0.0833) - width / 2 * sin(radians(90))); 143 rotate(radians(0)); 144 text("90°", 0, 0); 145 resetMatrix(); 146 translate(width - width * 0.513 + width / 2 * cos(radians(120)), (height - height * 0.07129) -width / 2 * sin(radians(120))); 147 rotate(radians(-30)); 148 text("120°", 0, 0); 149 resetMatrix(); 150 translate((width - width * 0.5104) + width / 2 * cos(radians(150)), (height - height * 0.0574) - width / 2 * sin(radians(150))); 151 rotate(radians(-60)); 152 text("150°", 0, 0); 153 popMatrix(); 154}
Radar script for Arduino IDE
cpp
1#include <Servo.h> 2 3// variables 4const int trigPin = 2; 5const int echoPin = 9; 6 7long duration; 8int distance; 9 10int buzzpin = 7; 11int buzzState = LOW; 12 13int ledRed = 3; 14int ledGreen = 4; 15 16int switchpin = 10; 17int ledStatus = 8; 18 19byte leds = 0; 20 21bool canSpin = true; 22 23unsigned long previousMillis = 0; 24 25const long intervalFar = 250; 26const long intervalClose = 50; 27const long intervalIdle = 1250; 28 29Servo myServo; 30 31 32void setup() { 33 pinMode(trigPin, OUTPUT); 34 pinMode(echoPin, INPUT); 35 Serial.begin(9600); 36 myServo.attach(12); 37 pinMode(buzzpin, OUTPUT); 38 Serial.begin(9600); 39 pinMode(ledRed, OUTPUT); 40 pinMode(ledGreen, OUTPUT); 41 pinMode(switchpin, INPUT); 42 pinMode(ledStatus, OUTPUT); 43} 44 45// turning the radar on-off 46void loop() { 47 if (digitalRead(switchpin) == HIGH){ 48 digitalWrite(ledStatus, LOW); 49 StartScan(); 50 } 51 if (digitalRead(switchpin) == LOW){ 52 digitalWrite(ledStatus, HIGH); 53 } 54} 55 56// calculating the distance to a detected object 57int calculateDistance(){ 58 digitalWrite(trigPin, LOW); 59 delayMicroseconds(2); 60 digitalWrite(trigPin, HIGH); 61 delayMicroseconds(10); 62 digitalWrite(trigPin, LOW); 63 duration = pulseIn(echoPin, HIGH); // reads echoPin and returns the sound wave travel time (ms) 64 distance = duration * 0.034 / 2; 65 return distance; 66} 67 68// start scanning for objects 69void StartScan() { 70 // checking for every degree 71 72 // first from right to left 73 for (int i = 15; i <= 165; i++) { 74 myServo.write(i); 75 delay(30); 76 distance = calculateDistance(); 77 78 // checking if something is detected in the 40 cm range 79 if (distance <= 40 && distance > 20) { 80 unsigned long currentMillis = millis(); 81 82 if (currentMillis - previousMillis >= intervalFar) { 83 previousMillis = currentMillis; 84 85 if (buzzState == LOW) { 86 buzzState = HIGH; 87 } else { 88 buzzState = LOW; 89 } 90 91 digitalWrite(buzzpin, buzzState); 92 digitalWrite(ledRed, buzzState); 93 digitalWrite(ledGreen, HIGH); 94 } 95 } else if (distance <= 20 && distance > 0) { 96 unsigned long currentMillis = millis(); 97 98 if (currentMillis - previousMillis >= intervalClose) { 99 previousMillis = currentMillis; 100 101 if (buzzState == LOW) { 102 buzzState = HIGH; 103 } else { 104 buzzState = LOW; 105 } 106 107 digitalWrite(buzzpin, buzzState); 108 digitalWrite(ledRed, buzzState); 109 digitalWrite(ledGreen, HIGH); 110 } 111 } else if (distance > 40) { 112 digitalWrite(buzzpin, LOW); 113 digitalWrite(ledGreen, LOW); 114 digitalWrite(ledRed, HIGH); 115 } 116 117 Serial.print(i); 118 Serial.print(","); 119 Serial.print(distance); 120 Serial.print("."); 121 } 122 123 // then from left to right 124 for (int i = 165; i >= 15; i--) { 125 myServo.write(i); 126 delay(30); 127 distance = calculateDistance(); 128 129 if (distance <= 40 && distance > 20) { 130 unsigned long currentMillis = millis(); 131 132 if (currentMillis - previousMillis >= intervalFar) { 133 previousMillis = currentMillis; 134 135 if (buzzState == LOW) { 136 buzzState = HIGH; 137 } else { 138 buzzState = LOW; 139 } 140 141 digitalWrite(buzzpin, buzzState); 142 digitalWrite(ledRed, buzzState); 143 digitalWrite(ledGreen, HIGH); 144 } 145 } else if (distance <= 20 && distance > 0) { 146 unsigned long currentMillis = millis(); 147 148 if (currentMillis - previousMillis >= intervalClose) { 149 previousMillis = currentMillis; 150 151 if (buzzState == LOW) { 152 buzzState = HIGH; 153 } else { 154 buzzState = LOW; 155 } 156 157 digitalWrite(buzzpin, buzzState); 158 digitalWrite(ledRed, buzzState); 159 digitalWrite(ledGreen, HIGH); 160 } 161 } else if (distance > 40) { 162 digitalWrite(buzzpin, LOW); 163 digitalWrite(ledGreen, LOW); 164 digitalWrite(ledRed, HIGH); 165 } 166 167 Serial.print(i); 168 Serial.print(","); 169 Serial.print(distance); 170 Serial.print("."); 171 } 172}
Downloadable files
Radar Wiring
Radar Sketch.jpg
Comments
Only logged in users can leave comments