how to use arduino PID library?

hi
i am new to arduino programming and i want to use the PID library as i am using a motor with position encoder, the example provided on the arduino PID website is confusing
my current code is :

#include <PID_v1.h>

//Define Variables we’ll be connecting to
double Setpoint, Input, Output;
int countA=0;
int last_A;
float error_A, error_int_A, error_calc_A, D_A;
int forward = 9;
int backward = 7;
int pwm = 10;
int directionForward = 5;
int directionBackward = 2;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,5,0,30, DIRECT);

void setup()
{ //mototA
pinMode(pwm, OUTPUT);
pinMode(forward, OUTPUT);
pinMode(backward, OUTPUT);
pinMode(directionBackward , INPUT);
pinMode(directionForward , INPUT);
attachInterrupt(digitalPinToInterrupt(directionBackward ), pulseCountA, RISING);
Serial.begin(19200);

//initialize the variables we’re linked to
Input = digitalRead(5);
Setpoint = 100;
// SetOutputLimits(-255, 255);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}

void loop()
{

Input = digitalRead(5);

myPID.Compute();
analogWrite(10,Output);
digitalWrite(backward, HIGH);
digitalWrite(forward, LOW);
Serial.print("countA: "); Serial.println(countA);

}

but the code is not performing as needed, instead my motor just runs contieously to position 6000 and does not stop at 100.
can anyone help please? or share code?
also how do i use the arduino atuo tune library?
thanks

If you're new to arduino programming....then probably should start small first, rather than jumping straight into the thick stuff.

In your code...

Input = digitalRead(5);
Setpoint = 100;

I see those two settings above...ie Input and Setpoint .... but I don't see what happens to these values after that point.

I'm thinking that those values need to be inserted into :

PID myPID(&Input, &Output, &Setpoint,5,0,30, DIRECT);

..... however, that particular line appears to occur BEFORE you the definition of Input and Setpoint .... so looks like an issue with ordering of the code/commands.

Also, set the serial speed to maximum, since a slower speed can actually slow things down...

so set the serial speed to 115200 (instead of 19200).

Also...... add comments in your code, to make things easy for somebody else to follow...eg..

Double lines // is the start of a comment.....like.....

int forward = 9; // initial forward speed or something
int backward = 7; // initial backward speed
int pwm = 10; // initial pwm level
int directionForward = 5;
int directionBackward = 2;

Southpark:
In your code…

Input = digitalRead(5);
Setpoint = 100;

I see those two settings above…ie Input and Setpoint … but I don’t see what happens to these values after that point.

I’m thinking that those values need to be inserted into :

PID myPID(&Input, &Output, &Setpoint,5,0,30, DIRECT);

… however, that particular line appears to occur BEFORE you the definition of Input and Setpoint … so looks like an issue with ordering of the code/commands.

If you think that, you need to revise your C/C++, particularly about pointers and the address-of operator,
and the difference between definition and assignment.

The code is valid, its defining Input and Setpoint, then passing their addresses to the PID constructor, then
Input is being updated from digital pin 5 on a regular basis.

Back to the real issues:

  1. Glaring problem - Output is a signed value, so you must use that sign to set the direction, something like
    this:
  digitalWrite (forward, Output > 0) ;
  digitalWrite (backward, Output <= 0);
  analogWrite (10, constrain (abs (Output), 0, 255)) ;

If you only drive you motor one way, it is not being position controlled (speed controlled perhaps,
but not position).

  1. Your input is binary, pin 5. If your PID input is two-state, its not going to be able to do anything
    useful for you. Your input needs to be the encoder position, an integer. You seem to have not got
    anything to read the encoder.

MarkT:
If you think that, you need to revise your C/C++, particularly about pointers and the address-of operator,
and the difference between definition and assignment.

No revision necessary. Just a reminder or note that pointers are involved will sort it. I overlooked the & symbol.

I saw no pointer declaration in the line :

double Setpoint, Input, Output;

So &Input would mean 'address of' Input.