Python Arduino communication stops

Hi all,

I am trying to send an array of 400 rgb values to an arduino. I am waiting for my sd card slot I ordred on ebay, and in the meantime I’m just trying to get the arduino to display the data rather than storing it.

I am using this python script:

import time, os, serial

matrix=[
[236,164,0],[252,219,169],[0,105,146],[39,71,110],[0,29,74],
[236,164,0],[252,219,169],[0,105,146],[39,71,110],[0,29,74],
[236,164,56],[252,219,169],[0,105,146],[39,71,110],[0,29,74],
[236,164,0],[252,219,169],[0,105,146],[39,71,110],[0,29,74]
]

c=0
m_count=0

while 1:
	print 'Waiting for arduino...'
	try:
		ser = serial.Serial('/dev/cu.usbmodem1421', 9600) # Establish the connection on a specific port
		counter=0
		break
	except:
		time.sleep(1)

print "Press enter to light up the leds"
raw_input()

for n in matrix:

	ser.write("[")

	print ser.read()

	print str(c)+' - '+str(n)

	for m in n:
		ser.write(str(m))
		if m_count<2:
			ser.write(',')
		print ser.read()
		m_count+=1
		time.sleep(0)

	m_count=0

	ser.write("]")

	wait=0

	while ser.read()!='?':
		wait+=1
		print str(wait)+' - waiting for response'
		ser.read()

	time.sleep(0.05)

	c+=1

print 'Done'

raw_input()

Which communicates with this Arduino script:

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
boolean readarray=false;
String inString = "";
int c=0;
int cc=0;
int led=0;
int i=0;
int j=0;

int ledd[3]={0,0,0};

int matrix[400][3];

void setup() {
  Serial.begin(9600);
}

void loop() {

  if (stringComplete) {

    Serial.println(inString.toInt());
    
    inString = "";
    stringComplete = false;
  }

  if (readarray) {
    Serial.println("Ok I will read array");
    
    for (i = 0; i < 400; i++) {
    Serial.println("LED");
      for (j = 0; j < 3; j++) {
        //Serial.println(matrix[i][j]);
      }
    }
    readarray=false;
  }
}

void serialEvent() {
  while (Serial.available()) {
    
    char inChar = Serial.read();

    if (isDigit(inChar)) {
      inString += (char)inChar;
    }
    
    if (inChar == '[') {
      led=led+1;
      Serial.println("BEGIN OF LED");
      Serial.println(led);
      Serial.println("With rgb values:");
    }
    
    if (inChar == ',') {
      ledd[c]=inString.toInt();
      inString="";
      c=c+1;
    }

    if (inChar == ']') {

      ledd[c]=inString.toInt();
      
      for (j = 0; j < 3; j++) {
        Serial.println(ledd[j]);
      }

      inString="";
      Serial.println("END OF LED ");
      Serial.println(led);
      c=0;

      delay(5);
      Serial.write('?');
    }

    if (inChar=='a') {
      readarray=true;
    }
  }
}

And this works fine, until the 10th index of the python array, for some reason. It just stops and prints “waiting for response”, because it is expecting a ‘?’ from Arduino. But it’s not even looping, it just stops, and I don’t really understand why.

Is it a memory overload from Arduino? Serial overload? Or does the communication need some delay? I need the 400 values to be sent very quick as they need to light up a LED Matrix, and the delay has to be as short as possible.

Any help will be greatly appreciated,

Thanks in advance.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

This Python - Arduino demo may help.

...R

String inputString = "";         // a string to hold incoming data

No, it is NOT!