I have posted this question before, and a user was good enough to answer it. I used his suggestions and found that the code was not working at all, after researching and tinkering with it, I came to an impasse. Here is what I want to do:
I want to control two arduinos using the serial connection between the arduinos and the Raspberry Pi and I am using Python to control them. I am using a GUI interface and tkinter library to create the buttons.
The original problem was that the information would only go through the Arduino when the user stopped pressing the button and I needed the information to be sent as soon as the button was pressed and continuously until the button was released. The member suggested using lambda.
Although did not work as proposed, I feel that he might have had the right idea, here is the code that I came up with after his suggestion.
import serial
running = True
ser = serial.Serial('/dev/ttyUSB0')
def send_data(self):
if self.char is not None:
ser.write(self.char)
#run again in 100ms. Here is where you control how fast
#to send the data. The first parameter to after is a number
#of milliseconds to wait befor calling the function
self.job=self.after(100,self.send_data)
class Application(Frame):
"""Defining the remote control buttons"""
def __init__(self,master):
self.char = []
self.job = []
self.send_data = []
"""Initialize the frame"""
Frame.__init__(self,master)
self.grid() # How many times has the user clicked the button
self.create_widgets()
def set_char(self,char):
self.char=char
def create_widgets(self):
"""Creates four buttons that move the servos"""
#Create the 'up' button
self.button1=Button(self,text="Up")
self.button1.bind("<ButtonPress-1>",lambda x:self.set_char('1'))
self.button1.bind("<ButtonRelease-1>",lambda x:self.set_char(None))
self.button1.grid()
send_data(self)
#Create the 'down' button
self.button2=Button(self,text="Down")
self.button2.bind("<ButtonPress>",lambda x:self.set_char('2'))
self.button2.bind("<ButtonRelease>",lambda x:self.set_char(None))
self.button2.grid()
#Create the 'left' button
self.button3=Button(self,text="Left")
self.button3.bind("<ButtonPress-3>",lambda x:self.set_char('3'))
self.button3.bind("<ButtonRelease-3>",lambda x:self.set_char(None))
self.button3.grid()
#creeate the 'right' button
self.button4=Button(self,text="Right")
self.button4.bind("<ButtonPress-4>",lambda x:self.set_char('4'))
self.button4.bind("<ButtonRelease-4>",lambda x:self.set_char(None))
self.button4.grid()
#Main
root = Tk()
root.title("Remote control")
root.geometry("250x250")
app = Application(root)
root.mainloop()
self.char = []but check forNone. And thatself.send_data = []but then you try to schedule it as a callbable method inself.job=self.after(100,self.send_data).