i have the led connected up to react to an ir sensor, i have a low setting, so the led outputs nothing at the low level, and the idea being that when your hand is 10cm away its at half brightness and when its 2cm away its at full brightness...
this i have working fine.
what i want is when i take my hand away, instead of turning off, i want it to fade out.
bright2 is a mapped reading from the analog sensor
i can put a fade in, but it just constantly drops the brightness.. i cant get around my head, how to detect for a constant interruption (hand), and then change when the interruption disappears.
static int lastBrightness = 0;
if (ir2 < lowsetting1) {
if (lastBrightness > 0)
lastBrightness --; // Dim the light til it reaches 0
} else {
lastBrightness = bright2; // Set the brightness to match distance
}
analogWrite(led2, lastBrightness);
much appreciated, not for the code as much, but for the introduction to static int... this would have solved quite a lot of my if / else issues... but also for the code guidance!
static int lastBrightness = 0;
if (ir2 < lowsetting1) {
if (lastBrightness > 0)
lastBrightness --; // Dim the light til it reaches 0
if (lastBrightness > 0)
lastBrightness --; // Dim the light twice as fast
} else {
lastBrightness = bright2; // Set the brightness to match distance
}
analogWrite(led2, lastBrightness);
ok, so i've got this bad boy working how it should be, and i've gone for a more streamlined way of getting the fade, by multiplying by a sub 1 integer... basically, getting a percentage fade;
50%
256, 128, 64, 32, 16, 8, 4, 2, 1
75%
256, 192, 144, 108, 81, 61, 45, 34
which is really easy to control, and gives a nice fade...
but it leads me into another slight issue, but i've a feeling its not overly easy to fix.
when i get down to low levels of light, the leds are noticeably flickery... not a massive issue, but noticeable enough to take the professional look away from it. if i was to 10% frost you wouldn't notice...
just wondering if anyone else has a better solution.
ok, i know why this doesn't work, what i don't know is how to make it work!
this is the layout for setting the fade1 integer
[code]
int fade1 = 0.9;
void setup ()
void loop ()
and here is the chunk of code it refers to;
static int lastBright1 = 0; // set the lastbright variable
if (ir1<lowsetting1) { // check to see if the ir is detecting above the base level
analogWrite(led1, LOW); // turn the led off
if (lastBright1 > 0) // check to see if the last brightness is more than 0
lastBright1 = lastBright1 * fade1; // Dim the light til it reaches 0
} else {
lastBright1 = max1; // Set the brightness to match distance
}
if (lastBright1 < lowestbrightness) { // check to see if the led has got to a low level
lastBright1 = 0; // then turn the led off
}
analogWrite(led1, lastBright1); // write the brightness to the led
[/code]
if i change the lastBright1 = lastBright1 * 0.9 i get the desired fade
if i set a static int for fade1 just before the if(), it treats it as * 1 (no fade)
if i take the int out of the void loop () then i get the error not declared in scope, which i know is to do with setting a variable outside the if / else brackets.
no matter where i seem to plonk the int, it won't seem to take reference.
kelvinmead:
when i get down to low levels of light, the leds are noticeably flickery... not a massive issue, but noticeable enough to take the professional look away from it. if i was to 10% frost you wouldn't notice...
just wondering if anyone else has a better solution.
the blinking serial monitor!
I'm displaying lots of useful information, and i start up the serial monitor everytime so i can see the numbers changing... im getting real close to everything running smoothly so i dont start up comment out the serial monitor and bang! all the fading now runs 3 times as fast and its real smooth!
you live and learn eh... now i've just gotta slow down the fading some more! lol