PID Output does not work for negative values [PID Library]

Hello!

I am trying to implement a PID control for my robot and I have stumbled across a problem! The Setpoint for the servomotor is 80, and I get PID Outputs for any Input that is less than 80, but not for any over 80. This means that my PID will only adjust if my servo rotates to the right, not if it rotates to the left. How do I make sure that if the input is more than 80, it gives me an output?

Please see my attached code below. I am using the PID Library.

#include <PID_v1.h>
#include <Servo.h>
#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 25 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

Servo myservo;  // create servo object to control a servo

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

int distanceLeft;
int distanceRight;
int threshold;

int currentAngle;

int pos=80;    // variable to store the servo position

double Setpoint = 80;
double Input;
double Output;

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

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(12, 11, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(4, 3, MAX_DISTANCE),
 
};

void setup() {
  Serial.begin(9600);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
    myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    myPID.SetMode(AUTOMATIC);
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = MAX_DISTANCE + 10;      // Always set to higher than MAX_DISTANCE
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  // The following code would be replaced with your code that does something with the ping results.

  for (uint8_t i = 0; i < SONAR_NUM; i++) {                 // Go through the all the sensors.
    if(i==0) {                                               
    distanceLeft=cm[i];                                     // Assign distance to variables.
    distanceRight=cm[i+1];
    }
    if (i==1) {
      distanceLeft=cm[i-1];                                // Assign distance to variables.
      distanceRight=cm[i];
    }
    servoFollow();
    pid();
  }
}

void servoFollow() {
  if (distanceLeft <= MAX_DISTANCE || distanceRight <= MAX_DISTANCE)
  {
    if (distanceLeft + 5 < distanceRight) 
    {
      pos = pos + 2;
    }
    if (distanceRight + 5 < distanceLeft)
    {
      pos = pos - 2;
    }
  }
  if (pos > 160)
  {
    pos = 160;
  }
  if (pos < 0)
  {
    pos = 0;
  } 
  myservo.write(pos);
  currentAngle = myservo.read();
  
}

void pid() {
  Input = currentAngle;
  myPID.Compute();
  Serial.println(Output);
}

I think I solved it by using SetOutputLimites(-255,255). I would love to get a confirmation that this is the correct way to go.

Thank you.

Yes. The default limits are 0-255 (suitable for PWM output). If you want negative outputs you must change the limits.