How to create a square without noLoop(); ?

edited February 2014 in How To...

**Hello, **

I am working on my last year project for my highschool, and I am creating an invisible entry system that I control with my fingers.

(http://forum.processing.org/two/uploads/imageupload/481/QU4DV0AOJDBA.png "lmulz")

The black squares represent where your fingers have to go to open the gate, and the red ellipse represents my finger.

I am using a Leap Motion, Processing, and Arduino, but here's my problem :

I want someone to be able to create it's own digital code by creating a zone represented by a square, but I don't know how to do it. For the moment I use a noLoop(); after the creation of the square, but it stops my program ofc.

So, I am able to create a square that doesn't move according to my finger anymore, bu that completely my program.. ** Here is my code : **

import processing.serial.*;
import de.voidplus.leapmotion.*;
import development.*;
Serial portUSB;

int inByte;
int x,y;
int p,q;
int a;
LeapMotion leap; // creation de l'objet leap
ArrayList<PVector> points; //tableau pour contennir le tracé
ArrayList<PVector> points2;
PVector fp; // vector de la position des doigts

void setup()
{
  size(800, 800, P3D);
  background(125,125,125);
  println(Serial.list());
  portUSB = new Serial(this, Serial.list()[1], 9600);
  leap = new LeapMotion(this);
  points = new ArrayList<PVector>();  // Create an empty ArrayList
  points2 = new ArrayList<PVector>();
  smooth(8);
  noStroke();
  a = 1;
  x = 150;
  y = 150;
  p = 450;
  q = 450;
}

void draw()
{
  rect(x,y,150,150);
  rect(p,q,150,150);
  fill(0);

  int fps = leap.getFrameRate();
  frameRate(fps);
  // Mains
  for (Hand hand : leap.getHands())
  {
    // Doigts
    for (Finger finger : hand.getFingers())
    {
      fp   = finger.getPosition(); // retourne la position des doigts sous forme de vecteur
      nettoyer();
      mdr();
    }
  }
}


void nettoyer()
{
  background(125,125,125);
}

void mdr()
{

  rect(x,y,150,150);
  rect(p,q,150,150);
  fill(0);



  if (fp.z > 40 )
  {  
    fill(255,0,0);
    ellipse(fp.x, fp.y, constrain(fp.z, 10, 20), constrain(fp.z, 10, 20));

  }

if (fp.x > x && fp.x < x+150 && fp.y > y && fp.y < y+150 )
{
  if(a == 1)
  {
  points.add(new PVector(fp.x, fp.y));
  println("LES SAUCISSES");
  a++;
  }
} 


if (fp.x > p && fp.x < p+150 && fp.y > q && fp.y < q+150 )
{
  if(a == 2)
  {
  points.add(new PVector(fp.x, fp.y));
  portUSB.write(67);
  println(67);
  a++;
  }
} 

    if(fp.z > 65)
   { 
    rect(fp.x-75,fp.y-75,150,150);
    fill(0,255,0);
    points2.add(new PVector(fp.x,  fp.y));  
   }


for (int i = points.size()-1; i >= 0; i--) 
  {
    PVector p = points.get(i);
    fill(255, 0, 0);
    ellipse(p.x, p.y, 20, 20);
  }

for (int u = points2.size()-1; u >= 0; u--) 
  {
    PVector p = points2.get(u);
    fill(0, 255, 0);
    rect(fp.x-75,fp.y-75,150,150);
**    noLoop();**

  }

}

void keyPressed()
{
  if(key == '1')
    {
      println("RESET");
      portUSB.write(68);
      a = 1;
      points = new ArrayList<PVector>();
      points2 = new ArrayList<PVector>();
      nettoyer();
    }
  }

Thanks for your help, and sorry for my english. Ask me if you didn't understand something.

Answers

  • To format the source code highlight it so the code is selected and press Ctrl + k or click on the C button.

    I have deleted your second post which was the same code as the first post.

  • Ok, thanks !

  • edited February 2014

    as far as I understand it, your program has two situations:

    • create it's own digital code by creating a zone represented by a square

    • use the invisible entry system that I control with my fingers.

    At the moment you try to have noLoop after situation 1.

    Instead think of states of the program.

    • state / situation 1 being: create it's own digital code

    • state / situation 2 being: use the invisible entry system

    so you need a var "state" which can be 1 or 2

    int state = 1;
    

    in draw() have a switch that goes

    switch (state){
    case 1:
    // handle situation one
    break;
    case 2:
    // handle situation two
    break;
    default:
    // error 
    println ("Unknow state, error code 298");
    break;
    }
    

    the rects e.g.

      rect(x,y,150,150);
      rect(p,q,150,150);
      fill(0);
    

    are only (not) drawn in state 2

    Later

    in case he later unlocked the door or failed to do so you can expand the states to

    • state you won

    • state you failed

    Greetings, Chrisir

  • Thanks a lot! I will try to use this solution !

  • I tried your solution and it seems to match. The only problem is that I don't know how to switch from case1 to case2. And when I try to put the case 2 in a if() I get an error...

    Here is my code now :

    import processing.serial.*;
    import de.voidplus.leapmotion.*;
    import development.*;
    Serial portUSB;
    
    int inByte;
    int x,y;
    int p,q;
    int a;
    int state = 1;
    LeapMotion leap; // creation de l'objet leap
    ArrayList<PVector> points; //tableau pour contennir le tracé
    PVector fp; // vector de la position des doigts
    void setup()
    {
      size(800, 800, P3D);
      background(125,125,125);
      println(Serial.list());
      portUSB = new Serial(this, Serial.list()[0], 9600);
      leap = new LeapMotion(this);
      points = new ArrayList<PVector>();  // Create an empty ArrayList
      smooth(8);
      noStroke();
      a = 1;
      x = 150;
      y = 150;
      p = 450;
      q = 450;
    }
    
    void draw()
    {
    
     for (int i = points.size()-1; i >= 0; i--) 
      {
        PVector p = points.get(i);
        fill(255, 0, 0);
        ellipse(p.x, p.y, 20, 20);
      }
    
     //-----------------------------------------------------------------------------------------------//
    
      int fps = leap.getFrameRate();
      frameRate(fps);
      // Mains
      for (Hand hand : leap.getHands())
      {
        // Doigts
        for (Finger finger : hand.getFingers())
        {
          fp   = finger.getPosition(); // retourne la position des doigts sous forme de vecteur
          nettoyer();
          mdr();
        }
      }
    }
    
    //-----------------------------------------------------------------------------------------------//
    
    void nettoyer()
    {
      background(125,125,125);
    }
    
    //-----------------------------------------------------------------------------------------------//
    
    
    void mdr()
    {
    
        if (fp.z > 40 )
      {  
        fill(255,0,0);
        ellipse(fp.x, fp.y, constrain(fp.z, 10, 20), constrain(fp.z, 10, 20));
    
      }
    
      switch (state)
      {
    
        case 1:
    // handle situation one
      rect(x,y,150,150);
      rect(p,q,150,150);
      fill(0);
    
      if (fp.x > x && fp.x < x+150 && fp.y > y && fp.y < y+150 )
    {
      if(a == 1)
      {
      points.add(new PVector(fp.x, fp.y));
      println("LES SAUCISSES");
      a++;
      }
    } 
    
    if (fp.x > p && fp.x < p+150 && fp.y > q && fp.y < q+150 )
    {
      if(a == 2)
      {
      points.add(new PVector(fp.x, fp.y));
      portUSB.write(67);
      println(67);
      a++;
      }
    } 
    
    break;
    
      case 2:
      // handle situation two
      rect(x,y,150,150);
      rect(p,q,150,150);
      fill(0);
    
          if(fp.z > 65)
       { 
        rect(fp.x-75,fp.y-75,150,150);  
       }
    break;
    
    
    default:
    // error
    println ("Unknow state, error code 298");
    break;
    
    }
    
    
    }
    
    void keyPressed()
    {
      if(key == '1')
      {
        println("RESET");
        portUSB.write(68);
        a = 1;
        nettoyer();
      }
    
    
    }
    
  • edited February 2014

    I don't get what mdr stands for

    also I don't understand why you have switch in mdr and not in draw()

    I expected in draw():

        void draw() {
         switch (state)
          {
    
            case 1:
        // handle situation one
        // someone creates his own digital code by creating a zone represented by a square
        // he did it: state=2; !!!!!!!!!!!!!
        // he failed: state stays 1 
        break;
    
        case 2: 
        // check whether your fingers have gone now to open the gate
        break;
    
       default:
       // error
       println ("Unknow state, error code 298");
       break;
    
        } // switch
    
        } // func 
    

    Greetings, Chrisir

  • Nevermind, I understood ! Ty !

  • Here's what I get now :

    import processing.serial.*;
    import de.voidplus.leapmotion.*;
    import development.*;
    Serial portUSB;
    
    int inByte;
    int x,y;
    int p,q;
    int a;
    int state = 1;
    LeapMotion leap; // creation de l'objet leap
    ArrayList<PVector> points; //tableau pour contennir le tracé
    PVector fp; // vector de la position des doigts
    void setup()
    {
      size(800, 800, P3D);
      background(125,125,125);
      println(Serial.list());
      portUSB = new Serial(this, Serial.list()[0], 9600);
      leap = new LeapMotion(this);
      points = new ArrayList<PVector>();  // Create an empty ArrayList
      smooth(8);
      noStroke();
      a = 1;
      x = 150;
      y = 150;
      p = 450;
      q = 450;
    }
    
    void draw()
    {
    
     for (int i = points.size()-1; i >= 0; i--) 
      {
        PVector p = points.get(i);
        fill(255, 0, 0);
        ellipse(p.x, p.y, 20, 20);
      }
    
     //-----------------------------------------------------------------------------------------------//
    
      int fps = leap.getFrameRate();
      frameRate(fps);
      // Mains
      for (Hand hand : leap.getHands())
      {
        // Doigts
        for (Finger finger : hand.getFingers())
        {
          fp   = finger.getPosition(); // retourne la position des doigts sous forme de vecteur
          nettoyer();
          mdr();
        }
      }
    }
    
    //-----------------------------------------------------------------------------------------------//
    
    void nettoyer()
    {
      background(125,125,125);
    }
    
    //-----------------------------------------------------------------------------------------------//
    
    
    void mdr()
    {
    
        if (fp.z > 40 )
      {  
        fill(255,0,0);
        ellipse(fp.x, fp.y, constrain(fp.z, 10, 20), constrain(fp.z, 10, 20));
    
      }
    
      switch (state)
      {
    
        case 1:
    // handle situation one
      rect(x,y,150,150);
      rect(p,q,150,150);
      fill(0);
    
      if (fp.x > x && fp.x < x+150 && fp.y > y && fp.y < y+150 )
    {
      if(a == 1)
      {
      points.add(new PVector(fp.x, fp.y));
      println("LES SAUCISSES");
      a++;
      }
    } 
    
    if (fp.x > p && fp.x < p+150 && fp.y > q && fp.y < q+150 )
    {
      if(a == 2)
      {
      points.add(new PVector(fp.x, fp.y));
      portUSB.write(67);
      println(67);
      a++;
      }
    } 
    
    break;
    
      case 2:
      // handle situation two
    
      println("PUTAIN OMG");
          if(fp.z > 65)
       { 
        rect(fp.x-75,fp.y-75,150,150);  
       }
    break;
    
    
    default:
    // error
    println ("Unknow state, error code 298");
    break;
    
    }
    
    
    }
    
    void keyPressed()
    {
      if(key == '1')
      {
        println("RESET");
        portUSB.write(68);
        a = 1;
        nettoyer();
      }
    
    if (key == '9')
       {
    state = 2;
    }
    }
    
  • ??

    does it work?

    switch state still in mdr, not in draw

    what does mdr do?

  • you can end state 1 and go to state 2 when 9 is hit or when return / enter is hit e.g.

  • Well, I am sorry but I've just realized that I worked two differents versions, so some elements are missing in 2 last versions of my code.

    I am working on it.

  • Well, yes it works. When I press 9, my state 2 is active. That's the only way I found for the moment, but this is the idea.

  • Sorry forgot to answer :

    mdr stands for pretty much everything in fact..

  • I don't speak french so I can't understand some of your code

    I'm sorry

    In situation 1 : how many rects does he have to submit until he's done?

    just count them and when he's finished say state = 2;

  • edited February 2014

    the big picture:

    a more logical approach would be:

    normally we all land in state 2

    when he points in a certain way (three corners e.g.), he tells the system he is a admin

    system asks for password

    when password is right, state 1, he can change the lock, and save it

    password and lock position is saved encrypted on hard-drive

    because

    because at the moment it's only a game without much purpose

    with a use like above there would be a more substantial way to tackle the user scenario I think

  • For the moment, the gate is opened when you first go in the top-left square and then in the second, so there's no specific number.

    The thing is that I am trying to use the square I create in the second caser, transfering it in the first case etc...

  • The problem I am facing is that I don't really know how to transfer the square from the state 2 to the state 1.

  • I can't run your code because I don't have serial etc.

    but in theory, you paint two rects as a hidden lock in situation 2

      rect(x,y,150,150);
      rect(p,q,150,150);
    

    is that right?

    so in situation 1 when he defines the locks you need to

    store his input in inputPosX1, inputPosY1 etc.

    and when he hits 9 (goes to state 2) you say once

    x = inputPosX1;
    y = inputPosY1;
    state = 2; 
    

    etc.

    so you can use x and y and p and q in state 2

  • edited February 2014

    this is the idea....

    NB

    • x,y and p,q here are upper left corner and lower right corner of the lock

    • there is only one lock.

    state 0: Admin starts by defining it (user must look away) and then state 1: user must unlock

    when he did state = 2 - he won

    the states are used in draw() and in mouseClicked() also - very elegant

    //
    // THE LOCK
    // that is upper left corner and lower right corner!!!!!!!!!!!!!!!
    int x=-100, y=-100;
    int p=-100, q=-100;
    //
    int state = 0; 
    int countMouseClicksInStateDefineLock=0;
    //
    // ---------------------------------------------------------------
    //
    void setup()
    {
      // init
      size(800, 600);
    } // func 
    //
    //
    void draw() 
    { 
      background(255);
      switch (state) {
      case 0:
        defineLock();
        break; 
      case 1:
        tryUnlock();
        break;
      case 2:
        // he won 
        textSize(33);
        fill(255, 1, 1);
        text("YOU  WON", 200, 200);
        fill(255, 2, 2);
        ellipse(x, y, 4, 4);
        if (x>-50) {
          noFill();
          stroke(255, 1, 1);
          rect(x, y, p-x, q-y);
        }
        break;
      } // switch
    } // func 
    //
    
    // --------------------------------------------------------------
    
    void defineLock() {
      fill(255, 2, 2);
      text("Admin, define lock", 20, 20);
      ellipse(x, y, 4, 4);
      if (x>-50) {
        noFill();
        stroke(255, 1, 1);
        rect(x, y, mouseX-x, mouseY-y);
      }
    }
    
    void tryUnlock() {
      fill(0);
      text("User, try unlock", 20, 20);
    }
    
    // --------------------------------------------------------------
    
    void mouseClicked() {
      switch (state) {
      case 0:
        // defineLock();
        mouseClickedInStateDefineLock();
        break; 
      case 1:
        // tryUnlock
        if (mouseX>x&&mouseY>y&&mouseX<p&&mouseY<q) {
          state = 2;
        }
        else 
        {
          println ("wrong");
          fill(255, 1, 1);
          text ("wrong", 200, 20);
        }
        break;
      case 2:
        //
        break;
      } // switch
    } 
    
    void mouseClickedInStateDefineLock() {
      // how many times did he click? 
      switch (countMouseClicksInStateDefineLock) {
      case 0:
        // 
        x=mouseX;
        y=mouseY;
        break; 
      case 1:
        // 
        p=mouseX;
        q=mouseY;
        state = 1; 
        break;
      }
      countMouseClicksInStateDefineLock++;
    }
    
    // =====================================================================
    
  • I don't get what you're trying to do in your second messsage, with the mouse game.

    For the moment I am using keyboard, but the goal is to control everything with my fingers only, but I will work on this later.

  • it is a simulation to show how I see the connection of state 0 and state 1

    you wrote

    The problem I am facing is that I don't really know how to transfer the square from the state 2 to the state 1.

    I wanted to give you an example on how to do that

    you wrote

    I don't get what you're trying to do in your second messsage, with the mouse game.

    in the state 0 an admin is defining a rect and in the state 1 an user is searching the lock and trying to open it

    the mouse is the replacement for what you do with the fingers

  • Oh okay! I'll read it again then. Thanks.

  • or rather load it and play with it in processing

  • I tried but the problem is that I can't write something like float w; w = fp.x (finger position axis x) so basically I don't know how to use this..

    When I try to do w = fp.x it says that the com port 1 is busy..

  • so I can't say to my program that w is fp.x in case 2 and fp.x just itself in the first case, so the program confuses both fp.x and fails miserably.

  • I dunno

    Maka a new post maybe?

  • Maybe I will.

    Sorry, I didn't answer because I was in holidays, I didn't work much...

    Thanks for all your help though. :)

Sign In or Register to comment.