Control of leds pwm

zoo12

New Member
Good morning this is my first post and would be grateful for any assistance with my code that will not do what I expect. I have split the code into two i.e. TOUCH and PWM and both work ok on their own.
The touch code is based on AXE181 data sheet and uses 25mm sq plates, the PWM on VSHOTT post of 22-07-2011 PWM FAN. The idea is to dim 2 surplus white LED strip lighting units via a Picaxe 20M2 using touch plates switches controlling a n- channel Mosfet via PWM.
The original PWM Circuit used two switches to ground the pull up resistors on B.0 and B.1
The code appears to simulate in PE and loads with no problems but when run the output on C.5 just stays static at a constant PWM output. Two chips have been tried. Many thanks.

Code
camp light1.jpg
Code:
'20m2

main:   
   	touch16 C.2, w2 ;UP read sensor C.2 into word variable w2
	if w2 > 5500 then
low B.0 
	else
high B.0 
	end if
	
	touch16 c.3, w3 ; DOWN read sensor C.3 into word variable w3
	if w3 > 5500 then
low B.1 
	else

high B.1 
	end if



Symbol Duty = W0
   Duty = 400  ' start with medium duty
   PWMOut C.5, 249, Duty

TOP:
   GoSub Flash  ' flash the LED on Out0
   
   If PinB.0 = 0 and PinB.1 = 0 Then StopPWM ' if both pushbuttons depressed
   If PinB.0 = 0 and PinB.1 = 1 Then Slower ' if one depressed
   If PinB.0 = 1 and PinB.1 = 0 Then Faster ' if other depressed
    ' no change if neither depressed.
   GoTo TOP      			' continually loop

StopPWM:
   PWMOut C.5, 0, Duty   	 ' zero the period
   GoTo TOP
Slower:
   If Duty < 20 Then Top 	' minimum floor
   Duty = Duty - 15  		' decrease duty
   PWMOut C.5, 250, Duty
   Pause 100
   GoTo TOP
Faster:
   If Duty > 984 Then Top
   Duty = Duty + 15 		 ' increase duty
   PWMOut C.5, 250, Duty
   Pause 100
   GoTo TOP

Flash:    ' flash LED on Out0
   High C.7
   Pause 100
   Low C.7
   Pause 100
   Return
  goto main ; loop
 

MartinM57

Moderator
Welcome!

I think you have a problem with B.0 and B.1:
- you set them high or low at the start depending on the touch switches, but then read them in your tests to determine whether to stop PWM, go slower or go faster.
- ..but as they are tied high with resistors they will both read as 1, so none of your tests are true and you just go round (the code) in circles

Don't try to use the physical pins B.0 and B.1 as variables - use real program variables

Try (untested)
Code:
[COLOR="#FF0000"]#picaxe 20m2

symbol up = b10
symbol down = b11[/COLOR]

main:   
   	touch16 C.2, w2 ;UP read sensor C.2 into word variable w2
	if w2 > 5500 then
'low B.0
[COLOR="#FF0000"]up = 1[/COLOR]
	else
'high B.0
[COLOR="#FF0000"]up = 0[/COLOR] 
	end if
	
	touch16 c.3, w3 ; DOWN read sensor C.3 into word variable w3
	if w3 > 5500 then
'low B.1
[COLOR="#FF0000"]down = 1[/COLOR] 
	else

'high B.1
[COLOR="#FF0000"]down = 0 [/COLOR]
	end if



Symbol Duty = W0
   Duty = 400  ' start with medium duty
   PWMOut C.5, 249, Duty

TOP:
   GoSub Flash  ' flash the LED on Out0
   
'   If PinB.0 = 0 and PinB.1 = 0 Then StopPWM ' if both pushbuttons depressed
'   If PinB.0 = 0 and PinB.1 = 1 Then Slower ' if one depressed
'   If PinB.0 = 1 and PinB.1 = 0 Then Faster ' if other depressed
[COLOR="#FF0000"]   If up = 1 and down = 1 Then StopPWM ' if both pushbuttons depressed
   If up = 0 and down = 1 Then Slower ' if one depressed
   If up = 1 and down = 0 Then Faster ' if other depressed[/COLOR]
    ' no change if neither depressed.
   GoTo TOP      			' continually loop

StopPWM:
   PWMOut C.5, 0, Duty   	 ' zero the period
   GoTo TOP
Slower:
   If Duty < 20 Then Top 	' minimum floor
   Duty = Duty - 15  		' decrease duty
   PWMOut C.5, 250, Duty
   Pause 100
   GoTo TOP
Faster:
   If Duty > 984 Then Top
   Duty = Duty + 15 		 ' increase duty
   PWMOut C.5, 250, Duty
   Pause 100
   GoTo TOP

Flash:    ' flash LED on Out0
   High C.7
   Pause 100
   Low C.7
   Pause 100
   Return
  goto main ; loop
 

westaust55

Moderator
For most of your code, after the initial check of the touch sensors, you are looping between scanning the condition of pins B.0 and B.1 without returning to the Main part for scanning the touch sensors.
Additionally with the newer parts there is the PWMDuty command which can be sued after the PWMOUT is first activated.

I have also moved the PWMOUT and Duty to the start so that it not within the main program loop.

try this code (untested) and see if it improves your situation:

Code:
'20m2
'Declarations/aliases
Symbol Duty = W0

Init:
   Duty = 400  ' start with medium duty
   PWMOut C.5, 249, Duty

Main:   
   	touch16 C.2, w2 ;UP read sensor C.2 into word variable w2
	if w2 > 5500 then
	     low B.0 
	else
	     high B.0 
	end if
	
	touch16 c.3, w3 ; DOWN read sensor C.3 into word variable w3
	if w3 > 5500 then
	     low B.1 
	else
	    high B.1 
	end if


TOP:
   GoSub Flash  ' flash the LED on Out0
   
   If PinB.0 = 0 and PinB.1 = 0 Then StopPWM ' if both pushbuttons depressed
   If PinB.0 = 0 and PinB.1 = 1 Then Slower ' if one depressed
   If PinB.0 = 1 and PinB.1 = 0 Then Faster ' if other depressed
    ' no change if neither depressed.
   GoTo Main      			' continually loop

StopPWM:
   PWMDuty C.5, 0, Duty   	 ' zero the period
   GoTo Main
Slower:
   If Duty < 20 Then Top 	' minimum floor
   Duty = Duty - 15  		' decrease duty
   PWMDuty C.5, 250, Duty
   Pause 100
   GoTo Main
Faster:
   If Duty > 984 Then Top
   Duty = Duty + 15 		 ' increase duty
   PWMDuty C.5, 250, Duty
   Pause 100
   GoTo Main

Flash:    ' flash LED on Out0
   High C.7
   Pause 100
   Low C.7
   Pause 100
   Return
 
Last edited:

westaust55

Moderator
MartinM57 got in faster there :)

But he has not changed the
GOTO TOP​
to
GOTO Main​
to retest the touch sensors.

His comments about using pins are "variables" is true so leave it to you to roll the two examples into a new version.
 

MartinM57

Moderator
ah yes - the goto TOP problem is fatal as well..as is the (WA55 corrected) GOTO at the bottom of flash: that should be a RETURN.

The code appears to simulate in PE
I suspect it didn't ;)
 

zoo12

New Member
MartinM57
Both your codes loads but give me the same static PWM out put.
Westaust55 your cod stops at line 35 with a sintax error " PWMDuty C.5, 0, Duty ' zero the period"

I am off for a walk and will look at it again when I return from looking at the boats in the Solent thanks for you help so far.
 

westaust55

Moderator
Westaust55 your cod stops at line 35 with a syntax error " PWMDuty C.5, 0, Duty ' zero the period"
Knew there was something fishy about it :)

just remove the period parameter and try:

Code:
'20m2
'Declarations/aliases
Symbol Duty = W0

Init:
   Duty = 400  ' start with medium duty
   PWMOut C.5, 249, Duty

Main:   
   	touch16 C.2, w2 ;UP read sensor C.2 into word variable w2
	if w2 > 5500 then
	     low B.0 
	else
	     high B.0 
	end if
	
	touch16 c.3, w3 ; DOWN read sensor C.3 into word variable w3
	if w3 > 5500 then
	     low B.1 
	else
	    high B.1 
	end if


TOP:
   GoSub Flash  ' flash the LED on Out0
   
   If PinB.0 = 0 and PinB.1 = 0 Then StopPWM ' if both pushbuttons depressed
   If PinB.0 = 0 and PinB.1 = 1 Then Slower ' if one depressed
   If PinB.0 = 1 and PinB.1 = 0 Then Faster ' if other depressed
    ' no change if neither depressed.
   GoTo Main      			' continually loop

StopPWM:
   PWMDuty C.5, Duty   	 ' zero the period
   GoTo Main
Slower:
   If Duty < 20 Then Top 	' minimum floor
   Duty = Duty - 15  		' decrease duty
   PWMDuty C.5, Duty
   Pause 100
   GoTo Main
Faster:
   If Duty > 984 Then Top
   Duty = Duty + 15 		 ' increase duty
   PWMDuty C.5, Duty
   Pause 100
   GoTo Main

Flash:    ' flash LED on Out0
   High C.7
   Pause 100
   Low C.7
   Pause 100
   Return
Did you read the text that went with the code put forward by Martin and myself or just try the code.
If you had read the words about the changes and the "banter" between Martin and I, you would understand why Martin's code did not fix the situation.
Mind you, my code is also untested
 
Last edited:

Goeytex

Senior Member
Here is a version that eliminates the unecsessary "ifs" in the main loop and consolidates the code a bit.
As has been noted, there is no need to use outputs b.0 and b.0. I have eliminated the need to use up or down
variables by directly comparing the touch readings. Some pauses may need to be added to slow the loop down
a bit.

Code:
#picaxe 20m2
#no_data

Symbol Duty = W0
symbol Up_Sensor = W1
symbol Down_Sensor = W2

Duty = 400       'start with medium duty
PWMOut C.5, 249, Duty




MAIN:

touch16 C.2, up_sensor    ; UP read sensor C.2  
touch16 c.3, down_sensor  ; DOWN read sensor C.3    
   
   GoSub Flash  ' flash the LED on Out0
   
   If up_sensor > 5500  and  down_sensor > 5500  Then StopPWM  ' if both pushbuttons depressed
   If up_sensor > 5500  and  down_sensor < 5500  Then Slower   ' if one depressed
   If up_sensor < 5500  and  down_sensor > 5500  Then Faster   ' if other depressed
   If up_sensor < 5500  and  down_sensor < 5500  Then MAIN 

GOTO MAIN 
       			   

StopPWM:
   duty = 0
   PWMDUTY C.5, Duty  	  'ZERO THE  DUTY CYCLE
   Goto MAIN 

Slower:
   If Duty < 20 Then MAIN 	' minimum floor
   Duty = Duty - 15  		' decrease duty
   PWMDUTY C.5, Duty
   Pause 100
   Goto MAIN   

Faster:
   If Duty > 984 Then MAIN
      DUTY = Duty + 15 		 ' increase duty
   PWMDUTY C.5, Duty
   Pause 100
   Goto MAIN 
      
Flash:    ' flash LED on Out0
   High C.7
   Pause 100
   Low C.7
   Pause 100
   Return
 
Last edited:

zoo12

New Member
Fishy nah just lot of mud and sea gulls tides out.

But back to the serious stuff, yes I did read both Martin and your mail but variables are new to me . Which is why in wanted to try them in the above program, but failed..

I have just tried your last code and it works well so thank you all who have helped. I can now go on and play to see where i went wrong.

Is there any tutorials on variables and memory??
 

MartinM57

Moderator
well done - and nice code Goey...

Tutorials - have a good read of PICAXE manual 1, especially the sections:
- Understanding the PICAXE Memory
- Tutorials 1 to 11

Come back and ask anything that is not clear...
 

zoo12

New Member
Martin and goeytex
Many thanks for reply, the tutorial did not do much for me (over head!!) but now i have something to work with that i know the out come.
goeytex code was the smoothest yet switched down to 3mA up to 215mA like silk. many thanks again.
 
Top