I am an absolute beginner for coding and I'm currently trying to design a turbine performance display using an Arduino Uno that will communicate the voltage reading using the input of pin A1.
This reading is then sent to Python and then the Python GUI is to display the readings for current etc based on the original serial input from Arduino. Based on the little I learnt, I did the simple coding from Arduino side and then designed a GUI using figma and converted it. My issue is, the data is only displayed as a sample for voltage and current but I am unable to constantly refresh the data automatically as the current or voltage values change. What am I missing? When I tried doing a loop it bugs out the entire code.
This is the code I have been trying with the loop:
# Tkinter-Designer
from pathlib import Path
# from tkinter import *
# Explicit imports to satisfy Flake8
import serial
ser = serial.Serial ('COM4', baudrate = 9600, timeout = 1)
while 1:
arduinoData = ser.readline().decode('ascii')
int(float(arduinoData))
voltageval = int(arduinoData)/204.6
currentval = int(arduinoData)/1.96
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path(r"C:\Users\haeyl\Downloads\tkinter\build\assets\frame0")
def relative_to_assets(path: str) -> Path:
return ASSETS_PATH / Path(path)
window = Tk()
window.geometry("1280x720")
window.configure(bg = "#600000")
canvas = Canvas(
window,
bg = "#600000",
height = 720,
width = 1280,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
canvas.place(x = 0, y = 0)
canvas.create_rectangle(
0.0,
0.0,
1280.0,
90.0,
fill="#474646",
outline="")
canvas.create_text(
423.0,
37.0,
anchor="nw",
text="TURBINE PERFORMANCE DISPLAY",
fill="#FFFFFF",
font=("Inter", 24 * -1)
)
image_image_1 = PhotoImage(
file=relative_to_assets("image_1.png"))
image_1 = canvas.create_image(
262.0,
182.0,
image=image_image_1
)
image_image_2 = PhotoImage(
file=relative_to_assets("image_2.png"))
image_2 = canvas.create_image(
909.0,
182.0,
image=image_image_2
)
image_image_3 = PhotoImage(
file=relative_to_assets("image_3.png"))
image_3 = canvas.create_image(
909.0,
344.0,
image=image_image_3
)
image_image_4 = PhotoImage(
file=relative_to_assets("image_4.png"))
image_4 = canvas.create_image(
262.0,
348.0,
image=image_image_4
)
image_image_5 = PhotoImage(
file=relative_to_assets("image_5.png"))
image_5 = canvas.create_image(
262.0,
519.0,
image=image_image_5
)
canvas.create_text(
105.0,
168.0,
anchor="nw",
text="VOLTAGE",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
752.0,
168.0,
anchor="nw",
text="RPM",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
740.0,
330.0,
anchor="nw",
text="POWER",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
105.0,
335.0,
anchor="nw",
text="CURRENT",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
105.0,
506.0,
anchor="nw",
text="TORQUE",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
323.0,
168.0,
anchor="nw",
text=(0 + voltageval),
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
970.0,
168.0,
anchor="nw",
text="rpmval",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
970.0,
330.0,
anchor="nw",
text="0 W ",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
323.0,
335.0,
anchor="nw",
text=(0 + currentval),
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
canvas.create_text(
323.0,
506.0,
anchor="nw",
text="0 N/M",
fill="#FFFFFF",
font=("Inter", 32 * -1)
)
image_image_6 = PhotoImage(
file=relative_to_assets("image_6.png"))
image_6 = canvas.create_image(
1132.0,
563.0,
image=image_image_6
)
button_image_1 = PhotoImage(
file=relative_to_assets("button_1.png"))
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=lambda: print("button_1 clicked"),
relief="flat"
)
button_1.place(
x=709.0,
y=476.0,
width=264.0,
height=108.0
)
window.resizable(False, False)
window.mainloop()
ser.readline()is a blocking function, it is better to execute serial port reading in a thread and transfer data to main thread usingqueue. The main GUI application should read data from the queue constantly using.after().