Back to the blog index.
Using arduino & fruit to control a 3D game using capacitance.
        Using an arduino circuit, I created the following video: (Scroll down for code.) 
The Unity project can be found here. A variable is incremented to represent the direction of the ball. It is then sent over serial.write to unity. The threshold represents the threshold capacitance.


// Import the library (must be located in the Arduino/libraries directory)
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor1 = CapacitiveSensor(3, 2);
CapacitiveSensor capSensor2 = CapacitiveSensor(5, 4);
CapacitiveSensor capSensor3 = CapacitiveSensor(7, 6);
CapacitiveSensor capSensor4 = CapacitiveSensor(9, 8);
CapacitiveSensor capSensorJump = CapacitiveSensor(11, 10);

// threshold for turning the lamp on
int threshold = 50;
int jumpThreshold = 50;

void setup() {
  Serial.begin(1200);
}

void loop() {

  long sensorValueNorth = capSensor3.capacitiveSensor(15);
  delay(2);
  long sensorValueEast = capSensor2.capacitiveSensor(15);
  delay(2);
  long sensorValueSouth = capSensor1.capacitiveSensor(15);
  delay(2);
  long sensorValueWest = capSensor4.capacitiveSensor(15);
  delay(2);
  long sensorValueJump = capSensorJump.capacitiveSensor(15);
  delay(2);

  //N = 1, E = 3, S = 10, W = 11
  //N = 1
  //NE = 4
  //E = 3
  //SE = 13
  //S = 10
  //SW = 21
  //W = 11
  //NW = 12

  int initVariable = 0;

  if (sensorValueNorth > threshold) {
    initVariable += 1;
    delay(1);
  }

  if (sensorValueEast > (threshold-0)) {
    initVariable += 3;
    delay(1);
  }

  if (sensorValueSouth > threshold) {
    initVariable += 10;
    delay(1);
  }

  if (sensorValueWest > (threshold)) {
    initVariable += 11;
    delay(1);
  }

  if (sensorValueJump > (jumpThreshold)) {
    Serial.write(2);
    delay(1);
  }

  Serial.write(initVariable);
  Serial.flush();
  delay(20);



  //  Serial.print(initVariable);
  //  Serial.print("\n");
  //  Serial.print("\n\n\nNorth: ");
  //  Serial.print(sensorValueNorth);
  //  Serial.print("\nEast: ");
  //  Serial.print(sensorValueEast);
  //  Serial.print("\nSouth: ");
  //  Serial.print(sensorValueSouth);
  //  Serial.print("\nWest: ");
  //  Serial.print(sensorValueWest);
  //  Serial.print("\n\nJump: ");
  //  Serial.print(sensorValueJump);
}