Recording Rapid Serial Port in Arduino from Processing

I need help to speed up writing to the serial number. I found some similar questions for this, but nothing about the processing language or Java, so I hope someone can help me with this question that I have.

change

As John points out below, it seems that the serial number is simply not fast enough to send as much data as fast as I want. Does anyone know of the other arduino interfaces available?

end edit

I use arduino to control a grid of 400 RGB LEDs that I connected. To send commands to arduino, I wrote a small program in Processing, which manages a large array that represents LEDs. Then I try to update the grid by sending 800 bytes of data to the arduino every 20 ms at 115200 bauds in serial. Processing code that is called every 20 ms:

noStroke(); int dataPos = 0; // position in LED data array byte[] dataLedGrid = new byte[400*2]; // array for bytes to send for(int j=0; j<LEDS_TALL; j++) { for(int i=0; i<LEDS_WIDE; i++) { int pos = j*20+i; int r = ledGrid[LEDS_WIDE-i-1][LEDS_TALL-j-1][0], g = ledGrid[LEDS_WIDE-i-1][LEDS_TALL-j-1][1] ,b = ledGrid[LEDS_WIDE-i-1][LEDS_TALL-j-1][2]; int colorData = ((g & 0x1F) << 10) | ((b & 0x1F) << 5) | (r & 0x1F); dataLedGrid[dataPos] = byte(colorData & 0x00FF); dataLedGrid[dataPos+1] = byte(colorData & 0xFF00); dataPos+=2; // draw LED squares on gui fill(ledGrid[i][j][0], ledGrid[i][j][1], ledGrid[i][j][2]); rect(SIDE_PANEL_WIDTH+(LED_SQUARE_SIDE+LED_SQUARE_SPACING)*i+HORIZONTAL_MARGIN, (LED_SQUARE_SIDE+LED_SQUARE_SPACING)*j+VERTICAL_MARGIN, LED_SQUARE_SIDE, LED_SQUARE_SIDE); } } myPort.write(dataLedGrid); // write to serial 

On arduino, I have a 1D array (Display) that represents the grid on the arduino side. Loop code:

 void loop() { unsigned int pos, c1, c2; if (Serial.available() > 0) { for(byte j=0; j<20; ++j) { for(byte i=0; i<20; ++i) { c1 = Serial.read(); c2 = Serial.read(); pos = i+20*j; if(j % 2 != 0) // it a square of leds created by a zigzaging line pos = 20*(j+1)-i-1; // so I have to reverse every other line Display[pos] = (unsigned int)(c1<<8 | c2); } } show(); } } 

Now the code itself works fine, but when serial writing slows everything down. When I run the processing code without sequential writing, everything works fine at a given speed. However, when I add serial input, everything becomes a little volatile. The processor does not maximize or nothing, so I assume this is the serial.write method that I call. What can I do to speed up this code or remove the backlog from serial input?

Thank you for your help!

+3
java c embedded serial-port processing
source share
2 answers

Do the math.

115200 baud, at 8-N-1, 11.520 bytes per second or 86.8 Ξs / byte.

After 20 ms, you can send 230.4 bytes. Sending 800 bytes will take about 70 ms.

Attempting to send 800 bytes at 115200 bauds every 20 ms will not work.

+8
source share

Try adding Serial.setTimeout(0) to setup() your arduino sketch

0
source share

All Articles