2

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.

Heres the CMD response:

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.

https://youtu.be/DjUkkpzF0so

2
  • Is it possible to see what is your Arduino code? – smajli Jun 17 '19 at 11:15
  • 2
    @smajli, Arduino code is PyFirmata – Juraj Jun 18 '19 at 5:11
0

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']):
4
  • Thank You. I will try to make these changes – SKAI Jun 17 '19 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 Jun 17 '19 at 12:10
  • Great to hear, you can accept my answer than, to let others notify the problem has been solved. – Michel Keijzers Jun 17 '19 at 12:11
  • 1
    if 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 True – Chad G Jun 17 '19 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.