Please help me with the code for kinect and arduino mega

Hi. I'm a Korean student who wants to learn more about processing and arduino and I am so new to this area. My team is trying to learn about processing and arduino.

What I'm trying to do is to display people's movements on LED 32*32 which are seen through kinect.
This LED has to be used with RGBMatrixPanel library and I'm new to it.
MY computer is Windows 7.
I have processing 2.2 and simpleopenNI 1.96
I'm using Arduino Mega also.

To talk about anything I will start with my code first.

I'm really sorry that our code is so messy.

This is for processing

import processing.serial.*;
import processing.opengl.*;
import SimpleOpenNI.*;

SimpleOpenNI kinect;
PImage kSM = new PImage(32,32,ARGB);
PImage kSmall = new PImage(640,480,ARGB);

byte g[] = new byte[1024];

int userMap[];

int numUsers = 0;
int arduinoPort = -1;


Serial myPort;

void setup() {
  size(901, 800, P2D);
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
  kinect.setMirror(true);
  kinect.enableUser();
  
  /*String[] ports = Serial.list();
  for (int i=0;i<ports.length;i++) {
    println(ports[i]);
    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////
    // THIS IS SPECIFIC TO THE PORT YOUR ARDUINO IS ON!!!
    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////
    if (ports[i].indexOf("411") > -1 && ports[i].indexOf("tty") > -1) {
      arduinoPort = i;
    }
  }
  if (arduinoPort > -1) {
    myPort = new Serial(this, Serial.list()[arduinoPort], 57600);
  }
  */
  //String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
  //myPort = new Serial(this, portName, 57600);
  myPort = new Serial(this, "COM4", 57600);

  background(128,0,0);

  // give some time to wake up arduino and kinect
  delay(3000);
}

void draw() {
    background(0);

  kinect.update();
  image(kinect.depthImage(), 20, 120);

  PVector[] depthPoints = kinect.depthMapRealWorld();

  for (int y=0;y<32;y++) {
    for (int x=0;x<32;x++) {
      PVector currentPoint = depthPoints[(x*floor(640/32)) + ((y * floor(480/32)) * kinect.depthImage().width)];
      if (currentPoint.z < 200 || currentPoint.z > 2000) {
        currentPoint.z = 0;
      }
      noStroke();
      fill(map(currentPoint.z, 0, 4000, 0, 255));
      ellipse(680+x, 120+y, 2, 2);
    }
  }

  numUsers = kinect.getNumberOfUsers();
  if (numUsers > 0) { 
    numUsers = 1;
    // find out which pixels have users in them
    //userMap = kinect.getUsersPixels(SimpleOpenNI.USERS_ALL); 
    //userMap = kinect.getUsers();
   userMap = kinect.userMap(); 
    // populate the pixels array
    // from the sketch's current contents 
    kSmall.loadPixels();
    for (int i = 0; i < userMap.length; i++) { 
      // if the current pixel is on a user
      if (userMap[i] != 0) {
        // make it green
        kSmall.pixels[i] = color(0, 255, 0,255);
      } else {
        kSmall.pixels[i] = color(0,0);
      }
    } 
    // display the changed pixel array 
    kSmall.updatePixels(); 
  }
  
  kSM.copy(kSmall,80,0,480,480,0,0,32,32);
  image(kSM,680,120,64,64);
  
  fill(255,0,0);
  text("users: " + numUsers,10,10);
   int as=10;
    int ad=20;
  for (int i=0;i<1024;i++) {
   //ad = ad +10;
    g[i] = kSM.pixels[i] == 0 ? (byte)0 : (byte)1;
    text(i+" pixel:" + g[i], as,ad);
    //if( i == 980 ) {
     // as = as+50;
     // text(i+" pixel:" + g[i], as,10);
    //}
}
  
  // mask depth image to show what matrix will get
  noStroke();
  fill(0);
  rect(20,120,80,480);
  rect(580,120,80,480);
  
  // send the data to arduino
  //drawGrid();
  //if (arduinoPort > -1) {
    sendToArduino();
  //}
  numUsers = 0;
  delay(50);
}


void sendToArduino() {
  // send data to arduino
  
  // header
  int b1 = 0x81;
  int b2 = 0xc3;
  myPort.write(b1);
  myPort.write(b2);
  // number of users
  myPort.write((byte)numUsers);
  myPort.write((byte)1024);
  // image bytes
  for (int offset=0;offset<32;offset++) {
    byte rb = 0x00;
    byte mask = 0x01;
    for (int i=0;i<32;i++) {
      if (g[i+(offset*32)] == 1) {
        rb = (byte)(rb | mask);
        
      }
      mask = (byte)(mask << 1);
    }
    myPort.write(rb);
  }
}

Processing code seem to work at least.

Next is arduino IDE

#include <Adafruit_GFX.h>
#include <RGBmatrixPanel.h>
#include <gamma.h>
#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif
#define CLK 11  // MUST be on PORTB! (Use pin 11 on Mega)
#define LAT 9
#define OE  10
#define A   A3
#define B   A2
#define C   A1
#define D   A0

const byte HEADER1       = 0x81;
const byte HEADER2       = 0xc3;
const int  TOTAL_BYTES   = 32 ; // 2 headers + 2 numusers + 32 imagedata
byte inBytes[32];
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 32);

void setup() {
  
  for (int i=0;i<32;i++) {
    inBytes[i] = (byte)0;
  }
  //pinMode(11, OUTPUT);
  Serial.begin(57600);
  //Serial.setTimeout(150);
  matrix.begin();
  
 /*// start with RGB tests (set all pixels to same color to look for bad LEDs)
  matrix.fillScreen(matrix.Color333(255,0,0));
 
  delay(500);
  matrix.fillScreen(matrix.Color333(0,255,0));
 
  delay(500);
  matrix.fillScreen(matrix.Color333(0,0,255));
 
  delay(500);
  matrix.fillScreen(0);
  delay(500);
  
  // Show white corners just to make sure we're addressing LEDs by number correctly (0-15)
  matrix.drawPixel(0,0,matrix.Color333(255,255,255));
  matrix.drawPixel(31,0,matrix.Color333(255,255,255));
  matrix.drawPixel(0,31,matrix.Color333(255,255,255));
  matrix.drawPixel(31,31,matrix.Color333(255,255,255));
 
  
  delay(500);
*/
}
void loop() {
  // wait for all bytes
  if ( Serial.available()>= TOTAL_BYTES)
  {
    
    Serial.print("input data ok");
    Serial.write(Serial.read());
    if( Serial.read() == HEADER1)
    {
      if (Serial.read() == HEADER2) {
        // next byte is number of users
        // for now we only care if it's not 0 (zero)
        char inByte = Serial.read();
        int numUsers = inByte - '0'; // convert byte to number
        Serial.read(); // 255 byte
        
        // read rest of packet
        for (int j=0;j<32;j++) {
          inBytes[j] = Serial.read();
        }
        
        if (numUsers != 0) {
          drawUser();
         
        } else {
          // we have 0 users
          // clear the screen
          matrix.fillScreen(0);
        
        }
        
      }
    }
  } else {
    // no serial data...
     matrix.drawPixel(0,0,matrix.Color333(255,255,255));
  matrix.drawPixel(31,0,matrix.Color333(255,255,255));
  matrix.drawPixel(0,31,matrix.Color333(255,255,255));
  matrix.drawPixel(31,31,matrix.Color333(255,255,255));
// This code is to light up every corner on LED if there's no serial data
  } 
}
void drawUser() {
  // clear matrix
  matrix.fillScreen(0);
  // iterate over byte array 
  // for each byte, check each bit and and turn pixels on where the bit is 1
  int x = 0;
  int y = 0;
  for (int j=0;j<32;j++) {
    byte b = inBytes[j];
    for (int i=0;i<8;i++) {
      if (bitRead(b,i) == 1) {
        matrix.drawPixel(x,y,matrix.Color333(255,255,255));
    
      } else {
        matrix.drawPixel(x,y,0);
 
      }
      x++;
      if (x > 31) {
        y++;
        x = 0;
      }
    }
  }
}

Sorry that it's a real mess.

Alright. Here is what I want to say. Arduino Mega seems to receive the values as you can see in the picture #1.
But when you see LED on picture #2, it seems it's not receiving any serial data because I made the code to light up every corner on LED if there are no serial data.

This is ironic for me. Can anybody help me please.

Picture #3 is a picture on PC monitor when I ran processing

Please edit your post and change the quote tags to code tags

AWOL:
Please edit your post and change the quote tags to code tags

Sorry about the tags. I changed it to code tags! :slight_smile:

This is ironic for me

Is this because you have no idea what the word irony means?

Those pictures are rather useless they explain nothing.

Why are you trying to get such complex code working with no testing of the much simpler concepts first?

Is your Arduino controlling the matrix correctly? Then is Processing talking to the Arduino? I would try a simple Processing sketch that just sends one of two bytes that lights up one led or another. Establish the hardware is working and your communications are sound before trying anything clever.