user14149: I'm currently working on a project where I need to use PID to get the correct speed for a motor when it is angled a certain amount. 
The problem is that I'm only getting 0.00 and 255.00 as the speed. Nothing in between. The code is supposed to activate when the angle value is more than 1300 and less than -1300. The ax variable is from an MPU6050 sensor.
int16_t ax, ay, az;

double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp,Ki,Kd, DIRECT);

void setup() {
  Input = analogRead(ax);
  Setpoint = 100;
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop() {
  accelgyro.getAcceleration(&ax, &ay, &az);
  int a=ax+10900;
  //Here I set the used variable to around 0. ax is actually around -10900
  //This is always true, this won't be in the final code. This is just making it work.
  if(1) {
    int val;
    val = map(a, -1300, 1300, 0, 1023);
    Input = val;
    myPID.Compute();
    Serial.println(Output);
  }
}


user13432: Please respect others by presenting code that is readable. By doing that you may also find obvious errors in your code. Let me clean up the loop() and you will see what I mean.
void loop() {
    int a = ax + 10900;
    if (1) {
      int val;
      val = map(ax, -1300, 1300, 0, 1023);
      Input = val;
      myPID.Compute();
      Serial.println(Output);
    }
  }
}

So what is "int a = ax + 10900"? "ax" is not defined but the other use of it is as an analog pin number "Input = analogRead(ax)". And "a" is not used. 
There is a condition that is always true "if (1)" and so on. 
Cheers!

user6628: How fast is your system compared to the loop() sampling time?  Does (100-map((ax+10900),-1300,1300,0,1024)) change appreciably during the sub-millisecond loop()?
With kI=5, an error of only 10 will saturate your 0-255 speed output in 26 iterations of loop(), potentially producing the behavior you see.  If you don't have control of your sampling time, the integral error can wind up (or down) to the process limits very quickly.
From https://www.arduino.cc/en/Reference/Map map() only linearly transforms the input data to the output data, but does not constrain() it to those limits.  If ax=-9000, then val would be greater than 1024.   Still, taking the normal working range of ax+10900 to be (-1300,1300) which is then mapped into  (0,1024), the Kp=2 term with a setpoint of 100 would produce a range of (-200,2048) constrained to the default (0,255) limits.   
Working backwards from the setpoint of 100, through the 0-1024 map is 0.0975 along the (-1300 to 1300) range, or val = -949, or ax = -11849.  And the kP=2 times the mapping factor of 1024/2600 would change the output by 2*1024/2600=0.79 points for each step difference of ax from -11849.
The http://playground.arduino.cc/Main/MPU-6050 pages says the acceleration registers are 16 bit with high sensitivity, and suggests that 50 points of noise is a small variation.  If the sensor set for 16 bits for +/-2G into ax, then, each step change 60 micro-G.  If that is the source of your ax, then the small variations could easily saturate your control system, making it bang-bang between its output limits with only milli-G variations at the sensor.
Slow down your sampling time, reduce kI, and think of your Kp, Ki, and Kd as conversions of your measured and translated variable ax errors into output space (0-255 speed).
Also, in choosing a sampling time, consider how often you want the PID to make adjustments to the process.  

14149: <p>I'm currently working on a project where I need to use PID to get the correct speed for a motor when it is angled a certain amount. </p>

<p>The problem is that I'm only getting 0.00 and 255.00 as the speed. Nothing in between. The code is supposed to activate when the angle value is more than 1300 and less than -1300. The <code>ax</code> variable is from an MPU6050 sensor.</p>

<pre><code>int16_t ax, ay, az;

double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&amp;Input, &amp;Output, &amp;Setpoint, Kp,Ki,Kd, DIRECT);

void setup() {
  Input = analogRead(ax);
  Setpoint = 100;
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop() {
  accelgyro.getAcceleration(&amp;ax, &amp;ay, &amp;az);
  int a=ax+10900;
  //Here I set the used variable to around 0. ax is actually around -10900
  //This is always true, this won't be in the final code. This is just making it work.
  if(1) {
    int val;
    val = map(a, -1300, 1300, 0, 1023);
    Input = val;
    myPID.Compute();
    Serial.println(Output);
  }
}
</code></pre>
 13432: <p>Please respect others by presenting code that is readable. By doing that you may also find obvious errors in your code. Let me clean up the loop() and you will see what I mean.</p>

<pre><code>void loop() {
    int a = ax + 10900;
    if (1) {
      int val;
      val = map(ax, -1300, 1300, 0, 1023);
      Input = val;
      myPID.Compute();
      Serial.println(Output);
    }
  }
}
</code></pre>

<p>So what is "int a = ax + 10900"? "ax" is not defined but the other use of it is as an analog pin number "Input = analogRead(ax)". And "a" is not used. </p>

<p>There is a condition that is always true "if (1)" and so on. </p>

<p>Cheers!</p>
 6628: <p>How fast is your system compared to the loop() sampling time?  Does (100-map((ax+10900),-1300,1300,0,1024)) change appreciably during the sub-millisecond loop()?</p>

<p>With kI=5, an error of only 10 will saturate your 0-255 speed output in 26 iterations of loop(), potentially producing the behavior you see.  If you don't have control of your sampling time, the integral error can wind up (or down) to the process limits very quickly.</p>

<p>From <a href="https://www.arduino.cc/en/Reference/Map" rel="nofollow">https://www.arduino.cc/en/Reference/Map</a> map() only linearly transforms the input data to the output data, but does not constrain() it to those limits.  If ax=-9000, then val would be greater than 1024.   Still, taking the normal working range of ax+10900 to be (-1300,1300) which is then mapped into  (0,1024), the Kp=2 term with a setpoint of 100 would produce a range of (-200,2048) constrained to the default (0,255) limits.   </p>

<p>Working backwards from the setpoint of 100, through the 0-1024 map is 0.0975 along the (-1300 to 1300) range, or val = -949, or ax = -11849.  And the kP=2 times the mapping factor of 1024/2600 would change the output by 2*1024/2600=0.79 points for each step difference of ax from -11849.</p>

<p>The <a href="http://playground.arduino.cc/Main/MPU-6050" rel="nofollow">http://playground.arduino.cc/Main/MPU-6050</a> pages says the acceleration registers are 16 bit with high sensitivity, and suggests that 50 points of noise is a small variation.  If the sensor set for 16 bits for +/-2G into ax, then, each step change 60 micro-G.  If that is the source of your ax, then the small variations could easily saturate your control system, making it bang-bang between its output limits with only milli-G variations at the sensor.</p>

<p>Slow down your sampling time, reduce kI, and think of your Kp, Ki, and Kd as conversions of your measured and translated variable ax errors into output space (0-255 speed).</p>

<p>Also, in choosing a sampling time, consider how often you want the PID to make adjustments to the process.  </p>
