How Many Fingers Am I Holding Up?

The aim of this project was to continue working on using the Leap Motion controller to control physical actions through an Arduino. The specific goal is to have a set of 5 LEDs display the number of fingers being detected by the Leap Motion controller. 

Here's a image of the Arduino setup,

HowManyFingersArduinoSetup.JPG

and a video of the project in action:

The Processing code:

// How Many Fingers Am I Holding Up?

import com.onformative.leap.LeapMotionP5;
import com.leapmotion.leap.Finger;
LeapMotionP5 leap;

import processing.serial.*;
Serial port;

public void setup() {
// set window, P3D = 3D rendering
size(600, 600, P3D);
noFill();
stroke(255);

// set LEAP object
leap = new LeapMotionP5(this);

// set com port. Currently: "/dev/tty.usbmodemfd121"
println("Available serial ports:");
println(Serial.list());
port = new Serial(this, "/dev/tty.usbmodemfd121", 9600);
}

public void draw() {
background(0);
fill(255);
int numFingers = leap.getFingerList().size();
print("Number of Fingers: ");
println(numFingers);
port.write(numFingers);
for (Finger f : leap.getFingerList()) {
PVector position = leap.getTip(f);
PVector velocity = leap.getVelocity(f);
ellipse(position.x, position.y, 10, 10);
}
}

public void stop() {
leap.stop();
}

And the Arduino code:

/* How Many Fingers Am I Holding Up?

LEDs attached to digital pin 3-7.
Serial Connection to Processing
*/

void setup(){
// initialize serial communication:
Serial.begin(9600);
// set LED pins 3-7 as outputs
for(int x = 3; x<8; x++){
pinMode(x, OUTPUT);
}
}

void loop(){
byte numFingers;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
numFingers = Serial.read();
if (numFingers == 0){
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
} else if (numFingers == 1){
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
} else if (numFingers == 2){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
} else if (numFingers == 3){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
} else if (numFingers == 4){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
} else if (numFingers == 5){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}
}
}

I used a not-so-elegant if/else decision structure because I was more concerned with the communication rather than the actual output structure. The trickiest part of this project was figuring out how to count the number of fingers. Because getFingerList(), which contains all of the fingers tracked by the Leap, is an ArrayList, I had to use .size() instead of .count().

Up next: controlling a servo motor with the Leap.