I'm trying to make a PID. But there are some relay issues. Please help

I'm trying to make a PID with many other logics along with that. But due to some reasons my relay is turning off and not turning on again. Please can u check the code and help me with it!!

#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <max6675.h>
#include <FlowSensor.h>
#include <FlowSensor_Type.h>

#define Relay_Pin1 9 // Relay Heater
#define Relay_Pin2 10 // Realy Pump
#define Relay_Pin3 11 // Relay Valve
const int Start_Pin = 12;
int button_state = 0;

float v1;
float setTemp1 = 40.00;

int thermoSCK = 2;
int thermoCS = 3;
int thermoSO = 4;
MAX6675 thermocouple1(thermoSCK, thermoCS, thermoSO);

uint8_t degree[8]  = {140,146,146,140,128,128,128,128};

const int Flow_sensor_Pin = 8;
float thresholdFlowRate = 0.0;
volatile int count;

LiquidCrystal_I2C lcd1(0x27, 16, 2); // LCD 1
LiquidCrystal_I2C lcd2(0x26, 16, 2); // LCD 2

void setup() {
  pinMode(Relay_Pin1, OUTPUT);
  pinMode(Start_Pin, INPUT_PULLUP); // Assuming Start_Pin is pulled up internally
  
  // Initialize the LCDs
  lcd1.begin();
  lcd2.begin();
  
  Serial.begin(9600);
  
  lcd1.backlight();
  lcd2.backlight();
  lcd1.createChar(0, degree); 
  delay(500);

  pinMode(Flow_sensor_Pin, INPUT);
  v1 = thermocouple1.readCelsius();
}

void loop() { 
{  if  (v1 < setTemp1) {
  digitalWrite(Relay_Pin1, HIGH);}
  else if  (v1 >= setTemp1) {
  digitalWrite(Relay_Pin1, LOW);}
  delay(2000);
}
  lcd1.clear();
  lcd1.setCursor(0, 0);
  lcd1.print("SV  OIL_HEAT  PV");
  lcd1.setCursor(0,1);
  lcd1.print("40.00C");
  lcd1.setCursor(9,1);
  lcd1.print(thermocouple1.readCelsius());
#if ARDUINO >= 100
  lcd1.write((byte)0);
#else
  lcd1.print(0, BYTE);
#endif
  lcd1.print('C');
  delay(1000);

}```

Try:

else if (v1 >= setTemp1 + 1.0) {

Your code only reads the thermocouple once at the end of setup. It never takes another reading. So that one reading is all you'll ever have. Since the number in v1 never changes the state of the relay never changes.

Add a line in loop to take another reading each time loop repeats.

1 Like

Thanks for ur suggestion. Sorted..

How would I know? You can check the datasheet as easily as I can.

1 Like

can anyone please explain this error.

 void FlowSensor::begin(void (*userFunc)(void), bool pullup = false)
      ^~~~~~~~~~
In file included from C:\Users\Kiran\Documents\Arduino\libraries\FlowSensor-Arduino-master\src\FlowSensor.cpp:13:0:
C:\Users\Kiran\Documents\Arduino\libraries\FlowSensor-Arduino-master\src\FlowSensor.h:38:7: error: candidate is: void FlowSensor::begin(void (*)())
  void begin(void (*userFunc)(void));
       ^~~~~
exit status 1
Error compiling for board Arduino Uno.

this program I'm working with

#include <FlowSensor_Type.h>


#include <max6675.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


const int thermo1CLK = 2; // MAX6675 CLK pin sensor1
const int thermo1CS = 3;  // MAX6675 CS (Chip Select) pin sensor1
const int thermo1DO = 4;  // MAX6675 DO (Data Out) pin sensor1
const int thermo2CLK = 5; // MAX6675 CLK pin sensor2
const int thermo2CS = 6;  // MAX6675 CS (Chip Select) pin sensor2
const int thermo2DO = 7;  // MAX6675 DO (Data Out) pin sensor2


const int relayPin1 = 9;   // Heater pin connected to the relay
const int relayPin2 = 10;  // Pump pin connected to the relay
const int relayPin3 = 11;  // solinoid valve pin connected to the relay

const int start_pin = 12;

MAX6675 thermocouple1(thermo1CLK, thermo1CS, thermo1DO);
const float TEMPERATURE_THRESHOLD = 40.0; // Adjust as needed

MAX6675 thermocouple2(thermo2CLK, thermo2CS, thermo2DO);

uint8_t degree[8]  = {140,146,146,140,128,128,128,128};

#define type YFS201
unsigned char Flow_sensor_Pin = 8;
volatile int pulseCount;  // Flow meter pulse counter
float flowrate = 450;  // Number of pulses per milliliter
float totalVolume = 0.0;  // Total volume accumulated
float setVolume = 1.0;  // Example set volume in liters


LiquidCrystal_I2C lcd1(0x27, 16, 2); // LCD 1
LiquidCrystal_I2C lcd2(0x26, 16, 2); // LCD 2

void pulseCounter() {
  pulseCount++;
}


void setup() {
  Serial.begin(9600);
  pinMode(relayPin1, OUTPUT);
  digitalWrite(relayPin1, LOW); // Initialize relay OFF
  pinMode(start_pin, INPUT_PULLUP); // Assuming Start_Pin is pulled up internally

  lcd1.begin();
  lcd2.begin();
  lcd1.backlight();
  lcd2.backlight();
  lcd1.createChar(0, degree); 
  delay(500);

  pinMode(Flow_sensor_Pin, INPUT);
  digitalWrite(Flow_sensor_Pin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(Flow_sensor_Pin), pulseCounter, FALLING);
  
  pulseCount = 0;
  lcd2.setCursor(0, 0);
  lcd2.print("Given Value:  1L");
  lcd2.setCursor(0, 1);
  lcd2.print("Present:");
}

void loop() {
  float temperature1 = thermocouple1.readCelsius();

  lcd1.clear();
  lcd1.setCursor(0, 0);
  lcd1.print("SV  OIL_HEAT  PV");
  lcd1.setCursor(0,1);
  lcd1.print("40.00C");
  lcd1.setCursor(9,1);
  lcd1.print(thermocouple1.readCelsius());
  #if ARDUINO >= 100
  lcd1.write((byte)0);
#else
  lcd1.print(0, BYTE);
#endif
lcd1.print('C');
delay(2500); 
  
  lcd1.clear();
  lcd1.setCursor(0, 0);
  lcd1.print("Body_Oil_Temp PV");
  lcd1.setCursor(9,1);
  lcd1.print(thermocouple2.readCelsius()); 
#if ARDUINO >= 100
  lcd1.write((byte)0);
#else
  lcd1.print(0, BYTE);
#endif
  lcd1.print('C');
delay(2500); // Adjust the delay as needed


  if (temperature1 > TEMPERATURE_THRESHOLD) {
    digitalWrite(relayPin1, HIGH); // Turn the relay ON
  } else {
    digitalWrite(relayPin1, LOW); // Turn the relay OFF
  }
  {
  detachInterrupt(digitalPinToInterrupt(Flow_sensor_Pin));
  {
    
    //flowrate = pulseCount / 450; // Replace 
     if (pulseCount >= 1) {  // Only increment volume if there are pulses
    flowrate += (pulseCount / flowrate);

  
  // Add current flow to total volume
  totalVolume = flowrate;

    // Display total volume on LCD
    lcd2.setCursor(10, 1);
    lcd2.print(totalVolume, 1);
    lcd2.print("L");
    pulseCount = 0;
  }

  // Compare total volume with set volume and display result
  if (totalVolume >= setVolume) {
  delay(100);
  }
  }
  attachInterrupt(digitalPinToInterrupt(Flow_sensor_Pin), pulseCounter, FALLING);
  }}```