1#include <Servo.h>
2
3#define trigPin 22
4#define echoPin 24
5#define servoPin 9
6
7Servo myservo;
8int pos = 0;
9int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 10, 11, 12};
10int ledScanPos[] = {27, 29, 31, 33, 35, 37, 39, 41, 43, 45};
11int ledDist[] = {26, 28, 30, 32, 34, 36, 38, 40, 42, 44};
12int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
13int dt=200;
14int potPin=A0, mapped_value, distance_pot, distIndex;
15long duration, distance_cm;
16
17void setup() {
18 Serial.begin(9600);
19 myservo.attach(servoPin);
20 pinMode(trigPin, OUTPUT);
21 pinMode(echoPin, INPUT);
22 for (int i = 0; i < numLeds; i++) {
23 pinMode(ledPins[i], OUTPUT);
24 }
25}
26
27void loop() {
28
29 for (pos = 0; pos <= 180; pos += 18) {
30 myservo.write(pos);
31 delay(dt);
32
33 digitalWrite(trigPin, LOW);
34 delayMicroseconds(2);
35 digitalWrite(trigPin, HIGH);
36 delayMicroseconds(10);
37 digitalWrite(trigPin, LOW);
38
39 duration = pulseIn(echoPin, HIGH);
40 distance_cm = duration * 0.034 / 2;
41
42 mapped_value = analogRead(potPin);
43 distance_pot = map(mapped_value, 0, 1023, 10, 100);
44 distIndex = map(mapped_value, 0, 1023, 0, 10);
45
46 for(int k=0; k<10; k++){
47 digitalWrite(ledDist[k], k<distIndex ? HIGH:LOW);
48 }
49
50
51 int ledIndex = pos / 18;
52 digitalWrite(ledScanPos[ledIndex], HIGH);
53
54 if (distance_cm < distance_pot) {
55 digitalWrite(ledPins[ledIndex], HIGH);
56 } else {
57 digitalWrite(ledPins[ledIndex], LOW);
58 }
59
60 Serial.print("Angle: ");
61 Serial.print(pos);
62 Serial.print(" degrees, Distance: ");
63 Serial.print(distance_cm);
64 Serial.println(" cm");
65 }
66
67
68 for (pos = 180; pos >= 0; pos -= 18) {
69 myservo.write(pos);
70 delay(dt);
71
72 digitalWrite(trigPin, LOW);
73 delayMicroseconds(2);
74 digitalWrite(trigPin, HIGH);
75 delayMicroseconds(10);
76 digitalWrite(trigPin, LOW);
77
78 duration = pulseIn(echoPin, HIGH);
79 distance_cm = duration * 0.034 / 2;
80
81 mapped_value = analogRead(potPin);
82 distance_pot = map(mapped_value, 0, 1023, 10, 100);
83 distIndex = map(mapped_value, 0, 1023, 0, 10);
84
85 for(int k=0; k<10; k++){
86 digitalWrite(ledDist[k], k<distIndex ? HIGH:LOW);
87 }
88
89 int ledIndex = pos / 18;
90 digitalWrite(ledScanPos[ledIndex], LOW);
91
92 if (distance_cm < distance_pot) {
93 digitalWrite(ledPins[ledIndex], HIGH);
94 } else {
95 digitalWrite(ledPins[ledIndex], LOW);
96 }
97
98 Serial.print("Angle: ");
99 Serial.print(pos);
100 Serial.print(" degrees, Distance: ");
101 Serial.print(distance_cm);
102 Serial.println(" cm");
103 }
104
105
106
107 for (int i = 0; i < numLeds; i++) {
108 digitalWrite(ledScanPos[i], LOW);
109 }
110
111 delay(dt);
112}