-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple Pot_GUI-Test
More file actions
94 lines (75 loc) · 2.69 KB
/
Simple Pot_GUI-Test
File metadata and controls
94 lines (75 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
###################################################################################################################
# #
# " Simple Pot_GUI " #
# #
###################################################################################################################
# import libraries
import serial
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# Global variables
global is_plotting
is_plotting = False
# Initialize serial communication
try:
Arduino = serial.Serial('COM3', 9600, timeout=1) # Change COM port as needed for Windows
except serial.SerialException as e:
print(f"Error opening serial port: {e}")
exit()
# Variables for plotting
x_data = []
y_data = []
# Create figure for plotting
fig, ax = plt.subplots(figsize=(15, 10))
line, = ax.plot([], [], lw=2)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Potincimeter Value')
ax.set_title('Real-Time Serial Data')
# Function to update plot
def update_plot(frame):
global is_plotting
if is_plotting and Arduino.in_waiting > 0:
data = Arduino.readline().decode('ascii').strip()
try:
value = int(data)
x_data.append(time.time())
y_data.append(value)
line.set_data(x_data, y_data)
ax.relim()
ax.autoscale_view()
except ValueError:
print(f"Invalid data: {data}")
return line,
# Animation function
def start_plotting():
global is_plotting
is_plotting = True
def stop_plotting():
global is_plotting
is_plotting = False
ani = FuncAnimation(fig, update_plot, interval=1)
# Create GUI
root = tk.Tk()
root.title('Real-Time Serial Plotter')
# Frame for buttons
button_frame = ttk.Frame(root)
button_frame.pack(padx=10, pady=10)
start_button = ttk.Button(button_frame, text="Start", command=start_plotting)
start_button.pack(side=tk.LEFT, padx=10)
stop_button = ttk.Button(button_frame, text="Stop", command=stop_plotting)
stop_button.pack(side=tk.LEFT, padx=10)
# Canvas to display plot
canvas_widget = FigureCanvasTkAgg(fig, master=root)
canvas_widget.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# Show plot
plt.ion() # Turn on interactive mode
plt.show()
# Start tkinter event loop
root.mainloop()
# Clean up
is_plotting = False
Arduino.close()