Sending data from arduino to processing

I want to send dmx data (channel and channel value) from arduino to processing. So I want to store DMX values from channels 0-512 in processin to array value, but I don't know how can I receive data from arduino.
Below is my arduino code, which is emulating dmx shield input.

void setup() {
  Serial.begin(9600);
}
int channel;
int value;
void loop() {
  for(channel = 0; channel < 10; channel++) {
    Serial.println("c");
    Serial.println(channel);
    Serial.println("ec");
    Serial.println("v");
    Serial.println(random(255));
    Serial.println("ev");
  }
}

Thanks for help!
Elias

eliasojala1234:
I want to send dmx data (channel and channel value) from arduino to processing.

Ok, you have our permission.

Is there a question here? If you're having trouble, you're going to actually describe the problem you're having.

There is a question.

So I want to store DMX values from channels 0-512 in processing to array value, but I don't know how can I receive data from arduino.

How can I receive channel value data from arduino?

eliasojala1234:
There is a question.

Not a question, but I'm not going to quibble over semantics.

How can I receive channel value data from arduino?

Create a protocol. Typically this involves a start packet marker, end packet marker, and delimiters. On the receiving end, read the data, and parse the values out. You can change "ec" and "ev" to single letters and use a state machine.

Yes I tried many codes and spent tens of hours thinking, but nothing works. So could you write processing code for me.

here you are.

This sketch process serial input:

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

void loop()
{
  if (Serial.available())
  {
    char myChar = Serial.read();
    Serial.print(myChar);
  }
}

Thank you! Do you mean I have to send data also from processing to arduino?

eliasojala1234:
Thank you! Do you mean I have to send data also from processing to arduino?

yes, attach whatever is sending serial data to your arduino to the TX/RX pins (pins one and zero respectively) and you will see in your serial monitor what that device is sending.

you will then need to decode what is sent. That is the more challenging part.

Yes I know that. Below is code which I tried to use in processing, but it says NullPointerExpection, so something went wrong.

int[] values;

// Example by Tom Igoe

import processing.serial.*;

Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports:
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    String inBuffer = myPort.readString();   
    if (inBuffer != null) {
      String[] channelArray = match(inBuffer, "c(.*?)ec");
      String[] valueArray = match(inBuffer, "v(.*?)ev");
      if (channelArray[1] != null && valueArray[1] != null) {
        if (channelArray.length == valueArray.length) {
          for (int i = 1; i < channelArray.length; i++) {
            values[i] = int(valueArray[i]);
          }
        }
      }
    }
  }
}

Below is code which I tried to use in processing, but it says NullPointerExpection, so something went wrong.

Yes, indeed. You have several arrays that you presume contain data, but that may not. That doesn't stop you from trying to reference undefined elements of the array.

Quit trying to write the whole program at once. First, do NOTHING more than read the serial data and print it. When you KNOW that you are receiving reasonable data, THEN you can try to parse it. Print the resulting arrays BEFORE you try to access the nth element of an array. Make SURE that there are n elements in the array BEFORE you try to access the nth element.

Edit: You should be sending ONE carriage return. You should be using Serial::bufferUntil() to prevent the serialEvent callback from being called before all the data has arrived. You should NOT be reading serial data in draw(). You SHOULD be using serialEvent().