assign python array elements in arduino array

Hi everyone,

I need to assign python array elements in arduino array, I sent the array elements to arduino but I could not solve the assignments, can anybody help me, here my codes;

Python parts;

Importing Libraries

import serial
import time
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=.1)

def write_read(x):
for a in x:
arduino.write(bytes(a, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
ser_bytes = data
decoded_bytes = ser_bytes[0:len(ser_bytes)].decode("utf-8")
print(decoded_bytes)

num = input("Enter a number: ")
numSplit = num.split(" ")
numLen = len(numSplit)
print(numSplit[1])
print(numLen)
write_read(numSplit)
arduino.close()

Arduino parts

String x;
int ang[5];
int y;
void setup() {
Serial.begin(9600);
Serial.setTimeout(1);
}
void loop() {
while (!Serial.available());

x = Serial.readString();
y = x.toInt();
for(int i=0; i<5; i++){
ang = y;
_ Serial.print(ang*);_
_
}_
_
// if I want to check each arduino elements it should be like this*_
* ang[0] = y;*
* Serial.print(ang[0]);*
}
thanks again...

Can you post an example of the message that Python sends?

If you are sending the data as text then have a look at this Simple Python - Arduino demo

If you are sending the data in binary format then you need to use the Python struct.pack() function and you should save the received bytes into a suitable array or struct on the Arduino.

When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to get the best out of the Forum

See How to get the best out of the Forum

...R

# Importing Libraries
import serial
import time
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=.1)

def write_read(x):
    for a in x:
        arduino.write(bytes(a, 'utf-8'))
        time.sleep(0.05)
        data = arduino.readline()
        ser_bytes = data
        decoded_bytes = ser_bytes[0:len(ser_bytes)].decode("utf-8")
        print(decoded_bytes)

num = input("Enter a number: ")
numSplit = num.split(" ")
numLen = len(numSplit)
print(numSplit[1])
print(numLen)
write_read(numSplit)
arduino.close()
String x;
int ang[5];
int y;
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(1);
}
void loop() {
  while (!Serial.available());
  
  x = Serial.readString();
  y = x.toInt();
  for(int i=0; i<5; i++){
    ang = y;
    Serial.print(ang);
  }
// if I want to check each arduino elements it should be like this
  ang[0] = y;
  Serial.print(ang[0]);
}

Thank you for posting the code in code tags.

However you did not answer my question about the message that the Python program sends.

...R

when I use python code I send to arduino such as "120 23 43 356", these numbers divided with blanks and first I split this text and then send arduino,

arduino send me back these separate numbers too but I want to assign these separated numbers in array elements in arduino, I have not found this solution yet,

I wish I explain what I need

Have a look at the parse example in Serial Input Basics. It uses a comma as the delimiter but that can easily be changed to a space. And as it's a demo it expects a string, an int and a float and that can be changed so it expects 4 ints. Then you can assign each int in turn to an element of an array with something like the following ...
in place of

integerFromPC = atoi(strtokIndx);

put

myArray[0] = atoi(strtokIndx);

...R

You can send arrays, numbers, floats, and more between Python and Arduino using the compatible libraries SerialTransfer.h and pySerialTransfer. SerialTransfer.h is installable via the Arduino IDE's Libraries Manager, pySerialTransfer is pip-installable and both come with examples (to include arrays/lists)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.