Looking for my PID code to be re-written from scratch (No PID library)[WILL PAY]

Im doing a project for school, and used this code for arduino using the PID_v1.h library to complete it. My teacher decides to tell us now (1 month before project is due) we cannot use the PID library, and need to write it out from scratch so to speak. Im a mechanical engineer and dont have the time to teach myself the amount of coding required to do this. The project uses a sonic sensor and servo to balance a ball on a beam. Needs to be something like this https://gist.github.com/ivanseidel/b1693a3be7bb38ff3b63.

Here’s the code, THANKS

// note: for my beam to be horizontal, Servo Motor angle should be 105 degrees.

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

const int servoPin = 9; //Servo Pin

float Kp = 2; //Initial Proportional Gain
float Ki = .5; //Initial Integral Gain
float Kd = .5; //Intitial Derivative Gain
double Setpoint, Input, Output, ServoOutput;

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); //Initialize PID object, which is in the class PID.

Servo myServo; //Initialize Servo.

void setup() {

Serial.begin(9600); //Begin Serial
myServo.attach(servoPin); //Attach Servo

Input = readPosition(); //Calls function readPosition() and sets the balls
// position as the input to the PID algorithm

myPID.SetMode(AUTOMATIC); //Set PID object myPID to AUTOMATIC
myPID.SetOutputLimits(-20,20); //Set Output limits to -80 and 80 degrees.

}

void loop()
{

Setpoint = 12;
Input = readPosition();

myPID.Compute(); //computes Output in range of -80 to 80 degrees

ServoOutput=105-Output; // 102 degrees is my horizontal
myServo.write(ServoOutput); //Writes value of Output to servo

}

float readPosition() {
delay(40); //Don’t set too low or echos will run into eachother.

const int pingPin = 12;
const int pingPin2 = 13;

long duration, cm;
unsigned long now = millis();
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin2, INPUT);
duration = pulseIn(pingPin2, HIGH);

cm = (duration/(29*2));

if(cm > 20) // 30 cm is the maximum position for the ball
{cm=20;}

Serial.println(cm);

return cm; //Returns distance value.
}

Have you looked at the PID library? The library only has a few methods, and they are pretty simple.

There is nothing to stop you from copying the methods as functions in your code. Change the names and the comments so it is less obvious that you are calling the teacher's bluff.

I am just wondering what you are supposed to LEARN if you have the job done by someone else. you should obviously know the math involved if you are an engineer, and translate that in code for arduino its trivial. If you have no idea about the math… then failing the test its just a good occasion to actually trying to understand the matter. I wrote a PID application to balance a magnetic field and it consist of just 56 lines of code, comments included.
sorry for being harsh but I think that would be plain cheating

I understand how a PID works, basically three different gains that adjust the input based on the output. One is a proportional (adjusts by simple ratios), one by using a derivative (the change in the set point), and an integral (trying to get the sample areas to all match each other). In industry we will never code a PID from scratch, these have been industrialized and are much more robust than any engineer can make with such minimal coding knowledge, so why does he make us do this? I am working on my senior design project which takes precedence of this stupid project and at this point would rather give someone $75 to produce me a code and be done, and never touch an arduino again in my life.

thats the point: you dont know how to do it, yet you think its a stupid project. he makes you do that so you will learn something and understand how things works. if you take all your studies with the idea that is better to “give someone $75” then you dont need to be an engineer, you just need money to buy things from other (real) engineers

This was supposed to be a group project (One mechanical, one mechatronic) the mechanical was supposed to size parts and build the structure, the mechatronic to do coding. Well...my partner bailed on me so now im stuck. If you do not want to help please save me the lecture.

ok no lectures, you obviously know everything already... good luck