0

I have some code which I am trying to run, basically the system should draw the tkinter canvas then constantly monitor the headset results - currently it seems to only run the code once rather than (as I expected) loop through the if statement.

Any suggestions would be most appreciated.

A

import Tkinter as tk
import serial
import mindwave
import time

headset = mindwave.Headset('/dev/ttyUSB0', 'AF66')

while headset.status != 'connected':
    time.sleep(0.5)
    if headset.status == 'standby':
        headset.connect()
        print ("Retrying connect...")
print(headset.status)

arduino = serial.Serial('/dev/ttyACM0', 9600)
count = 0
value = '0'
points = [
    [250,250,350,250,350,350,250,350],
    [300,100,500,500,100,500],
    [100,100,100,500,500,300],    
    [100,100,500,100,300,500],
    [100,300,500,100,500,500],
]

root = tk.Tk()

# set window to middle of screen
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
xcoord = screen_width//2 - 300
ycoord = screen_height//2 - 300
root.geometry("%dx%d+%d+%d" % (600,600,xcoord,ycoord))

#set up canvas
canvas1 = tk.Canvas(root, relief=tk.FLAT, background="blue")
canvas1.pack(fill=tk.BOTH, expand=1)
# create the polygon with tag "button"
canvas1.create_polygon(points[count], fill="darkgreen", outline="yellow", tag="button")

#monitor attention results
if headset.attention > 70:
    print("Attention: %s, Meditation: %s" % (headset.attention, headset.meditation))
else:
    print("attention too low")
time.sleep(.2)
Andy
  • 3
  • 2
  • The tkinter window will only appear once you call `root.mainloop()`, anything before that will still apply but nothing will be visible until mainloop. Anything after that won't run until you close the window. See [this post](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) for how to run code alongside the mainloop, if thats what you need. If you need something else please consider clarifying your question further :D – dwb Jul 20 '20 at 09:27
  • There is no loop statement for checking the `headset.attention` (only one if statement), so why do you expect there is *"constantly monitor the headset result"*. – acw1668 Jul 20 '20 at 10:37
  • I am more used to programming Arduino where code autoloops - i guess i expected the same in Python - i guess that explains the lack of continuous (thanks) the code is intended to monitor the attention level and send a signal based on the direction shown on the canvas when this value goes above a certain level. – Andy Jul 20 '20 at 18:29
  • Thanks dantechguy Thats going now as I want it to, and everything is almost done just my blink function to add now which should fit into the same part. /cheer – Andy Jul 20 '20 at 18:53

0 Answers0