How to fade in and fade out led strip connected to arduino based on continous analog input from capacitative sensor? So I've been trying to get a capacitive sensor made from aluminum foil to work. And I have had success the kind of proximity and readings I need to light up a single LED and produce basic tone. Using this very basic code : 

{
  Serial.println(value1);//value that i get from analog sensor
  //this is just to get the values in order for the led and sound
  if (value1&lt;=8) value1=-100; 
  analogWrite(ledPin,value1+100);//these weird calculations seem to work for me
  tone(tonepin,value1+100,100); 
  delay(10);


All this works perfectly for me, the delay isn't perceivable, the response is for all practical purposes simultaneous to input. Except it's not beautiful, because the LED keeps flickering when values reduce or increase because the jumps are sudden I suppose. 

SO I thought I'll try to fade in and fade out to different values from present values.  Used the fading examples. Here:

Serial.println(value1);
  if (value1&lt;=8) value1=-100;

  if (value1+100&lt;=value2+100) {
    for(int fadeValue = value2+100 ; fadeValue &lt;= value1+100; fadeValue +=5) {

    analogWrite(ledPin, fadeValue);        

    delay(10);                            
    } 
  }

  else 
  {

    for(int fadeValue = value2+100 ; fadeValue &gt;= value1+100; fadeValue -=5) 
    {
        analogWrite(ledPin, fadeValue);        

    delay(10);                            
    } 
  }
  //analogWrite(ledPin,value1+100);
  tone(tonepin,value1+100,100); 
  delay(10);
  value2=value1;


But this takes away the responsiveness, the whole thing is so slow to react, the sound and the light are obviously not in sync as I read my code. But also the light is not really fading, still blinking, but there seems to be some effect but I am not sure. 

Any help with what I could do and what I am doing wrong would be nice.
Smooth the change in output value by taking, e.g 10% of the new value, and 90% of the old value. You can try experimenting with different ratios and longer/shorter delays.

int lastValue = 0;

{
  Serial.println(value1);//value that i get from analog sensor
  //this is just to get the values in order for the led and sound
  if (value1&lt;=8) value1=-100;
  value1 = (1*value + 9*lastValue)/10;// smooth the change in value by taking 10% of the current value, and 90% of the previous value
  analogWrite(ledPin,value1+100);//these weird calculations seem to work for me
  tone(tonepin,value1+100,100); 
  lastValue = value1;
  delay(10);