/*
  musical t-shirt
  with smoothing
  and 3 flex sensors
*/
#include "Keyboard.h"

int pushButton = 2;
int vibeBoard = 11;
int inputPin = A5;
int inputPin2 = A4;
int inputPin3 = A3;

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

const int numReadings2 = 10;

int readings2[numReadings2];      // the readings from the analog input
int readIndex2 = 0;              // the index of the current reading
int total2 = 0;                  // the running total
int average2 = 0;                // the average


const int numReadings3 = 10;

int readings3[numReadings3];      // the readings from the analog input
int readIndex3 = 0;              // the index of the current reading
int total3 = 0;                  // the running total
int average3 = 0;                // the average



void setup() {

  pinMode(pushButton, INPUT_PULLUP);

  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
   for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) {
    readings2[thisReading2] = 0;
  }
     for (int thisReading3 = 0; thisReading3 < numReadings3; thisReading3++) {
    readings3[thisReading3] = 0;
  }
  Keyboard.begin();
  pinMode(vibeBoard, OUTPUT);
}

void loop() {

  int buttonState = digitalRead(pushButton);

  if (buttonState == LOW) {

    // subtract the last reading:
    total = total - readings[readIndex];
    // read from the sensor:
    readings[readIndex] = analogRead(inputPin);
    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    // if we're at the end of the array...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    }

    // calculate the average:
    average = total / numReadings;


        // subtract the last reading:
    total2 = total2 - readings2[readIndex2];
    // read from the sensor:
    readings2[readIndex2] = analogRead(inputPin2);
    // add the reading to the total:
    total2 = total2 + readings2[readIndex2];
    // advance to the next position in the array:
    readIndex2 = readIndex2 + 1;

    // if we're at the end of the array...
    if (readIndex2 >= numReadings2) {
      // ...wrap around to the beginning:
      readIndex2 = 0;
    }

    // calculate the average:
    average2 = total2 / numReadings2;

            // subtract the last reading:
    total3 = total3 - readings3[readIndex3];
    // read from the sensor:
    readings3[readIndex3] = analogRead(inputPin3);
    // add the reading to the total:
    total3 = total3 + readings3[readIndex3];
    // advance to the next position in the array:
    readIndex3 = readIndex3 + 1;

    // if we're at the end of the array...
    if (readIndex3 >= numReadings3) {
      // ...wrap around to the beginning:
      readIndex3 = 0;
    }

    // calculate the average:
    average3 = total3 / numReadings3;
    
    // send it to the computer as ASCII digits
    Serial.print("average 1 = ");
    Serial.print(average);
    Serial.print("\t average 2 = ");
    Serial.print(average2);
    Serial.print("\t average 3 = ");
    Serial.println(average3);

    if (average > 390 && average < 410) {
   
      Keyboard.press('b');
      delay(50);
      Keyboard.releaseAll();
 
    } else if (average2 > 600 && average2 < 610) {
        Keyboard.press('n');
      delay(50);
      Keyboard.releaseAll();
    } else if (average3 > 300 && average3 < 310) {
        Keyboard.press('h');
      delay(50);
      Keyboard.releaseAll();
    }
  }
}
