Skip to content

Commit 7d0da6d

Browse files
authored
Add files via upload
1 parent 7ab58db commit 7d0da6d

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

1.png

21.6 KB
Loading

2.png

23.4 KB
Loading

Testing map.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import serial
2+
import matplotlib.pyplot as plt
3+
4+
# Open the serial port
5+
ser = serial.Serial('COM3', 9600) # replace 'COM3' with your port
6+
7+
# Set up the figure
8+
plt.ion() # enable interactive drawing
9+
fig = plt.figure()
10+
ax = fig.add_subplot(111)
11+
12+
distances = []
13+
14+
while True:
15+
try:
16+
# Read a line from the serial port
17+
line = ser.readline().decode().strip()
18+
19+
# Convert the line to a float
20+
distance = float(line)
21+
22+
# Ignore readings that exceed 400 cm
23+
if distance > 400:
24+
continue
25+
26+
# Add the distance to the list
27+
distances.append(distance)
28+
29+
# Clear the plot
30+
ax.clear()
31+
32+
# Plot the distances
33+
ax.plot(distances)
34+
35+
# Display the distance as text
36+
ax.text(1, 1, f'Distance: {distance} cm', horizontalalignment='right', verticalalignment='top', transform=ax.transAxes, fontsize=15)
37+
38+
# Set the limits
39+
ax.set_ylim(2, 400) # set range from 2 cm to 400 cm
40+
41+
# Redraw the plot
42+
plt.draw()
43+
plt.pause(0.001)
44+
45+
except ValueError:
46+
# Ignore lines that don't parse to a float
47+
pass
48+
49+
except KeyboardInterrupt:
50+
# Exit the loop when Ctrl+C is pressed
51+
break
52+
53+
# Close the serial port
54+
ser.close()

sketch.ino

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#define TRIG_PIN 12
2+
#define ECHO_PIN 11
3+
4+
void setup() {
5+
pinMode(TRIG_PIN, OUTPUT);
6+
pinMode(ECHO_PIN, INPUT);
7+
Serial.begin(9600);
8+
}
9+
10+
void loop() {
11+
long duration, distance;
12+
digitalWrite(TRIG_PIN, LOW);
13+
delayMicroseconds(2);
14+
digitalWrite(TRIG_PIN, HIGH);
15+
delayMicroseconds(10);
16+
digitalWrite(TRIG_PIN, LOW);
17+
duration = pulseIn(ECHO_PIN, HIGH);
18+
distance = (duration/2) / 29.1;
19+
Serial.println(distance); // Distance
20+
delay(1000);
21+
}

0 commit comments

Comments
 (0)