First off thanks for all the answers to my previous questions. Ok here's the scenario. I have my Mega connected to a DC motor via an H bridge. The motor is connected to a 10 turn pot via a drive belt. The pot is connected back to the Mega's analog 'A0' port. My program lets you input a setpoint via the serial monitor and the system is supposed to drive to the new setpoint. if I manually set the pot to a low value (input<100) and then input a high setpoint (setpoint=1000) ,it drives to the new set point (+- about 40).
if I then input a low setpoint nothing happens. Long story short the whole program is wildly unpredictable. Anyone have any ideas to whats going on? I am a PID newbie. Thanks for any help!
/********************************************************
* Arduino PID Library
* by Brett Beauregard
* contact: br3ttb@gmail.com
* PID Adaptive Tuning Example
* One of the benefits of the PID library is that you can
* change the tuning parameters at any time. this can be
* helpful if we want the controller to be agressive at some
* times, and conservative at others. in the example below
* we set the controller to use Conservative Tuning Parameters
* when we're near setpoint and more agressive Tuning
* Parameters when we're farther away.
********************************************************/
#include <PID_v1.h>
//Define Variables we'll be connecting to
double Input, Output;
double Setpoint;
int pin=2;
//Define the aggressive and conservative Tuning Parameters
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=1, consKi=0.05, consKd=0.25;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd ,DIRECT);
void setup()
{
//initialize the variables we're linked to
Serial.begin(9600);
Serial.setTimeout(10);
Input = analogRead(0);
Setpoint = 10;
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(200);
}
void loop()
{
Input = analogRead(0);
if (Serial.available()>0)
{
Setpoint=Serial.parseInt();
}
if (Setpoint >= Input)
{
pin=2;
myPID.SetControllerDirection(DIRECT);
}
else
{
pin=3;
myPID.SetControllerDirection(REVERSE);
}
double gap = abs(Setpoint-Input); //distance away from setpoint
if(gap<10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
analogWrite(pin, Output);
Serial.print(Setpoint);
Serial.print(" ");
Serial.print(pin);
Serial.print(" ");
Serial.print(Output);
Serial.print(" ");
Serial.println(Input);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.