141 lines of code

We started to program all the components one by one. We had one piece of code for the bend sensors, one piece for the tilt sensor and a one for the keyboard switch. Now we’ve tried to put those in to one piece of code, resulting in 141 lines. This code is a first version, not yet fully tested. We’re expecting to change it a lot before the exhibition day.

int thumbPin = 1;	// Analog pin for thumb bend sensor.
int indexPin = 2;	// Analog pin for index finger bend sensor.
int longPin  = 3;	// Analog pin for long finger bend sensor.
int ringPin  = 4;	// Analog pin for ring finger bend sensor.
int pinkyPin = 5;	// Analog pin for pinky finger bend sensor.

int switchPin1 = 8;	// Digital pin for switch E0.
int switchPin2 = 9;	// Digital pin for switch E1.
int switchPin3 = 10;	// Digital pin for switch E2.
int switchPin4 = 11;	// Digital pin for switch E3.

int tiltPin = 7;	// Digital pin for tilt sensor.

int state      = 0;	// Variable for reading state of tilt sensor.
int oldState   = 0;	// Variable for remembering last state of tilt sensor.
int lastChange = 0;	// Variable for time when state of tilt sensor last changed.
int lastNudge  = 0;	// Variable for time when last nudge was sent.
int diffChange = 0;	// Variable for how long ago state of tilt sensor changed.
int diffNudge  = 0;	// Variable for how long ago last nudge was sent.

int thumbVal = 0;	// Variable for analog value of thumb bend sensor.
int indexVal = 0;	// Variable for analog value of index finger bend sensor.
int longVal  = 0;	// Variable for analog value of long finger bend sensor.
int ringVal  = 0;	// Variable for analog value of ring finger bend sensor.
int pinkyVal = 0;	// Variable for analog value of pinky finger bend sensor.

boolean thumbB = false;		// Boolean telling if thumb is bent.
boolean indexB = false;		// Boolean telling if index finger is bent.
boolean longB  = false;		// Boolean telling if long finger is bent.
boolean ringB  = false;		// Boolean telling if ring finger is bent.
boolean pinkyB = false;		// Boolean telling if pinky finger is bent.

int gesture = 0;	// Variable for telling what gesture is active (which fingers are bent?).

boolean needReset = false;	// Boolean telling if fingers need to be reset to
				// be able to send new gestures.
void setup() {			// SETUP
  pinMode(thumbPin, INPUT);	// Set thumbPin as a INPUT pin.
  pinMode(indexPin, INPUT);	// Set indexPin as a INPUT pin.
  pinMode(longPin, INPUT);	// Set longPin as a INPUT pin.
  pinMode(ringPin, INPUT);	// Set ringPin as a INPUT pin.
  pinMode(pinkyPin, INPUT);	// Set pinkyPin as a INPUT pin.

  pinMode(switchPin1, OUTPUT);	// Set switchPin1 as a OUTPUT pin.
  pinMode(switchPin2, OUTPUT);	// Set switchPin2 as a OUTPUT pin.
  pinMode(switchPin3, OUTPUT);	// Set switchPin3 as a OUTPUT pin.
  pinMode(switchPin4, OUTPUT);	// Set switchPin4 as a OUTPUT pin.

  pinMode(tiltPin, INPUT);	// Set tiltPin as a INPUT pin.

  Serial.begin(9600);
}

void loop(){				// LOOP
  if(!needReset){			// If there's no need for a reset of fingers.
    resetValues();			// Calls resetValues()-method.
    gesture = getGesture();		// Calls getGesture()-method and saves the value.

    if(gesture == 1){			// If gesture equals thumbs up.
      digitalWrite(switchPin1, HIGH);	// Send CTRL key stroke.
      digitalWrite(switchPin2, HIGH);	// Send § key stroke.
    } else if(gesture == 2){		// If gesture equals f you.
      digitalWrite(switchPin1, HIGH);	// Send CTRL key stroke.
      digitalWrite(switchPin3, HIGH);	// Send . key stroke.
    } else if(gesture == 3){		// If gesture equals shake.
      digitalWrite(switchPin1, HIGH);	// Send CTRL key stroke.
      digitalWrite(switchPin4, HIGH);	// Send ' key stroke.
    }

    Serial.print(gesture);
  } else{
    while(needReset){                        // As long as there's a need for a reset of fingers.
      if(!thumbB && !indexB && !longB && !ringB && !pinky){// If no fingers are bent.
        needReset = false;                   // There's no need for a reset of fingers anymore.
      }
    }
  }
}

void resetValues(){			// RESET VALUES
  digitalWrite(switchPin1, LOW);	// Sets all the switchPins to LOW to keep the switches open.
  digitalWrite(switchPin2, LOW);	// When set to HIGH a switch closes and completes the
  digitalWrite(switchPin3, LOW);	// circuit for sending a keystroke to the computer.
  digitalWrite(switchPin4, LOW);

  thumbVal = analogRead(thumbPin);	// Reads analog value of thumb bend sensor.
  indexVal = analogRead(indexPin);	// Reads analog value of index finger bend sensor.
  longVal  = analogRead(longPin);	// Reads analog value of long finger bend sensor.
  ringVal  = analogRead(ringVal);	// Reads analog value of ring finger bend sensor.
  pinkyVal = analogRead(pinkyVal);	// Reads analog value of pinky finger bend sensor.

  state = digitalRead(tiltPin);		// Reads state of tilt sensor

  thumbB = false;        	// Boolean telling if thumb is bent.
  indexB = false;               // Boolean telling if index finger is bent.
  longB  = false;               // Boolean telling if long finger is bent.
  ringB  = false;               // Boolean telling if ring finger is bent.
  pinkyB = false;               // Boolean telling if pinky finger is bent.
}

int getGesture(){               // GET GESTURE
  int temp = 0;                 // Temporary variable
				// The following values are not the one's we're going to use.
  if(thumbVal < 500){           // If thumb bend sensor value is less than 500.
    thumbB = true;              // Thumb is bent.
  }
  if(indexVal < 500){           // If index finger bend sensor value is less than 500.
    indexB = true;              // Index finger is bent.
  }
  if(longVal < 500){            // If long finger bend sensor value is less than 500.
    longB = true;               // Long finger is bent.
  }
  if(ringVal < 500){            // If ring finger bend sensor value is less than 500.
    ringB = true;               // Ring finger is bent.
  }
  if(pinkyVal  2000 && diffChange  0 && temp < 4){   // If temp is 1, 2 or 3
    needReset = true;                                // Fingers need to be reset before next gesture.
  }

  oldState = state;		// Saves current state of tilt sensor as old state.

  return temp;			// Returns the gesture value back to the loop.
}
Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: