Go Down

Topic: Controling LED with Processing (Read 3413 times) previous topic - next topic

jdk199

     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


Code: [Select]

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


Code: [Select]
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);
  }
 
}

PaulS

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.
The art of getting good answers lies in asking good questions.

jdk199



PaulS

Code: [Select]
  }
    }
      }
        }

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

Code: [Select]
myPort.write("A" + arduinoValuea);
Addition?
The art of getting good answers lies in asking good questions.

jdk199

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.

PaulS

Quote
So if the value is 100 it would send A100 to the Arduino IDE.
Code: [Select]
myPort.write("A");
myPort.write(arduinoValuea);

Certainly would, and takes no longer to execute.

The art of getting good answers lies in asking good questions.

jdk199

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?





PaulS

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.
The art of getting good answers lies in asking good questions.

jdk199

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

jdk199

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

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

jdk199

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

jdk199

This is the code for arduino I used
Code: [Select]
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. 

PaulS

I used this code:
Code: [Select]
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.
The art of getting good answers lies in asking good questions.

Go Up