Hi!
I'm trying to build a PID controller, with an arduino. I have an uno R3 and a motor shield with the l298p chipset. The idea is to control a valve based on temperature.
The idea is that through the analog inputs (sensor value in this case a ntc (10k) with 5v reference proces value) and the second input a pot-meter (10k) (setpoint). Error = SP - PV
So the code:
#include <arduino.h>
#include <PID_v1.h>
#define PIN_INPUT 0 //setpoint
#define PIN_INPUT 1 //input
#define PIN_OUTPUT 3 //pwm output
#define PIN_OUTPUT 8 // enable motor
#define PIN_OUTPUT 12 // direction
//Define Variables
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup()
{
//initialize the variables we're linked to
Input = analogRead(1) /4;
// value divide by 4 (1024 / 4 = 256 for pwm output
Setpoint = analogRead(0) /4;
digitalWrite(8, HIGH);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = analogRead(0) /4;
Setpoint = analogRead(1) /4;
myPID.Compute();
If (analogRead(0) / 4 =< analogRead(1) /4) {
digitalWrite(12, HIGH);
analogWrite(3, output)
}
Else if (analogRead(0) / 4 => analogRead(1) /4) {
digitalWrite(12, LOW);
analogWrite(3, OUTPUT)
}
Else
{
// do nothing
}
Delay(1000)
}
So the problem is the motor turns only one direction and if the input is almost at the setpoint both output light up (on the shield + en - led). I was thinking i should set the output limits because the arduino has a default of 0 》255
Thanks in advance