2022-04-13, 05:08 PM
Hi, my controller only has two pins: PWM and DIR. If DIR is HIGH it drives the pump one way and if it is LOW it drives it the other. The PWM input controls the speed. I greatly simplified the Arduino code, the main change being in the position() function:
// this function is called from update_command function (which is called every 20 mS by a timer) to set the motor speed and direction. The parameter (value) is sent from the PyPilot app. 1000 = do nothing. < 1000 = turn to Port (check). >1000 = turn to Starboard
// The amount the value differs from 1000 = how fast to run motor???
// Force stop motor if bypass valve open added 13/01/21
// This function sets a global Var (PWMout) which is used in main() to do an analog.write to the PWM pin.
void position(int value)
{
int cValue = 0;
cValue = abs(1000-value); // calculate amount of movement required from neutral
if (value > 1000) {DIRstate=1;} else {DIRstate=0;} // set direction of pump
PWMout=0; // default is to stop pump
if ((cValue > 20) && (BypSWS == true)) // check cValue > deadband AND bypass is NOT open (FALSE if it is)
{
PWMout = map(cValue,0,1000,125,255); // map from 0 - 1000 to suit PWM out 100 - 255 (125 to give min speed for pump to run)
}
// debug
// lcd.setCursor(0,0);
// lcd.print("v= ");
// lcd.print(value);
// set pump direction and speed
digitalWrite(pumpDIR, DIRstate);
analogWrite(pumpPWM, PWMout); // write PWM value to output
lastpos = value; // keep a copy of the last command value (used in rate of change calculation
} // end of position function
Regards
David
// this function is called from update_command function (which is called every 20 mS by a timer) to set the motor speed and direction. The parameter (value) is sent from the PyPilot app. 1000 = do nothing. < 1000 = turn to Port (check). >1000 = turn to Starboard
// The amount the value differs from 1000 = how fast to run motor???
// Force stop motor if bypass valve open added 13/01/21
// This function sets a global Var (PWMout) which is used in main() to do an analog.write to the PWM pin.
void position(int value)
{
int cValue = 0;
cValue = abs(1000-value); // calculate amount of movement required from neutral
if (value > 1000) {DIRstate=1;} else {DIRstate=0;} // set direction of pump
PWMout=0; // default is to stop pump
if ((cValue > 20) && (BypSWS == true)) // check cValue > deadband AND bypass is NOT open (FALSE if it is)
{
PWMout = map(cValue,0,1000,125,255); // map from 0 - 1000 to suit PWM out 100 - 255 (125 to give min speed for pump to run)
}
// debug
// lcd.setCursor(0,0);
// lcd.print("v= ");
// lcd.print(value);
// set pump direction and speed
digitalWrite(pumpDIR, DIRstate);
analogWrite(pumpPWM, PWMout); // write PWM value to output
lastpos = value; // keep a copy of the last command value (used in rate of change calculation
} // end of position function
Regards
David