Thanks very much for all the replies!
I have tried Steve's recommendation and it seems to get rid of the initial issue with the shifting of window at unexpected times. Great!
However I am facing another issue that I didn't have before.
For the record, pump and flow through heater are switched off, so the output should be 100 at all times, however I get outputs of 45.02 at random times and looking at the actual relay status, the window does not seem to adjust properly either.
I am starting to lose my mind here :D
My current code:
```cpp
#include <FlowSensor.h>
#include <LcdBarGraph.h>
#include <LiquidCrystal.h>
//Internal NTC Stuff
const long int RT0 = 50000; // Ω
const int B = 3970; // K
const int VCC = 5; //Supply voltage
const long int R = 12000; //R=10KΩ
//Variables
float RT, VR, ln, TX, T0, VRT;
//Thermocouple Stuff
#include <SPI.h>
#include "Adafruit_MAX31855.h"
// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO_1 31
#define MAXCS_1 30
#define MAXCLK_1 29
// initialize the Thermocouple
Adafruit_MAX31855 thermocouple_1(MAXCLK_1, MAXCS_1, MAXDO_1);
#define MAXDO_2 26
#define MAXCS_2 27
#define MAXCLK_2 28
// initialize the Thermocouple
Adafruit_MAX31855 thermocouple_2(MAXCLK_2, MAXCS_2, MAXDO_2);
//PID Stuff
#include <PID_v1.h>
#define RELAY_PIN 19
#define RELAY_ON HIGH
#define RELAY_OFF LOW
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp = 30, Ki = 10, Kd = 100;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
//WindowSize DON'T CHANGE FOR NOW!!!
int WindowSize = 100;
unsigned long windowStartTime;
//LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte LCD_Columns = 20;
byte LCD_Rows = 4;
//Flow Sensor
FlowSensor Sensor(YFS201, 18);
unsigned long timebefore = 0; // same type as millis(), is not being used again in this sketch
const int Pump = 10;
int variables[2][2] = { { 0, 1 }, { 2, 200 } }; // I want to link to the array in the array that I setup in the Menu file
void count() {
Sensor.count();
}
void setup() {
Serial.begin(2000000);
//PID Stuff
//windowStartTime = millis();
windowStartTime = 0;
//initialize the variables we're linked to
Setpoint = 70; //Temperature is the Setpoint
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
myPID.SetSampleTime(WindowSize / 10); // make sure we measure and sample at least 10 times per WindowSize.
//turn the PID on
myPID.SetMode(AUTOMATIC);
//Thermocouple
thermocouple_1.begin();
thermocouple_2.begin();
//NTC
T0 = 25 + 273.15;
//Flowmeter
Sensor.begin(count);
//Flow Through Heater
pinMode(RELAY_PIN, OUTPUT);
//Pump
pinMode(Pump, OUTPUT);
//LCD Begin and Test
lcd.begin(20, 4);
lcd.print("Test");
delay(1000);
lcd.clear();
}
void loop() {
// Pump and show progress on LCD until the desired volume has been reached
LcdBarGraph lbg0(&lcd, 19, 1, 2);
for (variables[0][0]; variables[0][0] < variables[1][1];) {
//for (millis(); millis() < 10000;) {
lcd.setCursor(7, 1);
lcd.print("PROGRESS");
lbg0.drawValue(variables[0][0], variables[1][1]);
//delay(10);
digitalWrite(Pump, HIGH);
Sensor.read();
variables[0][0] = Sensor.getVolume() * 1000;
Serial.print("Volume: ");
Serial.print(variables[0][0]);
Serial.print("ml --- ");
//Temperature Stuff
VRT = analogRead(A15); //Acquisition analog value of VRT
VRT = (5.00 / 1023.00) * VRT; //Conversion to voltage
VR = VCC - VRT;
RT = VRT / (VR / R); //Resistance of RT
ln = log(RT / RT0);
TX = (1 / ((ln / B) + (1 / T0))); //Temperature from thermistor
TX = TX - 273.15; //Conversion to Celsius
Serial.print("NTC Temperature: ");
Serial.print(TX);
Serial.print(" degC --- ");
//Thermocouple
//Serial.print("Internal Temp 2 = ");
//Serial.print(thermocouple_2.readInternal());
double d = thermocouple_2.readCelsius();
Serial.print("FTH Control Temp. = ");
Serial.print(d);
Serial.print(" degC");
//Serial.print(" --- Internal Temp 1 = ");
//Serial.print(thermocouple_1.readInternal());
Input = TX;
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (windowStartTime == 0) windowStartTime = millis();
if (millis() - windowStartTime > WindowSize) { //time to shift the Relay Window
windowStartTime += WindowSize;
Serial.println("");
Serial.print("Relay Window shifted");
//Serial.print(" --- VALUES: Input: ");
//Serial.print(Input);
Serial.print(" --- Setpoint: ");
Serial.print(Setpoint);
Serial.print(" --- Output: ");
Serial.print(Output);
}
if (Output > millis() - windowStartTime) {
digitalWrite(RELAY_PIN, RELAY_ON);
Serial.print(" --- Relay ON ");
Serial.print(" --- Time: ");
Serial.print(millis());
Serial.println("ms");
} else {
digitalWrite(RELAY_PIN, RELAY_OFF);
Serial.print(" --- Relay OFF");
Serial.print(" --- Time: ");
Serial.print(millis());
Serial.println("ms");
}
}
digitalWrite(RELAY_PIN, RELAY_OFF);
digitalWrite(Pump, RELAY_OFF);
lcd.clear();
lcd.setCursor(8, 1);
lcd.print("DONE");
while (1) {}
}
```
And parts of what I get (everything behind "Time: XXX" in each line has been added by me in the email)
09:55:29.980 -> Relay Window shifted --- Setpoint: 70.00 --- Output: 45.02 --- Relay ON --- Time: 9742ms
09:55:29.980 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9747ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9752ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9758ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9763ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9769ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9775ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9780ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9786ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9791ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9796ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9802ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9807ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9812ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9819ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9824ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9830ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9835ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC
09:55:29.391 -> Relay Window shifted --- Setpoint: 70.00 --- Output: 100.00 --- Relay ON --- Time: 9143ms
09:55:29.391 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9148ms
09:55:29.391 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9153ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9159ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9164ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9169ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9176ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9181ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9186ms <--- OFF even though Output = 100
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9192ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9197ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9203ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9208ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9213ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9220ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9225ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9230ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9236ms <--- OFF even though Output = 100
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC
09:55:29.489 -> Relay Window shifted --- Setpoint: 70.00 --- Output: 100.00 --- Relay ON --- Time: 9242ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9248ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9253ms
09:55:29.522 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9259ms
09:55:29.554 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON --- Time: 9308ms