PID

I'm making a project for school. The meaning of the project is to balance a steel ball on 2 resistance wire with PID and a servomotor. But my problem is that my output is always 1600 for every position of the ball.

Is there something missing in myn program or so?

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


Servo servo;

  int weerstand = A2;
  int led_aan = 11;
  int led_uit = 10;
  int schakelaaraan = 4;
  int schakelaaruit =3;
  int variable; 
  int microseconds; 
  
  float Kp=5;
  float Ki=5;
  float Kd=5;
  
  double Setpoint, Input, Output;
  PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
  
void setup()
{
  pinMode(10, OUTPUT);
  myPID.SetOutputLimits(1400, 1600);
  pinMode(11, OUTPUT);
  pinMode(4, INPUT);
  pinMode(3, INPUT);
  servo.attach (9);
  myPID.SetMode(AUTOMATIC);
  Setpoint = 225;
  variable = LOW;
}

void loop()
{
  if(digitalRead(schakelaaruit) == HIGH) variable = LOW;
  if(digitalRead(schakelaaraan) == HIGH) variable = HIGH;
  if( variable == LOW){
      servo.writeMicroseconds(1500);
      digitalWrite(led_uit, HIGH);
      digitalWrite(led_aan, LOW);
}
  if( variable == HIGH){
    digitalWrite(led_uit, LOW);
    digitalWrite(led_aan, HIGH);
    Input = analogRead(weerstand);
    myPID.Compute();
    servo.writeMicroseconds(Output);
  }
}

Tell me if I'm right about these words:
weerstand = resistance
schakelaaruit = switch off
schakelaaraan = switch on
led_aan = LED on
led_uit = LED off
Is that what they mean?

I don't see any diagnostic messages in your sketch. Have you tried printing some? If so, what does it tell you? If not, how do you determine what the sketch is doing?

How did you determine the values of Kp, Ki and Kd? Do you have some reason to believe that these are reasonable values?

You don't show your circuit, or give us any description of it. I presume that there's a metal ball folling on a pair of conductive rails, and that your sketch will calculate the ball's position based on the resistance between the ball and the ends of the rails. But, that's just a guess. Can you show us a schematic, and describe the mechanical part as well?

This sounds like an academic project. Based on my experience, if you want participation from the forum, you'll have to do almost all the work, with occasional guidance from the forum. We don't typically do anyone's homework for them.

I think the important thing to do is print "Input" to see if your resistance is varying.

How do you have the wire hooked up? what is the top resistance in the voltage divider leg and what voltage is it connected to?

I can pretty much guarantee your PID parameters (Kp, Ki, Kd) are NOT correct. I don't think I've ever seen a case where all three parameters could be set to the same value. In general, P will be much larger than I, which will be much larger than D. What you have looks like a recipe for completely uncontrolled operation. Start by setting Ki and Kd to 0, set a low value of Kp (1 or less), and see how it behaves. You will likely find you don't even need Ki and Kd.

Regards,
Ray L.

tmd3:
Tell me if I'm right about these words:
weerstand = resistance
schakelaaruit = switch off
schakelaaraan = switch on
led_aan = LED on
led_uit = LED off
Is that what they mean?

Yep