I have installed an AutoPID library (by ryan downing version 1.0.3) to my arduino IDE, but im not sure which parts i will have remove to get the PID to control the servo angle.
Thanks for the reply, but i know i have not used the library in the sketch but i am unsure how to combine it, i have an example code for how to use the AutoPID, i will add it below.
void setup() {
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop() {
float a;
float b;
float desired_angle;
float actual_angle;
float error;
float motor;
float setpoint;
a=analogRead(A1);
desired_angle=map(a,0,1023,0,180);
b=analogRead(A2);
actual_angle=map(b,125,920,0,180);
if ( (desired_angle>0) && (desired_angle<70) )
{
setpoint=50;
}
if ( (desired_angle>70) && (desired_angle<110) )
{
setpoint=90;
}
if ( (desired_angle>110) && (desired_angle<180) )
{
setpoint=130;
}
error=(setpoint-actual_angle);
motor=desired_angle/270*255;
if (error>0)
{
digitalWrite(5,LOW);
analogWrite(6,motor);
}
if (error<0)
{
digitalWrite(6,LOW);
analogWrite(5,-1*motor);
}
Serial.print(actual_angle);
Serial.print(",");
Serial.println(setpoint);
}
and here is the example code
#include <AutoPID.h>
// PID settings and gains
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP 0.12
#define KI 0.8
#define KD 0.003
// Variables
double analogReadValue, setPoint, outputVal;
// Input/output variables passed by reference, so they are updated automatically
AutoPID myPID(&analogReadValue, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);
// Setup timeStep
void setup() {
myPID.setTimeStep(500);
}
void loop() {
setPoint = analogRead(A3); // Set our desired point (read the POT)
analogReadValue = analogRead(A1); // Read our output voltage
myPID.run(); // Run the PID
analogWrite(6, outputVal); // Write the PID result to the output
}