Go Down

Topic: Arduino does not respond to Python codes (Read 274 times) previous topic - next topic

simeon9446

Mar 02, 2019, 04:15 pm Last Edit: Mar 02, 2019, 07:21 pm by simeon9446
Hello world.

So, this is my situation. TX and External LED responds to "Examples" -> "Blink" from Arduino IDE. I tried to play around with delay and it worked fine. But when I try to run similar code with Python it does not respond but stucks in the last code run by Arduino IDE. The code runs clean yet Arduino does not respond to commands; the LEDs keeps on blinking at set rate. Where might be the problem?



Code: [Select]
#Python Code for Arduino LED Control
import serial
import os
import time


arduino = serial.Serial("COM3", 9600)

def onOffFunction():
    command = input("Type in something (on/off/bye): ");
    if command == "on":
         print ("The LED is ON")
         time.sleep(1)
         arduino.write(b'H')
         onOffFunction()
    elif command == "off":
        print ("The LED is OFF")
        time.sleep(1)
        arduino.write(b'L')
        onOffFunction()
    elif command == "bye":
        print ("Bye Bye!")
        time.sleep(1)
        arduino.close()
    else:
        print ("Sorry.. Try typing something else.")
        onOffFunction()

time.sleep(2)

onOffFunction()

           

Here is how I connected everything.


Robin2

This simple Python - Arduino demo should help get you started.

See also Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up