1#include <Servo.h>
2
3Servo myServo;
4
5const int trigPin = 2;
6const int echoPin = 9;
7
8int angle = 0;
9int direction = 1;
10
11int minAngle = 0;
12int maxAngle = 75;
13
14unsigned long lastServoMove = 0;
15unsigned long lastSensorRead = 0;
16
17int distance = 0;
18
19void setup() {
20 Serial.begin(9600);
21
22 pinMode(trigPin, OUTPUT);
23 pinMode(echoPin, INPUT);
24
25 myServo.attach(11);
26}
27
28void loop() {
29
30 // ✅ SMOOTH + NOT TOO FAST SERVO
31 if (millis() - lastServoMove >= 20) { // balanced speed
32 lastServoMove = millis();
33
34 angle += direction; // step = 1 (smooth)
35
36 if (angle >= maxAngle) {
37 angle = maxAngle;
38 direction = -1;
39 }
40
41 if (angle <= minAngle) {
42 angle = minAngle;
43 direction = 1;
44 }
45
46 myServo.write(angle);
47 }
48
49 // 📡 SENSOR READ
50 if (millis() - lastSensorRead >= 50) {
51 lastSensorRead = millis();
52
53 distance = getDistance();
54
55 // 🔴 ONLY UNDER 10 CM
56 if (distance > 10 || distance == 0) {
57 distance = 0;
58 }
59
60 Serial.print(angle);
61 Serial.print(",");
62 Serial.print(distance);
63 Serial.print(".");
64 }
65}
66
67// 📏 DISTANCE FUNCTION (STABLE)
68int getDistance() {
69
70 digitalWrite(trigPin, LOW);
71 delayMicroseconds(2);
72
73 digitalWrite(trigPin, HIGH);
74 delayMicroseconds(10);
75 digitalWrite(trigPin, LOW);
76
77 long duration = pulseIn(echoPin, HIGH, 25000);
78
79 if (duration == 0) return 0;
80
81 int dist = duration * 0.034 / 2;
82
83 return dist;
84}
85
86
87
88
89
90
91// processing code
92
93
94
95import processing.serial.*;
96
97Serial myPort;
98
99String data = "";
100
101int iAngle = 0;
102int iDistance = 0;
103int index1;
104
105float pixsDistance;
106
107PFont font;
108
109void setup() {
110 size(1280, 720);
111 smooth();
112
113 println(Serial.list());
114
115 // ✅ SELECT CORRECT PORT (change index if needed)
116 myPort = new Serial(this, Serial.list()[0], 9600);
117 myPort.bufferUntil('.');
118
119 font = createFont("Arial", 20, true);
120 textFont(font);
121}
122
123void draw() {
124
125 // 🔥 Smooth fade background
126 fill(0, 15);
127 noStroke();
128 rect(0, 0, width, height);
129
130 fill(0, 255, 0);
131
132 drawRadar();
133 drawLine();
134 drawObject();
135 drawText();
136}
137
138// ✅ READ SERIAL DATA
139void serialEvent(Serial myPort) {
140 data = myPort.readStringUntil('.');
141
142 if (data != null) {
143 data = trim(data);
144
145 if (data.contains(",")) {
146 index1 = data.indexOf(',');
147
148 try {
149 iAngle = int(data.substring(0, index1));
150 iDistance = int(data.substring(index1 + 1));
151 } catch (Exception e) {
152 // ignore bad data
153 }
154 }
155 }
156}
157
158// 🟢 RADAR GRID
159void drawRadar() {
160 pushMatrix();
161 translate(width/2, height - height * 0.08);
162
163 noFill();
164 stroke(0, 255, 0);
165 strokeWeight(2);
166
167 arc(0, 0, width, width, PI, TWO_PI);
168 arc(0, 0, width * 0.75, width * 0.75, PI, TWO_PI);
169 arc(0, 0, width * 0.5, width * 0.5, PI, TWO_PI);
170 arc(0, 0, width * 0.25, width * 0.25, PI, TWO_PI);
171
172 line(-width/2, 0, width/2, 0);
173
174 popMatrix();
175}
176
177// 🟢 SCANNING LINE
178void drawLine() {
179 pushMatrix();
180 translate(width/2, height - height * 0.08);
181
182 strokeWeight(4);
183
184 if (iDistance > 0) {
185 stroke(255, 0, 0); // red if object
186 } else {
187 stroke(0, 255, 0);
188 }
189
190 line(0, 0,
191 (height * 0.9) * cos(radians(iAngle)),
192 -(height * 0.9) * sin(radians(iAngle)));
193
194 popMatrix();
195}
196
197// 🔴 OBJECT DOT (ONLY ≤10 cm)
198void drawObject() {
199 pushMatrix();
200 translate(width/2, height - height * 0.08);
201
202 if (iDistance > 0 && iDistance <= 10) {
203
204 pixsDistance = map(iDistance, 0, 10, 0, height * 0.9);
205
206 stroke(255, 0, 0);
207 strokeWeight(10);
208
209 point(
210 pixsDistance * cos(radians(iAngle)),
211 -pixsDistance * sin(radians(iAngle))
212 );
213 }
214
215 popMatrix();
216}
217
218// 📝 TEXT INFO
219void drawText() {
220 fill(0);
221 noStroke();
222 rect(0, height - 60, width, 60);
223
224 fill(0, 255, 0);
225 textSize(22);
226
227 text("Angle: " + iAngle + "°", 50, height - 20);
228 text("Distance: " + iDistance + " cm", 250, height - 20);
229
230 if (iDistance > 0) {
231 text("OBJECT DETECTED", 500, height - 20);
232 } else {
233 text("NO OBJECT", 500, height - 20);
234 }
235}