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<=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<=8) value1=-100;
if (value1+100<=value2+100) {
for(int fadeValue = value2+100 ; fadeValue <= value1+100; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(10);
}
}
else
{
for(int fadeValue = value2+100 ; fadeValue >= 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.
2 Answers 2
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<=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);
-
so I changed that part of the code to this
int value1=0; Serial.println(value); if (value<=8) value=-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,value+100,100); lastValue = value1; delay(10);
Where value is the actual input from the sensor. and it sort of works as I changed one to the new value ( LED) And used directly for the sound. The LED doesnt seem to switch off ever, some residue feelCollectiveum– Collectiveum2015年05月17日 15:04:24 +00:00Commented May 17, 2015 at 15:04 -
Try
value1 = 0.1*value + 0.9*lastValue;
insteadGerben– Gerben2015年05月17日 15:12:24 +00:00Commented May 17, 2015 at 15:12 -
Nope, similar results. Do you think using some kind of counter would help slowly fade it out when the sensor is not being played with?Collectiveum– Collectiveum2015年05月17日 15:27:47 +00:00Commented May 17, 2015 at 15:27
-
Some more playing around. If I remove the statement that makes the counter -100 and put a zero instead and change the +100 values everywhere I use value1/value then the LED dies out when not interacting, but the upper limit also drops.
int value1=0; Serial.println(value); if (value<=8) value=0; value1 = (1*value + 9*lastValue)/10; analogWrite(ledPin,value1); tone(tonepin,value,100); lastValue = value1; delay(10);
Tried the same code by substituting +100 by *10 and removing -100, but then the smoothing obviously also starts scrambling to some degree. Delay is the key?Collectiveum– Collectiveum2015年05月17日 15:46:23 +00:00Commented May 17, 2015 at 15:46 -
I think a nice way to redefine this challenge at this time would be to say that we need to figure out a way to keep the smoothed response while widening the range of output which has the minimum at 0 and maximum at a nice high range. While all this happens the minimum value should be achieved if low input values occur over a time ( seeing them as feedback and not really input)Collectiveum– Collectiveum2015年05月17日 16:14:41 +00:00Commented May 17, 2015 at 16:14
You need to re-think the entire way of dealing with the fading.
Instead of saying "The value has changed, fade the LED to that brightness", you need to say "The value has changed. I want the LED to be this brightness." followed by "Is the LED the brightness I want? No? Then move it a bit towards the brightness I want".
Something like, in pseudo-code:
TargetBrightness = SensorValue
if LEDBrightness < TargetBrightness
increment LEDBrightness
set LED to LEDBrightness
if LEDBrightness > TargetBrightness
decrement LEDBrightness
set LED to LEDBrightness
If that all happens too fast then you need to only run the brightness tests at certain periods. Something like:
TargetBrightness = SensorValue
Has 10ms passed since last check? Yes?
Remember the time this check happened.
if LEDBrightness < TargetBrightness
increment LEDBrightness
set LED to LEDBrightness
if LEDBrightness > TargetBrightness
decrement LEDBrightness
set LED to LEDBrightness
delay()
calls.delay()
is eeeeeeeeeeeevil.