Problem with PWM command on 08M2

dpbarry

New Member
Hi Folks..

Following on from my lighthouse/buoyage chart picture, I decided to go with a mixture of 08M2 and 14M2's and at present all works.

I've been looking at using the pwmout command to simulate the flashing sequence of the lighthouse and using the PWM code attached, it works - a nice ramp up and ramp down effect rather than a straight on-off sequence BUT

if I add in a second led to pin 2, it appears to revert the pwm pin back to a straight on/off sequence.

Any pointers as to what I have done wrong.

Declan
 

Attachments

hippy

Technical Support
Staff member
The PWMOUT simply adjusts the time the LED is on for against the time it is off for; the longer it is on for the brighter the eye will make it appear to be.

If a LED is on for half the time it should appear less bright than one on all the time, two LED's and they should both be less bright than being on all the time.

I can't see any obvious explanation as to why that would simply stop working when another LED was added unless there were some other issue creating the outcome such as the LED drawing too much current and resetting the PICAXE, the on-off appearance being a result of continually resetting rather than PWMOUT control.
 

AllyCat

Senior Member
Hi,

You have used a separate series resistor for each LED (and a decoupling capacitor between suppy and ground pins) haven't you?

As hippy says, your symptoms sound like unintended resets: Put a #TERMINAL 4800 and a SERTXD("Starting",cr,lf) at the top of your program to find out.

Cheers, Alan.
 

dpbarry

New Member
The PWMOUT simply adjusts the time the LED is on for against the time it is off for; the longer it is on for the brighter the eye will make it appear to be.

If a LED is on for half the time it should appear less bright than one on all the time, two LED's and they should both be less bright than being on all the time.

I can't see any obvious explanation as to why that would simply stop working when another LED was added unless there were some other issue creating the outcome such as the LED drawing too much current and resetting the PICAXE, the on-off appearance being a result of continually resetting rather than PWMOUT control.
Sorry Hippy..

I maybe confused the issue. What I meant was, I added in a second parallel task that just flashed a sequence but in adding this in, it has stripped out the PWM from the first task. both LED flash their sequence but the first led doesn't perform with the pwmout. I've even changed the batteries just in case the are below par but still no joy.

I've attached the second test program.

Declan
 

Attachments

hippy

Technical Support
Staff member
Thanks for the clarification. In multi-tasking mode the time periods for PAUSE are slightly altered and this could be the issue here. Changing your main: loop to below might help improve things. Have increased the PAUSE 10 to 20 and altered the STEP (+/-)25 to (+/-)50 to compensate, and also using PWMDUTY rather than PWMOUT -

Code:
main: 'First LED Sequence using PWM
do
 pwmout 2, 249, 0
 for w1= 1 to 2
 	for w0 = 0 to 1000 step 50 'led ramping up
  		pwmduty 2, w0
  		pause 20
	next w0
pause 250
	for w0 = 1000 to 0 step -50 'led ramping down
  		pwmduty 2, w0
  		pause 20
	next w0
pause 250
next w1
pause 3500 ' 3500 pause
loop
It might not fix things but worth a try. If it doesn't we may have to think further on that and try and duplicate the hardware.
 

dpbarry

New Member
Cheers Hippy..

That seems to have done the trick. One led is working nicely with your pmwduty code and the other led is quite happily just flashing on and off.

Many thanks for your help.

Declan

Thanks for the clarification. In multi-tasking mode the time periods for PAUSE are slightly altered and this could be the issue here. Changing your main: loop to below might help improve things. Have increased the PAUSE 10 to 20 and altered the STEP (+/-)25 to (+/-)50 to compensate, and also using PWMDUTY rather than PWMOUT -

Code:
main: 'First LED Sequence using PWM
do
 pwmout 2, 249, 0
 for w1= 1 to 2
 	for w0 = 0 to 1000 step 50 'led ramping up
  		pwmduty 2, w0
  		pause 20
	next w0
pause 250
	for w0 = 1000 to 0 step -50 'led ramping down
  		pwmduty 2, w0
  		pause 20
	next w0
pause 250
next w1
pause 3500 ' 3500 pause
loop
It might not fix things but worth a try. If it doesn't we may have to think further on that and try and duplicate the hardware.
 

hippy

Technical Support
Staff member
That seems to have done the trick.
Excellent news. In multi-tasking pauses are I believe in 20ms increments rather than the usual 1ms so I was guessing the PAUSE 10 might be becoming PAUSE 0 which runs rather quicker than intended. It was fading but just fading so quickly that it wasn't observable. Using PWMOUT which restarts the duty timing may have been compounding that.
 

dpbarry

New Member
Hi Hippy..

Sorry about this but could you explain a wee bit more on what your code is doing that my original coding wasn't doing so that I am 'clear' (ish) in my own mind what is happening.

I'm interested in the initial pwmout statement then the pmwout within the loop. I've been trying to understand the commands in the manual but not quite getting it.

Declan

Thanks for the clarification. In multi-tasking mode the time periods for PAUSE are slightly altered and this could be the issue here. Changing your main: loop to below might help improve things. Have increased the PAUSE 10 to 20 and altered the STEP (+/-)25 to (+/-)50 to compensate, and also using PWMDUTY rather than PWMOUT -

Code:
main: 'First LED Sequence using PWM
do
 pwmout 2, 249, 0
 for w1= 1 to 2
 	for w0 = 0 to 1000 step 50 'led ramping up
  		pwmduty 2, w0
  		pause 20
	next w0
pause 250
	for w0 = 1000 to 0 step -50 'led ramping down
  		pwmduty 2, w0
  		pause 20
	next w0
pause 250
next w1
pause 3500 ' 3500 pause
loop
It might not fix things but worth a try. If it doesn't we may have to think further on that and try and duplicate the hardware.
 

hippy

Technical Support
Staff member
I am not quite sure what more there is to explain other than what is in post #9.

PWMOUT reinitialises PWM output every time it is executed so there can be consequences with that when PWMOUT is executed frequently. PWMDUTY lets PWM output continue without reinitialising and just changes the duty so should not have the same consequences.
 

dpbarry

New Member
Ahh!! Understand you now..

Would you describe it as a more 'cleaner' way of using the commands to do what I need?

Declan

I am not quite sure what more there is to explain other than what is in post #9.

PWMOUT reinitialises PWM output every time it is executed so there can be consequences with that when PWMOUT is executed frequently. PWMDUTY lets PWM output continue without reinitialising and just changes the duty so should not have the same consequences.
 

hippy

Technical Support
Staff member
Would you describe it as a more 'cleaner' way of using the commands to do what I need?
Probably. PWMDUTY doesn't have the restarting of the PWM which PWMOUT has. Note in the following that the second PWMOUT at the top reset the PWM and truncated the output pulse; the missing 5 step. The bottom keeps the 12345 sequencing -

Code:
PWMOUT        PWMOUT
  |             |
____---__---__--____-____-_
  1234512345123412345123451

PWMOUT        PWMDUTY
  |             |
____---__---__---____-____-_
  12345123451234512345123451
But I think the main thing was probably the PAUSE 10 becoming 'non existent' when multi-tasking.
 

stan74

Senior Member
You used pwmout ,use that in init then on pwmduty
init:
pwmout pwmdiv16, B.0, 249, 499
main:
pwmduty B.0,whatever
 
Top