lloydd4 lloydd4 - 22 days ago 9
Python Question

How to execute a while loop multiple times?

I have a program that acts as a GUI for several arduino boards. I'm trying to show the results of the sensors in a dynamic graph.

Question: Why is my GUI unit not printed 5 times ? - it did work without the graph

My idea of the end result is the following:
enter image description here
Unfortunately my result is:

enter image description here
I can't seem to figure out why my code isn't printing the GUI 5 times.
This is my code for the graphs:

# pre-load dummy data
for i in range(0, 30):
self.values.append(0)

for a in range(0, 30):
self.values2.append(0)

while True:
while (self.serialArduino.inWaiting() == 0):
pass
self.data = self.serialArduino.readline().decode('utf-8') # maakt connectie met arduino

self.valueRead, self.valueRead2 = [int(x.encode()) for x in self.data.split(",")]

self.valueInInt = int(self.valueRead)
self.valueInInt2 = int(self.valueRead2)
print("lux %d" % self.valueInInt)
print("temp %d" % self.valueInInt2)

self.values.append(self.valueInInt)
self.values.pop(0)
self.values2.append(self.valueInInt2)
self.values2.pop(0)
drawnow(self.plotValues)

self.f.tight_layout(pad=0.8, w_pad=0.5, h_pad=1.0)
self.canvas = FigureCanvasTkAgg(self.f, master=self.frame1, )
self.canvas.get_tk_widget().grid(row=8, column=0, columnspan=2)

def plotValues(self):
self.f = plt.Figure(figsize=(4, 5), dpi=90)
self.a = self.f.add_subplot(211)
self.a.plot(self.values2, 'bx-', label='TEMP')
self.a.set_title('Tempertuur')
self.a.set_xlabel('Uren')
self.a.set_ylabel('Celsius')
self.a.grid(True)


self.b = self.f.add_subplot(212)
self.b.plot(self.values, 'rx-', label='LUX')
self.b.set_title('Lux')
self.b.set_xlabel('Uren')
self.b.set_ylabel('Lichtintensiteit')
self.b.grid(True)


Constructor for the class that contains the graph:

self.values = []
self.values2 = []

self.plt = plt.ion()
self.cnt = 0

self.serialArduino = serial.Serial('COM7', 9600)

self.frame1 = Frame(master)
self.frame1.pack(side=LEFT)


The GUI itself is call from a different class:

class GUI_Root:
def __init__(self):
print("hoi")
window = Tk()
window.title("Project: Embedded Systems")

rootframe = Frame(window, width=1800, height=750)
rootframe.pack()
for y in range(0, 5):
Unit(rootframe)

window.mainloop()

Answer

I've figured out a solution. It might not be the code, but it works. Every Gui Unit represents a Arduino Unit. So I check if a comport is used. For every comport that is being used, a Gui unit is created. This solved my issue.

class GUI_Root:
def __init__(self):
    print("hoi")
    window = Tk()
    print("test1")
    window.title("Project: Embedded Systems")
    print("test2")
    rootframe = Frame(window, width=1800, height=750)
    rootframe.pack()


    try:
        Unit(rootframe, 'COM3' )

    except: print("Com3 is niet aangesloten")

    try:
        Unit(rootframe, 'COM4' )
    except:
        print("Com4 is niet aangesloten")

    try:
        Unit(rootframe, 'COM5' )
    except:
        print("Com5 is niet aangesloten")

    try:
        Unit(rootframe, 'COM6' )
    except:
        print("Com6 is niet aangesloten")

    try:
        Unit(rootframe, 'COM7' )
    except:
        print("Com7 is niet aangesloten")

    try:
        Unit(rootframe, 'COM8' )
    except:
        print("Com8 is niet aangesloten")

    window.mainloop()

The issue was in the class GUI_Root.

Comments