Positioning control of DC motor with PID

Hello, I am trying to control a dc motor with the dcservo code made by Misan.

I am using an Arduino Uno with this type of H-bridge.
https://www.ebay.com/itm/Dual-Motor-Driver-Module-board-H-bridge-DC-MOSFET-IRF3205-3-36V-10A-Peak-30A-NEW/172429141298?hash=item282593e532:g:gVcAAOSwiONYQAEK

The motor has 2 hall effect sensors on the output shaft to count steps.

The problem is whenever the motor creeps up to the setpoint the output never goes to 0, it stays at where it last was, say 20, the I-term then increases this over time which makes the motor to keep going, eventually it goes over the setpoint which sets the PID controller in REVERSE.

This is the code for that.

if(setpoint>input) {
      DIR=false;                                //Setting the direction to forward.
      myPID.SetControllerDirection(DIRECT);     
    }
    
    if(setpoint<input) {
      DIR=true;                                 //Setting the direction to backwards.
      myPID.SetControllerDirection(REVERSE);    //Going reverse to increase the output with negative setpoint/less than input.        
    }

It oscillates around the setpoint really fast, I guess it just goes back and forth with REVERSE and DIRECT.

I've tried to set the output to 0 manually if the error is 0, no difference.
I've tried to use Adaptive tunings from the PID example code, it does what it is supposed to, but doesn't stop the oscillations.
It seems like the PID loop want to keep going even though the goal is met...

Any suggestions?

-Anton

dcservo_V1.2_Sort_of_Working.ino (10.7 KB)

Perhaps the fine folks at http://snippets-r-us.com could help with your snippets. We can't. The problem is CLEARLY with the code you didn't post. That bit of code does nothing if input equals setpoint, so whatever was happening when input was below the setpoint continues to happen, or whatever was happening when input was above the setpoint continues to happen, until the input is again on the other side of setpoint.

You must add code to deal with input equal to setpoint.