Send long array to arduino over serial from processing

user3399652 picture user3399652 · Mar 9, 2014 · Viewed 12.3k times · Source

So i build an 24x16 (hight of 16, length of 24) LED matrix and am using an Arduino Uno to control it. It is only a one color matrix and i am using an array to store all of the bits of data.

This is an example of how i am storing the data on the arduino:

long frame [16] = {11184810, 5592405, 11184810, 5592405, 11184810, 5592405, 11184810, 5592405, 11184810, 5592405, 11184810, 5592405, 11184810, 5592405, 11184810, 5592405};

The way that i store this data is by rows. ex: long frame [16] = {row 1 bits, row 2 bits, ... and so on}

If you convert 11184810 to binary; you would get 101010101010101010101010 which represents the data for one row of LED's.

One limitation that i came across on when using the arduino was the limited space on it to store theses arrays so i wanted to find a way to send it through serial.

I wanted to ask if i could get some help writing some code for Processing and for the arduino so that i could send this array data from Processing to the array on the arduino live over serial. I have never used Processing and don't know how to use it, but read that is is a good way of sending data to the arduino and also having a GUI to enter in the data.

Answer

Galarzaa90 picture Galarzaa90 · Apr 9, 2014

The easiest way I can think of is sending the array with the values separated by commas.

GUI

First of all in my personal experience, making a GUI for user input in Processing is difficult, because you don't have predefined classes (as far as I know) to make text fields, check boxes, etcetera, so you would have to draw rectangles and program mouse and key events to change focus between fields. However, you could create a JFrame and Text Fields and everything, and still use Processing's serial communication, you would end up with a window running the sketch and an external window with your GUI. Here's some information about how to create frames and other elements. Remember that Processing is actually running on Java (sorry if that's not the correct term).

But if you are going to do that, you might as well switch to "pure" Java

Sending data over serial

Processing

First you will need to build the array with your GUI, I have two ideas for that, either 16 fields, one for each row, where you write a number and it converts it to binary, or, making a group of checkboxes, 24x16, and each one represents a LED, so you tick the LEDs you want to turn on.

To send the data from Processing/Java to Arduino, here's an example code:

for(int i = 0; i < 16; i++){
  if(i > 0){
    str += ",";//We add a comma before each value, except the first value
  }
  str += frame[i];//We concatenate each number in the string.
  }
  serial.write(str);
}

Where frame[] is the data you want to send to Arduino.

You have to import a special library to use serial communication. And create a class object (not sure if I'm using the correct term).

import processing.serial.*
void setup(){
    /* Some setup code here */

    /* Opening first port, 9600 baud rate */
    Serial serial = new Serial(this,Serial.list()[0],9600)
}

Arduino

Then, on Arduino, inside loop() you use something like this:

String content = "";
if (Serial.available()) {
  while (Serial.available()) {
    content += Serial.read();
  }
}
long data[16]; //The results will be stored here
for(int i = 0; i < 16; i++){
  int index = content.indexOf(","); //We find the next comma
  data[i] = atol(content.substring(0,index).c_str()); //Extract the number
  content = content.substring(index+1); //Remove the number from the string
}

Now data[] has the data you sent from Processing.