pySerial reading and writing with arduino

82 Views Asked by At

I've got a question. I'm working on an Arduino project, and at this moment, I have a reader that writes all the data from my sensors in Tkinter. However, I also need to control digitalWrite with buttons. For example, while reading data from sensors every 1 second, when I click the button, I want to set a chosen pin to HIGH. The only method that I know is serial.write(bytes...), but I think it collides with reading data from sensors. Do you have any ideas on how to get over this issue?

this is a bit of my code:

def read_data():
    dataPacket = arduinoData.readline().decode('utf-8')
    dataSplit = dataPacket.split(',')
    ....
    window.after(2000, read_data)

def pumpOn():
    arduinoData.write(bytes('N', 'UTF-8'))
    
def pumpOff():
    arduinoData.write(bytes('F', 'UTF-8'))

btn_pumpOn = tk.Button(window, text ="Włącz pompę", command = pumpOn())
btn_pumpOn.pack()

btn_pumpOff = tk.Button(window, text ="Wyłącz pompę", command = pumpOff())
btn_pumpOff.pack()

read_data() // method that reads data from sensors

window.mainloop()

I tried to control it with interrupts but I'm stuck.

0

There are 0 best solutions below