Sending to Arduino UNO two values from Python code

I'm starting to learn how to work with Arduino and communication between a Python code and Arduino. So I decided to start with, what I thought to be a easy task, sending from user input from Python 2 values, one delay between Arduino LED (port 13) and servo angle. It didn't work and I couldn't find any answers, hope you can help me.

Arduino code :

#include <Servo.h>

const int ledPin = 13; // pin the LED is attached to
int incomingByte = 1000; // variable stores serial data
int pos = 0;
int incoming[2];
Servo myservo;

void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(9);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
myservo.write(180);
delay(1000);
digitalWrite(ledPin, LOW);
myservo.write(0);
}

void loop()
{

if (Serial.available()>=2)
{
for (int i = 0; i < 1; i++)
{
incoming = Serial.read();

  • }*
  • digitalWrite(ledPin, HIGH);*
  • delay(incoming[0]);*
  • digitalWrite(ledPin, LOW);*
  • myservo.write(incoming[1]);*
  • }*
    Python code :
    import serial
    import time
    ser = serial.Serial('COM5', 9600, timeout=1)
    user_input = '1'
    while user_input != 'q':
  • user_input = input("'Blink time is or q = quit' : ")*
  • servo_input = input("What position servo go? ")*
  • byte_command = str.encode(user_input) *
  • ser.write(byte_command) *
  • servo_command = str.encode(servo_input)*
  • ser.write(servo_command)*
  • time.sleep(0.5) *
    print('\nq entered. Exiting the program')
    ser.close()
    Thanks

The serial input basics tutorial may help you understand how to receive serial data.

That code does not even compile. You have an error assigning the values you read from Serial. You also only read 1 value since your for() loop only iterates once

     for (int i = 0; i < 2; i++)
     {
      incoming[i] = Serial.read();
     }

Also note that when you open the COM port in your python script, you reset the Arduino so you have to wait a couple of seconds before you begin sending data or the Arduino may not receive it. It's better to have the Arduino respond first with something "I'm alive!" so your python script knows it is ready.

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

...R