 
 #define MAX_TPS_IN 800
 #define MIN_TPS_IN 100
-#define POWER 0.333
+#define POWERUP 0.333
+#define POWERDOWN 3
+#define EXTRA 100
 
 //0.52-4.7 -> 3-235
 //0.55-3.9 -> 3-195
 
 Adafruit_MCP4725 dac;
-static unsigned short table[MAX_TPS_IN-MIN_TPS_IN+1];
+static unsigned short table_up[(MAX_TPS_IN - MIN_TPS_IN+1)/2];
+static unsigned short table_down[(MAX_TPS_IN - MIN_TPS_IN + 1)/2]; 
+
+int accumulated[20];
+int TPS_DIFF =MAX_TPS_IN - MIN_TPS_IN + 1;
 
 #define PRINT 0
+
+int pTPS;  //previous TPS
 
 void setup() {
 #if PRINT

  pinMode(INPUT_PIN, INPUT);
  dac.begin(0x62);
  //pinMode(OUTPUT_PIN, OUTPUT);
  for (int i = 0; i < MAX_TPS_IN-MIN_TPS_IN+1; i++) {
    float x = (((float)i)/(MAX_TPS_IN-MIN_TPS_IN+1));
    x = pow(x,POWER);
    x = x * (UPPER-LOWER);
    unsigned short res = ((unsigned short)x) + LOWER;
    if (res > UPPER) res = UPPER;
    if (res < LOWER) res = LOWER; 
    table[i] = res;
#if PRINT
    Serial.print(i+MIN_TPS_IN);
    Serial.print(" ");

}

void loop() {
  int v_in = analogRead(INPUT_PIN); 
  int i_in = v_in - MIN_TPS_IN;
  int idx;
  if (pTPS - i_in > EXTRA) i_in = i_in + 150;
  else if (i_in - pTPS > EXTRA) i_in = i_in - 150;
  
  if (i_in < 0) idx = 0;
  else if (i_in > MAX_TPS_IN - MIN_TPS_IN) idx = MAX_TPS_IN-MIN_TPS_IN;
  else idx = i_in;

  int v_out;
  int movingAv = 0; // CHANGE THIS
  if (movingAv > 0)
  v_out = table_up[idx];
  else
  v_out = table_down[idx];
  //analogWrite(OUTPUT_PIN, v_out);
  dac.setVoltage(v_out, false);
  pTPS = i_in;
#if PRINT
  Serial.print(v_in);
  Serial.print(" ");
