#include <Keyboard.h>

// Flex code
const int flexPin1 = A0;
const int flexPin2 = A1;
const int flexPin3 = A2;
const int flexPin4 = A3;
const int flexPin5 = A4;

const int threshold1 = 720;
const int threshold2 = 700;
const int threshold3 = 700;
const int threshold4 = 720;
const int threshold5 = 700;

//Boolean used to initiate glove typing
boolean flexread = false;

//Button Control
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int ledState = LOW;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
//End

void setup() {
  // Analogue Setup:
  pinMode(flexPin1, INPUT);
  pinMode(flexPin2, INPUT);
  pinMode(flexPin3, INPUT);
  pinMode(flexPin4, INPUT);
  pinMode(flexPin5, INPUT);

  //Digital setup
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
        flexread = !flexread;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;

//obtain flex values
  int flex1 = analogRead(flexPin1);
  int flex2 = analogRead(flexPin2);
  int flex3 = analogRead(flexPin3);
  int flex4 = analogRead(flexPin4);
  int flex5 = analogRead(flexPin5);

  if (flexread == true){
  //if thumb 
    while (flex1 > threshold1){
      //Send an ASCII '0', 
      delay(300);
      int flex1t = analogRead(flexPin1);
      if (flex1t < flex1);{
        Keyboard.write('0');
        break;
      }
}
      //if index 
    while (flex2 > threshold2){
       delay(300);
      int flex2t = analogRead(flexPin2);
      if (flex2t < flex2);{
        Keyboard.write('2');
        break;
      }
}
      //if middle
    while (flex3 > threshold3){
     delay(300);
      int flex3t = analogRead(flexPin3);
      if (flex3t < flex3);{
        Keyboard.write('3');
        break;
      }
}
      //if ring 
    while (flex4 > threshold4){
     delay(300);
      int flex4t = analogRead(flexPin4);
      if (flex4t < flex4);{
        Keyboard.write('4');
        break;
      }
}
      //if little
    while (flex5 > threshold5){
       delay(300);
      int flex5t = analogRead(flexPin5);
      if (flex5t < flex5);{
        Keyboard.write('5');
        break;
      }
}
}
    else {
    
    delay(20);
}

}