/*
Michael Kontopoulos
2011

Foot Tap Amplifier (Sender)
*/

#include <NewSoftSerial.h>
NewSoftSerial mySerial =  NewSoftSerial(4, 5);

int pot = 5;
int analogOne = 0;       // X acceleration 
int analogTwo = 1;       // Y acceleration
int MAX = 100;
int THRESH = 0;
float easing = 0.5;
float target, newval, old_val;
int LED = 11;

int sensorValue = 0;     // reading from the sensor

void setup() {
  Serial.begin(9600); 
  mySerial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  serialHandler();
  
  sensorValue = analogRead(0) + analogRead(1);
  //Serial.println(sensorValue);
  sensorValue = map(sensorValue, 600,900, 0,MAX);
  sensorValue = constrain(sensorValue, 0,MAX);
  //Serial.println(sensorValue);
  THRESH = analogRead(pot);
  THRESH = map(THRESH, 0,1024, 50,70);

  target = sensorValue; //do some easing on the incoming vals
  float d = target - newval;
  if(abs(d)>1)
  {
    newval+= d*easing; //newval is now the # we want.
  }

  //Serial.println(newval);
  
  if( (newval<THRESH) && (old_val>=THRESH)) 
  {
     //send an '8'  to the serial port
     mySerial.print(8, DEC);
     digitalWrite(LED, HIGH);
  }
  else
  {
     digitalWrite(LED, LOW); 
  }

  old_val = newval;
  delay(100);

}

//////////////////////////////////
void serialHandler()
{
  if (mySerial.available()) {
    Serial.print((char)mySerial.read());
  }
  if (Serial.available()) {
    mySerial.print((char)Serial.read());
  }

  delay(100);

}