 }
 
 void loop() {
-  for(int i = 0; i < 5; i++)
-    input_val = cutAndMap(input.filteredAnalogRead(AVERAGE), 50, 975, 0, 1023);
+  input_val = cutAndMap(input.filteredAnalogRead(AVERAGE), 50, 975, 0, 1023);
 
-  /*if(input_val < 0)
-    input_val = 0;
-  else if(input_val > 1023)
-    input_val = 1023;*/
-
-  if(ledState == HIGH)
-    analogWrite(output_pin, input_val/4);
-  else if(ledState == LOW)
-    analogWrite(output_pin, 0);
-    
+  // read sensor
   sensorReading = digitalRead(tilt_sensor);
   
   if (sensorReading != lastTiltState) {

    // than the debounce delay, so take it as the actual current state:
    
    // if the button state has changed:
    if (sensorReading != tiltState)
    {
      tiltState = sensorReading;
      
      // if the sensor reading is low
      if (sensorReading == LOW) {
        // make the status of the output_pin to go on:
        ledState = HIGH;   
        analogWrite(output_pin, input_val/4);
          
        // dalay the LED on for one second:
        delay(500);
      }
      // otherwise if it is high
      else if (sensorReading == HIGH){
        // make the status of the output_pin to stay off:
        ledState = LOW;
        analogWrite(output_pin, 0);
      }
    }
   }
   
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastTiltState = sensorReading; 
  
  // delay to avoid overloading the serial port buffer:
  delay(100);
}                                                                                                                                                                                                                                                                                            

int lastTiltState = HIGH; // the previous reading from the input pin

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

SignalProcessing input(input_pin, filter_size);

