The basic "P" term in the PID control loop sets the increase based on how far away you are from the goal:
potValue = analogRead(potPin);
currentValue = analogRead(0);
ValveValue = ValveValue + (potValue - currentValue) * Gain;
OutputValue = map(ValveValue, 0, 1023, 0, 255);
analogWrite(ValvePin, OutputValue);
Note this keeps ValveValue at high resolution (0-1023) but maps it down to 0-255 for output only. Gain may be varied to change the speed of response. Use a floating-point number because it will probably end up rather small, like 0.05.
Get Answers For Free
Most questions answered within 1 hours.