I'm trying to write a program that takes inputs from 4 potentiometers, converts it to a PWM duty cycle and outputs it to one of 4 motors. The pot-motor is a static assignment, so say Pot1 directly controls Motor1. I'm early in the software design phase and trying to optimize my code since I know I'll probably be using most of the program storage.
I want to write a single read pot/assign pwm duty routine that can can work for any of the pot/motor pairs. I have it essentially done, but am getting tripped up on the PWMDUTY command (pwmduty oMotor, iSpeed). In the compile error window, the little carrot is under the apostrophe between the pin and duty cycle.
I know I can just write 4 different IF/THEN statements, which I was previously doing, but I'd like to shorten/optimize this.
I want to write a single read pot/assign pwm duty routine that can can work for any of the pot/motor pairs. I have it essentially done, but am getting tripped up on the PWMDUTY command (pwmduty oMotor, iSpeed). In the compile error window, the little carrot is under the apostrophe between the pin and duty cycle.
I know I can just write 4 different IF/THEN statements, which I was previously doing, but I'd like to shorten/optimize this.
Code:
'---[I/O PIN DEFINITIONS]---
SYMBOL Motor1 = c.3 'PWM1 output pin
SYMBOL Motor2 = b.1 'PWM2 output pin
SYMBOL Motor3 = c.2 'PWM3 output pin
SYMBOL Motor4 = c.5 'PWM4 output pin
SYMBOL Pot1 = b.0 'Pot1 input pin
SYMBOL Pot2 = b.3 'Pot2 input pin
SYMBOL Pot3 = b.2 'Pot3 input pin
SYMBOL Pot4 = b.0 'Pot4 input pin
SYMBOL Pot5 = c.1 'Pot5 input pin
'---[VARIABLE DEFINITIONS]---
SYMBOL cSpeed1 = b2 'Motor1 current speed
SYMBOL cSpeed2 = b3 'Motor2 current speed
SYMBOL cSpeed3 = b4 'Motor3 current speed
SYMBOL cSpeed4 = b5 'Motor4 current speed
SYMBOL iPot = b6 'Temp input pot pin
SYMBOL oMotor = b7 'Temp output motor pin
SYMBOL iSpeed = w4 'ADC input
'---[CONSTANT DEFINITIONS]---
SYMBOL period = 199 'PWM period, calculated for 20kHz
SYMBOL dutyMult = 50 'PWM duty cycle multiplier for speed control
SYMBOL dutyMin = 300 'PWM minimum duty cycle
'Initialize frequency, setup I/O pins and PWM output.
setup:
setfreq M16
dirsb = %00000010
dirsc = %00101101
pwmout pwmDiv4, Motor1, period, 0
pwmout pwmDiv4, Motor2, period, 0
pwmout pwmDiv4, Motor3, period, 0
pwmout pwmDiv4, Motor4, period, 0
main:
iPot = Pot1
oMotor = Motor1
gosub readInput
iPot = Pot2
oMotor = Motor2
gosub readInput
iPot = Pot3
oMotor = Motor3
gosub readInput
iPot = Pot4
oMotor = Motor4
gosub readInput
goto main
readInput:
readadc iPot, iSpeed
gosub calcSpeed
if iSpeed <> cSpeed1 then
cSpeed1 = iSpeed
if iSpeed <> 0 then
iSpeed = iSpeed * dutyMult + dutyMin
endif
pwmduty oMotor, iSpeed
endif
return
calcSpeed:
iSpeed = iSpeed + 25
iSpeed = iSpeed * 1 / 26
return