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? Commented Jun 17, 2019 at 11:15
  • 2
    @smajli, Arduino code is PyFirmata Commented Jun 18, 2019 at 5:11

1 Answer 1

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 Commented 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. Commented Jun 17, 2019 at 12:10
  • Great to hear, you can accept my answer than, to let others notify the problem has been solved. Commented Jun 17, 2019 at 12:11
  • 2
    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 Commented Jun 17, 2019 at 22:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.