Hi
In my Arduino Project, I have programmed a PID controller. Purpose is to keep a reactor water pH similar to set point for around one hour (whole time of the testwork) .
pH modification of the water is done by using a peristaltic pump which can add some alkaline solution to increase the pH.
I have a question how can I record the procedure which the PID used to keep the pH at the set point, what I mean is I want to record the time that relay was ON and Off during the test. which then i can use that for all the other test to have a same condition.
please see below the code
#include <PID_v1.h>
#define PIN_INPUT 0
#define RELAY_PIN 6
//Define Variables we'll be connecting to
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);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 100;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = analogRead(PIN_INPUT);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
else digitalWrite(RELAY_PIN, LOW);
}
Can I save the time that relay was on in to a SD card and the time which was OFF from below code?
if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
else digitalWrite(RELAY_PIN, LOW);
}
Thanks
Alireza
It might make sense to keep track of the on/off state of the relay, and then record the on time, then off time, in milliseconds.
Also, you should verify the 'polarity' of your control. Right now, the larger Output is, the longer the relay pin stays LOW. If that isn't what is intended, the experiment will drive the pH to one extreme or the other.
boolean RelayOn = false;
if (Output < millis() - windowStartTime) {
digitalWrite(RELAY_PIN, HIGH);
if (!RelayOn) {
// Write On time here, comma but no end-of-line
RelayOn = true;
} // end if !RelayOn
} // end if Output
else {
digitalWrite(RELAY_PIN, LOW);
if (RelayOn) {
// Write Off time here, with End of line
RelayOn = false;
} // end if RelayOn
} // end else
AlirezaRabieh:
I have a question how can I record the procedure which the PID used to keep the pH at the set point, what I mean is I want to record the time that relay was ON and Off during the test. which then i can use that for all the other test to have a same condition.
I don't know if same pump times will get what you want but you can log relay ON and OFF times to SD and later read them back to replay the sequence.
To log you must open a log file then write lines to it.
Every line should start with a time. For playback, use the time differences not the absolute times.
With Arduino you have time functions micros() and millis() that return time since startup.
The micros() gives usecs to the nearest 4. The millis() gives msecs.
Both are subject to precision of the Arduino clock which may vary by temperature and the clock source itself to some small degree.
To be most accurate, use a temperature-corrected RTC (real time clock) module to pulse an Arduino pin and count those using an interrupt routine.
If this is what you want then develop on it further. The exactness of the timing will mean some difference between recording and playback, I am puzzled that you don't run the PID every time since conditions will never be quite the same either.
If what you really want is to save changed PID parameters, the on-chip EEPROM will let you store those through power-outs.