Blazemaguire
Senior Member
Hi,
I'm experimenting with fading an LED based on the distance from an ultasonic range finder unit. - I've got the code working in principle, but its not quite working as nicely as I want
When the distance is at maximum extents (about 285mm) the LEDs should be be on minimum brightness, and then as you reduce the distance to the sensor, the brightness increases, upto about 30mm from the sensor. (If I go closer than 30mm, I get messy readings from the sensor)
The problem at the moment is that the LEDs seem to flicker, as though they're turning on and off, although they are giving the correct brightness levels. - The flickering is far more pronounced at further distances (i.e, when the brightness should be lowest), and less pronounced the closer I get to the sensor.
Is it something to do with the 20us recharge period for the ultrasonic reading? - Does this shut down the pulseout command every time it takes a reading?
If so, does anyone have a workaround? I'm using a 20m2 chip
Also, am I barking up the wrong tree by keeping the duty cycle the same throughout at 150,150? - Using 150,150 throughout worked fine when I did the same experiment with a potentiometer - as in, minimum brightness at one end of the pot's travel and max brightness at the other, exactly as needed, with no flicker
Any help, as always, greatly received.
Rob
I'm experimenting with fading an LED based on the distance from an ultasonic range finder unit. - I've got the code working in principle, but its not quite working as nicely as I want
When the distance is at maximum extents (about 285mm) the LEDs should be be on minimum brightness, and then as you reduce the distance to the sensor, the brightness increases, upto about 30mm from the sensor. (If I go closer than 30mm, I get messy readings from the sensor)
The problem at the moment is that the LEDs seem to flicker, as though they're turning on and off, although they are giving the correct brightness levels. - The flickering is far more pronounced at further distances (i.e, when the brightness should be lowest), and less pronounced the closer I get to the sensor.
Is it something to do with the 20us recharge period for the ultrasonic reading? - Does this shut down the pulseout command every time it takes a reading?
If so, does anyone have a workaround? I'm using a 20m2 chip
Also, am I barking up the wrong tree by keeping the duty cycle the same throughout at 150,150? - Using 150,150 throughout worked fine when I did the same experiment with a potentiometer - as in, minimum brightness at one end of the pot's travel and max brightness at the other, exactly as needed, with no flicker
Any help, as always, greatly received.
Rob
Code:
symbol ultrain =b.5
symbol ultraout=b.4
symbol red=c.2 'Red RGB pin
'symbol green=c.3 'Green RGB pin
'symbol blue=c.5 'blue RGB pin
symbol range=w0
symbol range_neg=b3
main:
pulsout ultraout,2 ; produce 20uS trigger pulse (must be minimum of 10uS)
pulsin ultrain,1,range ; measures the range in 10uS steps
pause 20 ; recharge period after ranging completes
let range = range * 100 / 58 ; multiply by 10 then divide by 58 to convert range to mm
if range>285 then 'had to add 30 to range to get full range of dimness to brightness (see line 42)
range=285
range_neg=1
'sertxd(#range," mm, Neg:",#range_neg,cr,lf) 'for debugging readings
pwmout red,150,150
pwmduty red,range_neg
goto main
elseif range<30 then '
range=30 'checking to see if the reading is too close a distance
range_neg=255
pwmout red,150,150
pwmduty red,range_neg
'sertxd(#range," mm, Neg:",#range_neg,cr,lf) 'for debugging readings
goto main
else
range_neg=-range ' convert the range to 'opposite' so that brightness increase the closer you get.
range_neg=range_neg+30 'compensating for the fact you can't get to within the last 30mm, otherwise, LED would never fade all the way to 255 (max brightness)
pwmout red,150,150
pwmduty red,range_neg
'sertxd(#range," mm, Neg:",#range_neg,cr,lf) 'for debugging readings
endif
goto main