help with pwm code

tonyb1943

New Member
help needed with pwm code
i need to press a switch to bring on a led full brightness
then press another switch to start dimming the led over time to off say 20 minutes
can anybody help
 

eclectic

Moderator
Welcome (again) to the Forum.

Please tell us what hardware you have
and how it's connected.

The solution is probably straightforward
once folk know your setup.

e
 

1968neil

Senior Member
The following code should get you started, press switch to light the led and start a fade over time.
I'm assuming that you are making some kind of night light ?
This will help you on your way without doing all the work for you ;)
I've commented the code to help you understand what's going on.
Regards
Neil




Code:
#picaxe 08m2
symbol val = b0 
symbol pwmPin = c.2     			;led via 330R resistor
symbol pbswitch = pinC.3                  ; Push button switch (tied logic low via 10k)


initialise:

pwmout pwmdiv4, pwmPin, 249, 500    	;set PWM


Main: 

do

   if Pbswitch = 1 then exit       ; Sit here until Push button switch is pressed

loop



						      ;loop to fade led  using PWM
val = 255
     do
	 pwmduty pwmPin, val           ; set pwm duty
	 val = val -1
	 for b1 = 1 to 6   	;loop to slow down fader
	 pause 1000
	
	 next
	 if val = 0 then goto Led_off  ;when PWM at 0 then turn off pwm
     loop


Led_Off:

      PWMOUT pwmPin,OFF
		
Goto initialise
 

tonyb1943

New Member
Welcome (again) to the Forum.

Please tell us what hardware you have
and how it's connected.

The solution is probably straightforward
once folk know your setup.

e
the hardware i am using is a axe091 development board with a picaxe 18m2+
 

The bear

Senior Member
Hi Everyone,
I 'borrowed' 1986neils' PWM code, as a learning curve. Question; How do you start to calculate the run time for this code?
I ran it, it finished after 26 minutes. Impressive!

Regards, Bear..
 

JimPerry

Senior Member
The program is counting down from 255 to 0 in 26 minutes = 6.11 seconds a count (approx.) - it is also delaying 6 times 1000mS each time ( about 6 seconds) do the Picaxe "overhead" is about 10mS per count - change the pause value to 100 and will complete in about 2.6 minutes :confused: Try it and see :rolleyes:
 

hippy

Ex-Staff (retired)
Another way to get a long fade down would be to use the 'time' variable which increments every second and calculate what the duty should be at a specific time. That is, there are 1200 seconds in 20 minutes, so the duty can be the inverse of that, decreasing from 1200 to 0 over that time.

Unfortunately 1200 is too large for a duty ( which has a maximum of 1023 ) but a 1023 to 0 duty can be calculated from a 1200 to 0 time -

duty = ( 1200 - ( time Max 1200 ) ) * ( 1023 / 1200 )

Alternatively, the maths can be made simpler and the PWM frequency adjusted to match what the duty may be. For example a 1200 to 0 can be easily converted to 600 to 0, and with a maximum duty of 600 for 100% brightness requiring a frequency of 150 ( freq = maxDuty / 4 ).

For dimming a higher maximum duty is best so a duty of 900 to 0 and a frequency of 225 is better.

duty = ( 1200 - ( time Max 1200 ) ) * ( 900 / 1200 )

That 900/1200 is a simpler 3/4. Untested ...

Code:
Initialise:
  PwmOut pwmPin, 225, 900

WaitForButtonPush:
  Do : Loop until Pbswitch = 1

FadeDown:
  time = 0
  Do
    val = time Max 1200
    val = 1200 - val * 3 / 4
    PwmDuty pwmPin, val
  Loop Until val = 0
The 'time Max 1200' is defensive programming, to handle the case should 'time' ever get higher than 1200. If there is no risk of that then the maths can be simplified -

val = 1200 - time * 3 / 4
 

The bear

Senior Member
Hello Picaxers,

Thanks to Jim & Hippy, for your explanations & example, I will work on it.

Regards, Bear. (1942 vintage, or should say antique).
 
Top