Controling LED with Processing

I'm doing this project for school where I'm using a Kinect, Processing, and an Arduino. Currently I'm having trouble sending the data from Processing to the Arduino IDE. I'm trying to control just one LED right now but eventually I will be controlling 12 LED's. When I try to run the program the LED turns on but the brightness variable I'm trying to manipulate seems to be set at 255 no matter what value I try to send. It seems that there has to be something wrong with how i send the value because when I put the value I'm trying to send in the Serial Monitor, everything works out just fine.

Processing Code

import processing.serial.*;

import SimpleOpenNI.*;
SimpleOpenNI kinect;



Serial myPort;
int closestValue1a;//closest depthvalue in section a
int closestValue2a;//2nd closest depthvalue in section a
int counter1a;//counter 1 of section a
int closestValue3a;//3rd closest depthvalue in section a
int realdeptha;//depth value we use for arduino of section a 
int counter2a;//counter 2 of section a
int arduinoValuea;


void setup()
{
  
  //size of picture and emables kinect
  size(640, 480);
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
   myPort = new Serial(this, "COM4", 9600);
   

}

void draw()
{
  //update kinect
  kinect.update();
  
  //display depth map
  PImage depthImage = kinect.depthImage();
  image(kinect.depthImage(), 0, 0);
  
  //default values
  closestValue1a = 8000;
  closestValue2a = 8000;
  closestValue3a = 8000;
  counter1a = 0;
  counter2a = 0;

  
  int[] depthValues = kinect.depthMap();

//serching for higest value in section 1  
  for(int y = 0; y < 160; y++) 
  {
    for(int x = 0; x < 160; x++)
    {
      //figures out the pixel value
      int i = x+(y*640);
      int currentDepthValue1 = depthValues[i];
      
      //if closest so far
      if(currentDepthValue1 > 0 && currentDepthValue1 < closestValue1a){
        
        //makes the current top value #3. 
        //This can be done over and over to make as many ranks as needed
        closestValue3a = closestValue2a;
        
        //makes the current top value #2
        closestValue2a = closestValue1a;
        
        //save the new value as #1
        closestValue1a = currentDepthValue1;
            
      }
    } 
  }
   //checks the box for close values
   for(int y = 0; y < 160; y++)
   {
     for(int x = 0; x < 160; x++)
     {
        //figures out the pixel value
      int i = x+(y*640);
      int currentDepthValue1 = depthValues[i];
      
      if(currentDepthValue1 > 0 && currentDepthValue1 < (closestValue1a + 200)){
             //counts everytime there's a close value
              counter1a++; 
          }
        }   
      }

//sees how many close values there are
if(counter1a > 750){
  //if enough pixels are close to the closest value declares the closest value realdeptha
  realdeptha = closestValue1a;
}
//if not enough this section sees how many values are close to the 3rd closest
else{for(int y = 0; y < 160; y++)
   {
     for(int x = 0; x < 160; x++)
     {
      int i = x+(y*640);
      int currentDepthValue1 = depthValues[i];
      if(currentDepthValue1 > 0 && currentDepthValue1 < (closestValue3a + 200)){
        counter2a++; 
  }
    }
      }
        }
//looks at how many pixels are close to the 3rd closest value
if(counter2a > 750){
  /*if enough are close(750) declares the 
  *realdeptha variable as the 3rd closest variable*/
  realdeptha = closestValue3a;
} 
//if nothing else works sets realdeptha variable as the average of all the pixel depth values
else{ 
               
    //calculate sum of all array elements
    int sum = 0;
   for(int y = 0; y < 160; y++)
   {
     for(int x = 0; x < 160; x++)
     {
        int i = x+(y*640);
      int currentDepthValue1 = depthValues[i];{
    sum = sum + depthValues[i];}}}
    //calculate average value
    int average = Math.round(sum / 25600);
    //declares it as realdeptha
    realdeptha = average;
}


// convert depth data to data for arduino
if(realdeptha > 0 && realdeptha < 6000){
arduinoValuea = Math.round(realdeptha*-0.0254545454545455+152.727272727273);
}
else{arduinoValuea = 0;}
println("A" + arduinoValuea);

//send data to arduin
myPort.write("A" + arduinoValuea);
delay(500);

}

Arduino Code

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
digitalWrite(13, LOW);

}

void loop() {
  // Assumes a string in from the serial port like so:
  // A brightness
  // for example: "A200"

  
  while(Serial.find("A")) {
   
    int brightness = Serial.parseInt();// parses numeric characters after the comma
    

    // set the LED:
    analogWrite(13, brightness);
    delay(500);
  }
  
}

Which Arduino are you using? On the UNO and other 328 based Arduinos, pin 13 is not a PWM pin. You can only vary the brightness of LEDs using PWM pins.

I'm using a MEGA 2650

sorry MEGA 2560

  }
    }
      }
        }

That looks pretty stupid. Can't you be bothered to indent properly? Or to use Tools + Auto Format?

myPort.write("A" + arduinoValuea);

Addition?

Sorry about the indents I will try to fix that.

With the other section I was trying to attach the "A" to the value to act as an identifier since I plan to send more values in the future. So if the value is 100 it would send A100 to the Arduino IDE.

So if the value is 100 it would send A100 to the Arduino IDE.

myPort.write("A");
myPort.write(arduinoValuea);

Certainly would, and takes no longer to execute.

Thank you for the reply. I added what you suggested but it did not fix the problem. The LED still turns on but does not change it's brightness based upon the value I send; it just stays at 255. Are there any other areas you might see where I made an error that would affect sending the value?

I'd suggest echoing the value that the Arduino receives. Perhaps it isn't what you think it is. Have Processing read and print(ln)() the data from the serial port.

Thanks, I'll give it a try and post the results a little later.

When I echo the value back to Processing the result is -1. I'm not sure what that means.

When I echo the value back to Processing the result is -1. I'm not sure what that means.

Generally, it means that there was nothing to read, but you read anyway. Shitcan the stupid find() method. Simply determine if there is data to read (using Serial.available()). If there is, read the next byte. If it is 'A', then you can call Serial.parseInt() to get the value.

OK I will give that a go, again thank you very much for your help.

This is the code for arduino I used

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);


}

void loop() {



  if (Serial.available()) {
    byte byteRead = Serial.read();
    if (byteRead == 'A') {
      int brightness = Serial.parseInt();// parses numeric characters after the comma
      // set the LED:
      analogWrite(13, brightness);
      delay(500);
    }
  }
}

When in pin 13 the same thing happened; the led light up but the brightness never changed. I then changed around the program and put the led in pin 12 and nothing happened. While the program was still running I pulled the positive lead of the led and put it in pin 13 and lit up even though pin 13 was never mentioned in the program.

I used this code:

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);
}

void loop()
{
  if (Serial.available()) 
  {
    byte byteRead = Serial.read();
    if (byteRead == 'A') 
    {
      int brightness = Serial.parseInt();
      
      Serial.print("Brightness: ");
      Serial.println(brightness);
      
      // set the LED:
      analogWrite(13, brightness);
      delay(500);
    }
  }
}

On my Mega, sending A255, A100, and A40 results in noticeable differences in the intensity of the external LED (even though I am using too high a resistor value).

So, you are not expecting the onboard LED to change brightness are you? You do have an external LED attached, with resistor? Without the resistor, the will not be a lot of change in brightness, because the LED will suck more current than the Arduino can provide.

When I put the values into the serial monitor everything works, it's when I try to send the values with the processing program that I have difficulties.

OK. Try this:

Arduino code:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

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

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    if(inData[0] == 'A')
    {
      inData[0] = ' ';
      int brightness = atoi(inData);
      Serial.print("Brightness: ");
      Serial.println(brightness);
      analogWrite(13, brightness);
    }

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Processing code:

import processing.serial.*;

Serial myPort;            // the serial port

void setup()
{
  // list all the available serial ports:
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 115200);
  myPort.bufferUntil('\n');
  size(510, 510);    // sets the size of the applet window
}

void draw()
{
//  print("mouseX: ");
//  println(mouseX);
//  print("width: ");
//  println(width);
  int x = (mouseX * 255) / width;
//  print("x = ");
//  println(x);
  
  String toSend = "<A" + x + ">";
//  print("toSend = ");
//  println(toSend);

  myPort.write(toSend);
}


void serialEvent(Serial myPort)
{
  boolean validString = true;  // whether the string you got is valid
  String errorReason = "";     // a string that will tell what went wrong

  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');

  // make sure you have a valid string:

  if (myString != null)
  {
    // trim off the whitespace (linefeed, carriage return) characters:
    myString = trim(myString);
  }
  println(myString);
}

The Processing code displays a box. Move the mouse to the right in the window, and the LED gets brighter. Move it to the left and the LED gets dimmer.

Note that parseInt() is nowhere to be seen in the Arduino code.

I tried what you put in and it didn't work for some reason. The same thing that has been happening happened again, the light lit up and wouldn't change when I moved the mouse around. I then changed everything to pin 12 and tried it, but it didn't even light up. Like before, I moved the positive led into pin 13 and the led lit up even though pin 13 was not mentioned in the program. I'm not sure why it's doing this, would you recommend trying to this with the Firmata library?

hi, i use the following code on Arduino to vary a RGB led,

Arduino Code

int RED = 10;
int BLUE = 9;
int GREEN = 11;
int i=0;
int colorData=0;
int greenData=0;
int prevGreenData=255;
int j=0;

void setup()
{
  // declare the serial comm at 57600 baud rate
  Serial.begin(57600);

  // output pins
  pinMode(GREEN, OUTPUT); // green
  pinMode(RED, OUTPUT); // red
  pinMode(BLUE, OUTPUT); // blue


  digitalWrite(RED, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(GREEN, HIGH);
}

void loop()
{
  // call the returned value from GetFromSerial() function
  switch(GetFromSerial())
  {
  case 'R':
    colorData = GetFromSerial();

    analogWrite(RED, colorData);

    break;
  case 'G':
    greenData=  GetFromSerial();

    analogWrite(GREEN,greenData);

    break;
  case 'B':
    analogWrite(BLUE, GetFromSerial());
    break;
  }

}

// read the serial port
int GetFromSerial()
{
  while (Serial.available()<=0) {
  }
  return Serial.read();
}

and this is what i send from Processing on serial
Mine is a common Annode RGB, you may probably send the actual color value.

void SendData(int r, int b, int g) 
{
  myPort.write("R");
  myPort.write(255-r);
  myPort.write("G");
  myPort.write(255-g); 
  myPort.write("B");
  myPort.write(255-b);
}

would you recommend trying to this with the Firmata library?

Not in a million years.

The code I posted DID make my LED change brightness as I moved the mouse back an forth. Obviously, I tested it on my Mega before I posted it.

One question. Which version of Processing are you using? I don't know of anyone that has been happy with the 2.0 version of Processing. I'm using 1.5.1.