Pwm with Picaxe 18M2 -help!

GeoffKnight

New Member
Much appreciated if someone can advise on the following:-

I have run a small 5VDC motor / gearbox directly with the 18M2 chip, pin B.6 as below, & have had success experimenting with duty & cycle parameters to get good speed control etc.

test:
pwmout B.6,99,250
main:
pwmduty B.6,250
pause 3000
pwmduty B.6,50
pause 3000
pwmout B.6, Off

Question is..how do I get rid of the "pause" lines and substitute a "if" or "do" loop, so that the motor starts, & runs until one of two possible input's into say C.0 or C.1 is detected. I can't get the motor to run continuously (only pulse) and don't know where to put the if or do... loop.

This is what I tried & failed with:-

test:
pwmout B.6,99,250
If C.0 = 1
{pwmout B.6,OFF
goto main}
Elseif C.1 = 1
{pwmout B.6,OFF
goto main}
Else
{goto test}
Endif

thanks
 

eclectic

Moderator
Welcome to the Forum Geoff.

"I have run a small 5VDC motor / gearbox directly with the 18M2 chip"

Hmm?


Before people reply to your code requests,
could you please supply a schematic
and preferably some HQ photographs of your setup.


e
 

nick12ab

Senior Member
Why that code didn't work...

C.0 is a constant for a pin number which is basically defined with invisible 'symbol' statements so C.0 = 8. 'If 8 = 1' will always be false and the compiler won't accept 'if C.0 = 1" anyway. If you want the value at the pin, you have to use 'pinC.0' rather than just C.0.

You also don't have any 'main:' label.

Also, you didn't put any 'then' after 'if'.
 

GeoffKnight

New Member
PWM on 18M2 -for Nick12ab

Sorry, you stand correct, it should be:-
Main:
.
.
test:
pwmout B.6,99,250
If pinC.0 = 1
{pwmout B.6,OFF
goto main}
Elseif pinC.1 = 1
{pwmout B.6,OFF
goto main}
Else
{goto test}
Endif

and it still fails to work. The motor only runs for the scan time of the pwm line. i.e very short pulse. Only way I can get it to run is by using a pause line.
 

Technical

Technical Support
Staff member
The problem is you have a very tight loop which is constantly re-issuing the pwmout command over and over again. This is not correct, you need to just issue the pwmout command once when something changes, not repeatedly keep issuing it when no condition has changed within your loop.
 

nick12ab

Senior Member
Syntax error from your code:

If pinC.0 = 1

needs to be

If pinC.0 = 1 then

Also, Technical is right above!


But if you're implying that you actually got it to download, are you posting the same thing as you are downloading?
 
Last edited:
Top