Combine Processing+Arduino+Leapmotion

edited December 2016 in Arduino

Hello, I'm combining leapmotion, processing and arduino, I use the leapmotion for processing library, and the way I do is if leapmotion detected hand, send '1' to serial port which is for arduino, and if there's no hand, send'0'. After receive 1, arduino turn on the relay, receive 0 turn off. but I failed and I can't figure out which part is the mistake, here is my code :

Processing :

        import de.voidplus.leapmotion.*;
        import processing.serial.*;

        Serial myPort;

        LeapMotion leap ; 

        void setup () {
          size(800, 500);
          background(255);
          leap = new LeapMotion(this);
          String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
          myPort = new Serial(this, portName, 9600);
        }
        void draw() {
          int number=0;
          for (Hand hand : leap.getHands() ) {
            number=hand.countFingers();
          }
          println(number);

          if (number!= 0) 
          {                           
            myPort.write('1');         //send a 1
            println("1");
          } else 
          {                           //otherwise
            myPort.write('0');          //send a 0
          }


        }

Arduino :

    char val; // Data received from the serial port
    int ledPin = 13; // Set the pin to digital I/O 13

    void setup() {
      pinMode(ledPin, OUTPUT); 
      Serial.begin(9600); 
    }
    void loop() {
      if (Serial.available())
      { // If data is available to read,
        val = Serial.read(); 
        }

      if (val == '1')
      { // If 1 was received
        digitalWrite(ledPin, HIGH); 
      } else {
        digitalWrite(ledPin, LOW); 
      }
      delay(10); 
    }

Answers

  • Answer ✓

    Hard to debug this without having your hardware-setup. If i removed the leap-related code and replaced your draw-function with this:

    void draw() {
      if (keyPressed) {                           
        myPort.write('1');        
        println("1");
      } else {                           
        myPort.write('0');         
        println(0);
      }
    }
    

    Pressing any key would now make the build-in arduino led light up. Your problem must be related to the leap or it's how you wired up your relay.

    Try adding more debug-statements, are hands/fingers recognized? Can you switch your relay on/off with arduino's blink-example? Does my version of the code work using keys instead of leap? If all the different parts work, then i have no idea.

  • I figure out that is the serial port problem, thank you for answering me :)

Sign In or Register to comment.