how to stop this 'pre flicker' in LED fade up routine

Blazemaguire

Senior Member
Hi,

I've adapted some code I found in a previous post regards fading an LED up - It works in a nice controlled manner and with some playing, now has the effect I want, however, I get an annoying 'pre flicker' before the main fade up kicks in. i.e, a very quick low brightness 'blip' and then the LEDs fade up as expected.

Does anyone have any insight into what's causing the flicker in the code?

Is it the pwmout command after the init: routine declaration? to my knowledge, that part needs to be there otherwise the other PWMduty command doesn't work. - Does anyone have any tips or workarounds that might get rid of the flicker? or am I dealing with a hardware issue and barking up the wrong tree?

thanks

Rob



Code:
symbol val = w0 
symbol pwmPin = c.2     			;led array
symbol fade_rate=4

init:
pwmout pwmdiv4, pwmPin, 249, 500
   	;set PWM

main: 						;loop to fade led array using PWM
val = 0
	do
		pwmduty pwmPin, val  ; set pwm duty
		val = val +fade_rate 'increase this to increase the fade rate
			'
				pause 1 'defines how quickly the fade down occur as well
			'
		
		if val = 1000 then pwmout pwmpin,off high pwmpin
		endif  
	loop
 

hippy

Technical Support
Staff member
You don't seem to be going to 'main:' nor resetting the 'val' to zero which may cause some problems.

You could also try removing the PWMOUT...OFF and the "high pwmpin".
 

Bill.b

Senior Member
Hi Rob

In you init you are setting the PWM to 50%, this may be causing the flicker for a very short duration.

try setting the pwmout command to '0'

init:
pwmout pwmdiv4, pwmPin, 249, 0


Bill
 

Goeytex

Senior Member
The blip could be that the Pwm Pin is floating at power up, or more likely because of the initial PWM setting of PWMOUT with a duty of 500.

1. Add a 10K pulldown resistor from the PWM pin to ground
2. Initialize PWM with "pwmout pwmdiv4, pwmPin, 249, 0"
3. Instead of turning PWM off and setting the Pin high, simply let it remain at a duty of 1000
4. The same when you want the light to remain off after fading down, just assure that pwmduty = 0

Try the modified code below and see if your glitch goes away

Code:
symbol duty = W0 
symbol pwmPin = c.2     ;led array

'change these to change effect
symbol fade_rate = 4
symbol delay = 1      

init:
pwmout pwmdiv4, pwmPin, 249, 0         ' start PWM with duty of 0
   	
main:            ;loop to fade led array using PWM

do 
    gosub fade_In
    pause 2000
    gosub fade_out
    pause 2000
loop


Fade_In:
 
    duty =0
    
    do while duty < 1000  
       duty = duty + fade_rate
       pwmduty pwmPin, duty  
       pause delay
    loop
    pwmduty PwmPin, 1000            ' make sure Led is full on (Redundant) 
    
Fade_Out:
    
    duty = 1000
    
    do until duty <= 1
       duty = duty - fade_rate
       if duty > 1000 then 
          duty = 0           'in case of underflow where duty becomes 65535    
       endif
       pwmduty pwmPin, duty
       pause delay
   loop
   pwmduty pwmPin,0  'assure led is off    
   return
 

jims

Senior Member
Goeytex...I put a "return" at the end of the "fade in" routine and it gives quite a different effect. Jims
The blip could be that the Pwm Pin is floating at power up, or more likely because of the initial PWM setting of PWMOUT with a duty of 500.

1. Add a 10K pulldown resistor from the PWM pin to ground
2. Initialize PWM with "pwmout pwmdiv4, pwmPin, 249, 0"
3. Instead of turning PWM off and setting the Pin high, simply let it remain at a duty of 1000
4. The same when you want the light to remain off after fading down, just assure that pwmduty = 0

Try the modified code below and see if your glitch goes away

Code:
symbol duty = W0 
symbol pwmPin = c.2     ;led array

'change these to change effect
symbol fade_rate = 4
symbol delay = 1      

init:
pwmout pwmdiv4, pwmPin, 249, 0         ' start PWM with duty of 0
   	
main:            ;loop to fade led array using PWM

do 
    gosub fade_In
    pause 2000
    gosub fade_out
    pause 2000
loop


Fade_In:
 
    duty =0
    
    do while duty < 1000  
       duty = duty + fade_rate
       pwmduty pwmPin, duty  
       pause delay
    loop
    pwmduty PwmPin, 1000            ' make sure Led is full on (Redundant) 
    
Fade_Out:
    
    duty = 1000
    
    do until duty <= 1
       duty = duty - fade_rate
       if duty > 1000 then 
          duty = 0           'in case of underflow where duty becomes 65535    
       endif
       pwmduty pwmPin, duty
       pause delay
   loop
   pwmduty pwmPin,0  'assure led is off    
   return
 

Goeytex

Senior Member
Thanks,

Looks like I forgot the return in that routine. It should have been there and was intended. Will correct.

It should fade in over about 1 sec or so, hold on for about 2 seconds, fade out over 1 second, hold off for 2 seconds, then repeat.

But the main thing is that it should not glitch/flash at power up, or at the beginning of the fade_in sub.
 

inglewoodpete

Senior Member
Rob, you have not mentioned the circuitry that connects the PICAXE to the LED. If it is a simple series resistor then ignore the following.

If you are using a transistor or FET to drive the LED, then the design of the circuit may affect the LED during boot up. The PICAXE output is initialised by the firmware as an input. As a result, the PICAXE output will float until it your software sets it as an output, which will also set its logical state (Low or High). If this is the case, you will need to ensure that the transistor is turned off in some way, depending on the actual circuit configuration.

P.S My wife is different to yours. She locks me in the shed and only lets me out when she wants. :)
 

Blazemaguire

Senior Member
Thanks guys. Removing the '500' and changing to 0 in the pwmout section solved the flicker. I guess this is the hazard of using code from other people when you don't fully understand it. Thanks again.
 
Top