I have some example / set i used in the past to play around, contains some fun info to use.
Instead of pwm, i want to change the output to stepper.
The balance arm has a fan blowing downwards, pivoting on a potmeter ( the feedback signal)
The other potmeter is the setpoint.
I want to replace the feedback with the output O2 sensor set. and the setpoint still use a potmeter to control the setting i want it to be.
The output signal , now pwm, i want to change into stepper.
I have few stepper valves and more steppers.
I think i still have one valve that runs on a pwm output.
have it in my junk corner the last 7 years…
The arduino program used is based on PID control
fetching the code…
PID control:
unsigned long lastTime;
double Feedback, Output, Setpoint;
double errSum, lastErr;
double ITerm, lastInput;
double kp, ki, kd;
int count_time;
#define PIN_OUTPUT 3 //เอาต์พุต PWM ขับมอเตอร์
void setup()
{
Serial.begin(19200);
}
void loop()
{
count_time++;
if(count_time<=150)Setpoint = 500;
if(count_time>150)Setpoint = 600;
if(count_time>300)count_time = 0;
//อ่านค่าจากตัวต้านทานปรับค่าได้เพื่อบอกถึงตำแหน่งที่ต้องการ
//Setpoint = analogRead(A5);
//อ่านค่าจากเซนเซอร์ตรวจจับตำแหน่ง
Feedback = analogRead(A4);
SetTunings(5.0,0.001,100);// set Kp =2.5
Compute();// Calculate PID Control
Plot();
}
void Compute()
{
/How long since we last calculated/
unsigned long now = millis();
double timeChange = (double)(now - lastTime);
/Compute all the working error variables/
double error = Setpoint - Feedback;
errSum += (error * timeChange);
ITerm = ki * errSum;
if(ITerm<-255)errSum = (ITerm/ki)-(error * timeChange);
if(ITerm>255)errSum = (ITerm/ki)-(error * timeChange);
double dInput = (error - lastErr);
/Compute PI Output/
Output = kp * error + ITerm + kd * dInput;
if(Output<0)Output = 0;
if(Output>255)Output = 255;
analogWrite(PIN_OUTPUT, Output);
/*Remember some variables for next time*/
lastErr = error;
lastTime = now;
}
void SetTunings(double Kp, double Ki, double Kd)
{
kp = Kp;
ki = Ki;
kd = Kd;
}
void Plot()
{
// แสดงผลกราฟ
Serial.print("\n");
Serial.print((Setpoint/1023)*100);
Serial.print("\t");
Serial.print((Feedback/1023)*100);
Serial.print("\t");
Serial.print((Output/255)*10);
Serial.print("\t");
Serial.print( 100);
Serial.print("\t");
Serial.print( 10);
Serial.print("\t");
Serial.print( 0);
}
And the code with UseLib
#include <PID_v1.h>// เรียกใช้งานไลเบอรี่PID
#define PIN_OUTPUT 3 //เอาต์พุต PWM ขับมอเตอร์
double Setpoint, Feedback, Output;
int count_time;
//จูนระบบด้วยการปรับค่าเกน Kp และ Ki
double Kp=8, Ki=1, Kd=0.15; // กำหนดค่าเกนทั้งสามตัว
PID myPID(&Feedback, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);//เปิดการใช้งาน PID
void setup()
{
Setpoint = analogRead(A5);//อ่านค่าทั้งต้องการ
Feedback = analogRead(A4);//อ่านค่าจากตัวตรวจจับตำแหน่ง
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1);
myPID.SetOutputLimits(0, 255);
Serial.begin(19200);
}
void loop()
{
count_time++;
if(count_time<=150)Setpoint = 500;
if(count_time>150)Setpoint = 600;
if(count_time>300)count_time = 0;
//Setpoint = analogRead(A5);//อ่านค่าจากตัวต้านทานปรับค่าได้เพื่อบอกถึงตำแหน่งที่ต้องการ
Feedback = analogRead(A4);//อ่านค่าจากเซนเซอร์ตรวจจับตำแหน่ง
myPID.Compute(); //สั่งให้ตัวPID เริ่มคำนวณ
analogWrite(PIN_OUTPUT, Output);//ส่งเอาต์พุต PWM ขับมอเตอร์
// แสดงผลกราฟ
Serial.print("\n");
Serial.print((Setpoint/1023)*100);
Serial.print("\t");
Serial.print((Feedback/1023)*100);
Serial.print("\t");
Serial.print((Output/255)*10);
Serial.print("\t");
Serial.print( 100);
Serial.print("\t");
Serial.print( 10);
Serial.print("\t");
Serial.print( 0);
}
Any idea how to change to stepper output ?