Restart sketch

edited April 2015 in Kinect

Hi there! I am very new at processing, but for an outcomming exhibition I would like to use processing and kinect. I have found some lines of code and changed them to meet my goals. But now I can't move forward and finish it! I would like to restart my sketch every 3 minutes (reseting everytime all lines drawn) without any keypressed or mouse event. Is this possible? Help me pleaseeeee! Here the code that I've made and changed so far:

PImage img;

import SimpleOpenNI.*;
SimpleOpenNI kinect;

boolean sketchFullScreen() {
  return true;
}
int closestValue;
int closestX;
int closestY;

float lastX;
float lastY;

//decalre global vriables for the 
//previous x and y co-ordinates
int previousX;
int previousY;

void setup()
{
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
  background(0);
  size(displayWidth, displayHeight);
  img = loadImage("bolaXL640.jpg");
  image(img, 350, 150);

}

void draw()
{

  closestValue = 4000;

  kinect.update();

  int[] depthValues = kinect.depthMap();
   translate (350, 150);

  for(int y = 0; y < 480; y++){
    for(int x = 0; x < 640; x++){

      // reverse x by moving in from the right side of image
      int reversedX = 640-x-1;

      // use reversedx to calculate
      //the array index
      int i = reversedX + y * 640;
      int currentDepthValue = depthValues[i];

      //only look for the closest values within a range
      //620 or 2 feet is the minimum
      //1525 or 5 feet is the maximum
      if(currentDepthValue > 610 && currentDepthValue < 1525
      && currentDepthValue < closestValue){

        closestValue = currentDepthValue;
        closestX = x;
        closestY = y;
      }
    }
  }

  //linear interpolation i.e smooth transition between last point and new 
  // closest point
  float interpolatedX = lerp(lastX, closestX, 0.2f);
  float interpolatedY = lerp(lastY, closestY, 0.2f);

  //line colour
  stroke(0);
  strokeWeight(1);

  //draw line from previous point to new one
  line(lastX, lastY, interpolatedX, interpolatedY);
  lastX = interpolatedX;
  lastY = interpolatedY;

  }

Answers

  • Answer ✓

    here---

    // forum.processing.org/two/discussion/1725/millis-and-timer
    
    final int WAIT_TIME = (int) (3.0 * 1000); // 3.0 seconds in millis
    int startTime;
    
    
    void setup() {
      size(200, 200); 
      startTime=millis();
      background(0);
    }
    
    
    void draw() {
      stroke(random(255), random(255), random(255));
      line(random(width), random(height), 
      random(width), random(height));
      if (hasFinished()) {
        println(WAIT_TIME/1000 
          + " seconds have past! At "
          + millis()
          + " millis.");
        startTime = millis();
        background(0);
      }
    }
    
    boolean hasFinished() {
      return millis() - startTime >= WAIT_TIME;
    }
    
    //
    
  • Thanks! it works just fine!! But, sorry my ignorance, now I have another problem...when the background goes to black how can I put my background image where it was on setup (center) ? I'm trying like this but it doesn't load.

    //...

    if (hasFinished()) { println(WAIT_TIME/1000 + " seconds have past! At " + millis() + " millis."); startTime = millis(); background(0); loadImage("bolaXL640.jpg"); startTime=millis(); }

    I have tried to copy the values of setup for my image -- image(img, 350, 150); -- but it simply moves to the right bottom instead of being centered, as it was. humpf! Don't know how to fix it!

    //...

    if (hasFinished()) { println(WAIT_TIME/1000 + " seconds have past! At " + millis() + " millis."); startTime = millis(); background(0); loadImage("bolaXL640.jpg"); image(img, 350, 150); startTime=millis(); }

  • edited April 2015 Answer ✓

    first imgBkg = loadImage("bolaXL640.jpg"); must always be in setup() never in draw()

    second just use background(imgBkg);

    ;-)

  • Hello again :) I've tried as you said...but it isn't running :( I have got this message "Could not run the sketch (Target VM failed to initialize)." Here's the full code...can you tell me what i did wrong? Thanks! you're being an huge help!!! :)

    final int WAIT_TIME = (int) (120.0 * 1000); // 120.0 seconds in millis

    int startTime;

    PImage imgBkg;

    import SimpleOpenNI.*;

    SimpleOpenNI kinect;

    boolean sketchFullScreen() { return true; }

    int closestValue;

    int closestX;

    int closestY;

    float lastX;

    float lastY;

    int previousX;

    int previousY;

    void setup() { kinect = new SimpleOpenNI(this);

    kinect.enableDepth();

    background(0);

    size(displayWidth, displayHeight);

    imgBkg = loadImage("bolaXL640.jpg");

    image(imgBkg, 350, 150);

    startTime=millis();

    }

    void draw() { closestValue = 4000;

    kinect.update();

    int[] depthValues = kinect.depthMap();

    translate (350, 150);

    for(int y = 0; y < 480; y++){

    for(int x = 0; x < 640; x++){

      // reverse x by moving in from the right side of image
      int reversedX = 640-x-1;
    
      // use reversedx to calculate
      //the array index
      int i = reversedX + y * 640;
      int currentDepthValue = depthValues[i];
    
      //only look for the closest values within a range
      //620 or 2 feet is the minimum
      //1525 or 5 feet is the maximum
    
      if(currentDepthValue > 610 && currentDepthValue < 1525
      && currentDepthValue < closestValue){
    
        closestValue = currentDepthValue;
        closestX = x;
        closestY = y;
      }
    }
    

    }

    //linear interpolation i.e smooth transition between last point and new // closest point

    float interpolatedX = lerp(lastX, closestX, 0.2f);

    float interpolatedY = lerp(lastY, closestY, 0.2f);

    //line colour

    stroke(0);

    strokeWeight(1);

    //draw line from previous point to new one

    line(lastX, lastY, interpolatedX, interpolatedY);

    lastX = interpolatedX;

    lastY = interpolatedY;

    if (hasFinished()) {

    println(WAIT_TIME/1000
      + " seconds have past! At "
      + millis()
      + " millis.");
    
    startTime = millis();
    

    background(imgBkg);

    startTime=millis();

    }

    }

    boolean hasFinished() {

    return millis() - startTime >= WAIT_TIME; }

  • Answer ✓
    1. How to format code and text (why is it correct in the first message?)
    2. Don't forget the golden rule: make the call to size() the first one in setup().
    3. I can't run your code (no Kinect), so I hope item 2 can be useful...
  • Sorry about the messy code... I am very new at this...the first one was probably corrected by the admin :) sorry! About the golden rule, not sure I understand...(as I said...very new at this and very poorly programming skills)

    Should I do it like this? It's not possible to run the sketch either "Could not run the sketch (Target VM failed to initialize)" What am i missing?

    `

    final int WAIT_TIME = (int) (120.0 * 1000); 
    // 120.0 seconds in millis  
    
    int startTime;  
    
    PImage imgBkg;  
    
    import SimpleOpenNI.*;
    SimpleOpenNI kinect;
    
    boolean sketchFullScreen() {
    return true;
    }
    int closestValue;
    int closestX;
    int closestY;
    
    float lastX;
    float lastY;
    
    //decalre global vriables for the 
    //previous x and y co-ordinates
    int previousX;
    int previousY;
    
    void setup()
    {
    kinect = new SimpleOpenNI(this);
    kinect.enableDepth();
    background(0);
    size(displayWidth, displayHeight);
    imgBkg = loadImage("bolaXL640.jpg");
    image(imgBkg, 350, 150);
    startTime=millis();
    }
    
    void draw() {
    closestValue = 4000;
    kinect.update();
    int[] depthValues = kinect.depthMap();
    translate (350, 150);
    
    for(int y = 0; y < 480; y++){
    for(int x = 0; x < 640; x++){
    
    // reverse x by moving in from the right side of image
    int reversedX = 640-x-1;
    
    // use reversedx to calculate
    //the array index
    int i = reversedX + y * 640;
    int currentDepthValue = depthValues[i];
    
    //only look for the closest values within a range
    //620 or 2 feet is the minimum
    //1525 or 5 feet is the maximum
    if(currentDepthValue > 610 && currentDepthValue < 1525
    && currentDepthValue < closestValue){
    
    closestValue = currentDepthValue;
      closestX = x;
      closestY = y;
      }
     }
    }
    
    //linear interpolation i.e smooth transition between last point and new 
    // closest point
    
    float interpolatedX = lerp(lastX, closestX, 0.2f);
    float interpolatedY = lerp(lastY, closestY, 0.2f);
    
    //line colour
    stroke(0);
    strokeWeight(1);
    
    //draw line from previous point to new one
    line(lastX, lastY, interpolatedX, interpolatedY);
    lastX = interpolatedX;
    lastY = interpolatedY;
    
    if (hasFinished()) {
    println(WAIT_TIME/1000
      + " seconds have past! At "
      + millis()
      + " millis.");
    startTime = millis();
    
    background(imgBkg);
    image(imgBkg, 350, 150);
    startTime=millis();
    }
    
    }
    
    boolean hasFinished() {
    return millis() - startTime >= WAIT_TIME;
    }
    

    `

  • Answer ✓

    the beginning of setup() should look like this

    void setup()
    {
    size(displayWidth, displayHeight);
    

    so move the line size(displayWidth, displayHeight); up.

    size() must occur only once.

  • Hello once more! :)

    I moved size(displayWidth, displayHeight); up to setup only... but no improvements...it still breaks down after 120 seconds and the sketch doesn´t run. The background(imgBkg); gets underlined and this message appears :

    120 seconds have past! At 122864 millis. Could not run the sketch (Target VM failed to initialize).

    Why? Is there another crucial error in the code? Until there the sketch runs fine!

  • edited May 2015 Answer ✓

    I can't help you

    I don't know the lib you use.

    Remarks

    lines 30 / 33 / 89 / 90 should be background(imgBkg);

    this could be before setup() :

    int[] depthValues;

  • edited May 2015 Answer ✓
    • do other processing sketches run at all - the simple examples?

    • do other kinect sketches with that lib run?

    Find out whether it's because of the current sketch or a deeper issue please.

    ;-)

  • Hi there again! Thanks! Think I got it! I have resized the data image imBkg to size (640,360) it works just fine and resets after 120 seconds. But I can´t do a fullscreen with boolean sketchFullScreen(). Any thoughts in what to use? Thanks!

  • edited May 2015 Answer ✓

    size(displayWidth, displayHeight);

    ?

  • All done and working!!!!thanks a lot!!!

  • you're welcome!

    ;-)

Sign In or Register to comment.