Hi,

I am trying write some code to get the HPWM to drive a motor in forward and reverse.
I have trawled the forums and google for ages and I have come up with this, but it doesn't want to compile.I get Syntax erros and bad expressions.

Can I write If .. then loops like this??
-----------------------------------------------------------------------------------------
WHILE 1 = 1 'Infinite loop
IF S1 = LOW THEN
IF SPEED = 0 THEN DIRECTION = REVERSE 'If motor stopped, set reverse direction

IF DIRECTION = REVERSE THEN 'If engine in reverse, then..
IF SPEED < 255 THEN INC SPEED 'If Speed less than Maximum, then increase

ELSE 'Else goto FORWARD loop
IF SPEED > 0 THEN DEC SPEED 'If speed not yet 0, then decrease speed

ENDIF
ENDIF

IF S2 = LOW THEN 'If S2 pressed, then
IF SPEED = 0 THEN DIRECTION = FORWARD 'If motor stopped, set forward direction

IF DIRECTION = FORWARD THEN 'If engine in reverse, then..
IF SPEED < 255 THEN INC SPEED 'If speed not yet 255, then increase speed

ELSE 'Else goto REVERSE loop)
IF SPEED > 0 THEN DEC SPEED 'If speed not yet 0, then reduce until 0

ENDIF
ENDIF

FORWARD = DIRECTION 'As DIRECTION = HIGH then
'FORWARD = HIGH
REVERSE = ~FORWARD 'REVERSE is negated to FORWARD

HPWM 1, SPEED, 2000 'Alloctae 'SPEED' value to the CCP module

PRINT AT 1, 13, DEC (SPEED * 100) / 255, "% " 'Reflect speed in %

DELAYMS 10 'Increase SPEED
IF SPEED = 0 THEN DELAYMS 700 'Wait longer when speed =0
WEND 'To While
-----------------------------------------------------------------------------------------

Could someone please steer me in the right direction.