Motor control

Mark.R

Member
Hi all, I have a small dc motor and wish to use a picaxe to control the speed. what I am not sure about and wonder if anyone could do one for me is the code. I would like it that when say input 1 is pulsed high the motor runs at half speed and then when input 2 is pulsed high it runs up to full speed.

Any help would be much appreciated
 

MPep

Senior Member
Look up PWM, to select various speeds, and IF PIN = High then ....
Hope this helps,
MPep.
 

eclectic

Moderator
Hi all, I have a small dc motor and wish to use a picaxe to control the speed. what I am not sure about and wonder if anyone could do one for me is the code. I would like it that when say input 1 is pulsed high the motor runs at half speed and then when input 2 is pulsed high it runs up to full speed.

Any help would be much appreciated
Please post more details about the motor.
A Datasheet reference would be an excellent start.

e
 

1968neil

Senior Member
@ Mark R

The following code is basic but should get you started

two buttons (momentary switches) pulled high, one when pressed will give you approx half speed and the other button will give approx full speed.

If both buttons pressed it will turn off and wait for button press again
tested in simulator so should help get you started, as others have stated the manual is the best place to start.

note: when driving dc (hobby) motors make sure to drive them via a transistor BD681 etc and add a 1N4007 and a 100n cap directly across the motor to avoid any noise upsetting the picaxe, 1k resistor between physical pin5 and the base of the transistor.

Regards

'Code

'*********************************************************
'* PWM OUT = PIN 2 (Physical pin 5) *
'* Half speed momentary switch = Pin 3 (Physical pin 4) *
'* Full speed momentary switch = Pin 1 (Physical pin 6) *
'* *
'*********************************************************


MAIN:

PAUSE 2000
IF PIN3=1 THEN Halfspeed
IF PIN1=1 THEN Fullspeed
GOTO MAIN


HALFSPEED:

pwmout 2 , 99, 200
if pin1 = 1 and pin3 = 1 then gosub MAIN
IF PIN1=1 THEN FULLSPEED
GOTO HALFSPEED


FULLSPEED:

pwmout 2 , 99, 400
if pin1 = 1 and pin3 = 1 then gosub MAIN
IF PIN3=1 THEN HALFSPEED
GOTO FULLSPEED
 
Last edited:

MartinM57

Moderator
two buttons (momentary switches) pulled high
You mean that the two input pins are pulled low with external resistors and the switches are connected to +5v so that when pressed they switch the input pins high.

If both buttons pressed it will turn off and wait for button press again
I can't see any code that will do that :(

If you press and release the buttons during the PAUSE 2000 there will be no response. The user experience will lead you you believe that the buttons are having no effect - that will seem odd.

Your GOSUB main commands should be GOTO main, or else you will pretty soon get a stack overflow (constant GOSUBs with no RETURNs) and a run time error which will probably result in random behaviour.
 

MartinM57

Moderator
Nope - still can't see.

Having turned the motor on at half or full speed via pwmout, to stop the motor you need to turn pwm off with ‘pwmout 2, off’ (or 'pwmout 2,0,0')...
 

MPep

Senior Member
Try this version:
Code:
#rem
*************************************************************
* PWM OUT = PIN 2 (Physical pin 5)                       *
* Half speed momentary switch = Pin 3 (Physical pin 4)    *
* Full speed momentary switch = Pin 1 (Physical pin 6)    *
*                                         *
* Modified by MPep                            *
* Date: 13 October 2010                            *
*                                        *
* This version stops when neither switches are activated,    * 
* OR both are activated                            *
*                                        *
*************************************************************
#endrem


#picaxe 08M

symbol FullSp = pin1
symbol HalfSp = pin3
symbol OP = pin2

MAIN:
if HalfSp = 1 and FullSp = 1 then
    goto NoSp
elseif HalfSp = 0 and FullSp = 0 then
    goto NoSp
elseif HalfSp = 1  then
    goto HalfSpeed
elseif FullSp = 1 then
    goto FULLSPEED
endif

HALFSPEED:
pwmout OP , 99, 200
goto Main

FULLSPEED:
pwmout OP , 99, 400
goto Main

NoSp:
pwmout OP,off
goto Main
 

1968neil

Senior Member
thanks for clearing that up guy's, had been a long day here!

The pause 2000 i should have removed but it aided seeing what was going in during simulation and i forgot the turn off pwm DOH ! and the gosub should have been goto as you say.

Anyway hope that all helps Mark R

Regards

Neil
 
Top