i am a beginner at python and I am average at Arduino and I had the idea of using both of them together for a project. I have been trying to make a Arduino + Python car which I can control with my computer. I have used Pyfirmata and the code seems perfect to me. When I run the code from CMD i have a weird bug/issue. I have uploaded Standard Firmata to the arduino UNO. I get no errors while uploading or running its just that its output is messed up.
Here's the Python Code:
from pyfirmata import Arduino, util
import time
board = Arduino("COM3")
MotorLeft = board.get_pin('d:10:o') # Assigning output to PIN
MotorRight = board.get_pin('d:11:o') # Assisging output to PIN
play = True
while play:
control = input(
print("Enter W, A or D to move forwards, left or right. Enter 0 to exit"))
if control != 0:
if control == "W" or "w":
MotorRight.write(1)
MotorLeft.write(1)
time.sleep(1)
MotorRight.write(0)
MotorLeft.write(0)
if control == "A" or "a":
MotorLeft.write(1)
time.sleep(1)
MotorLeft.write(0)
if control == "D" or "d":
MotorRight.write(1)
time.sleep(1)
MotorRight.write(0)
else:
print("Invalid Commmand")
else:
print("Exited Succesfully")
play = True
Currently I have not built the car, I am using a pair of LEDs to do what the motors function and to test out the code.
Here is the link for the output video. I entered "W" which should ideally turn on both of the LEDs however, both of them turn on then off and then turn on off one by one.
-
Is it possible to see what is your Arduino code?smajli– smajli2019年06月17日 11:15:05 +00:00Commented Jun 17, 2019 at 11:15
-
2@smajli, Arduino code is PyFirmataJuraj– Juraj ♦2019年06月18日 05:11:05 +00:00Commented Jun 18, 2019 at 5:11
1 Answer 1
I cannot check it, but I guess you have to change the or commands like:
if control == "W" or "w":
Change to
if ((control == 'W') or (control == 'w'))
And similar for the other if statements.
Also, you can do not need to check for control == 0, since it will not be a character otherwise.
Besides that, the elif
(else if) construct is normally used in Python, so instead of:
if control != 0:
if control == "W" or "w":
MotorRight.write(1)
MotorLeft.write(1)
time.sleep(1)
MotorRight.write(0)
MotorLeft.write(0)
if control == "A" or "a":
MotorLeft.write(1)
time.sleep(1)
MotorLeft.write(0)
if control == "D" or "d":
MotorRight.write(1)
time.sleep(1)
MotorRight.write(0)
else:
print("Invalid Commmand")
You can write:
if ((control == 'W') or (control == 'w')):
MotorRight.write(1)
MotorLeft.write(1)
time.sleep(1)
MotorRight.write(0)
MotorLeft.write(0)
elif ((control == 'A') or (control == 'a')):
MotorLeft.write(1)
time.sleep(1)
MotorLeft.write(0)
elif ((control == 'D') or (control == 'd')):
MotorRight.write(1)
time.sleep(1)
MotorRight.write(0)
else:
print("Invalid Commmand")
Also, you can combine the or
in a list (but I cannot check it, no python compiler at hand), e.g.
if (control in ['W', 'w']):
-
Thank You. I will try to make these changesSKAI– SKAI2019年06月17日 12:03:11 +00:00Commented Jun 17, 2019 at 12:03
-
Thank You! This project works now! Do you know what causes this problem? so I can avoid this problem in the future.SKAI– SKAI2019年06月17日 12:10:35 +00:00Commented Jun 17, 2019 at 12:10
-
Great to hear, you can accept my answer than, to let others notify the problem has been solved.Michel Keijzers– Michel Keijzers2019年06月17日 12:11:40 +00:00Commented Jun 17, 2019 at 12:11
-
2if control=='W' or 'w' since 'w' is not equal to 0, this will always be true, its the same as writing if control=='W' or TrueChad G– Chad G2019年06月17日 22:54:36 +00:00Commented Jun 17, 2019 at 22:54