Help on a Wireless Temperature Controlled Relay

I'm an engineering student but unfortunately I know nothing about code besides basic if and else statements that they teach in C introduction. Despite that I was able to put a hodge podge of code together and get it to do what I wanted, except one part.

I've got the temperature relay to give a reading. I figured out how to get it displayed on a LCD. Then I figured out how to get the relay working. All which were thankfully easy. I somehow figured out how to communicate through serial with the arduino so when a certain number or phrase is typed in, it takes the number converts it to an int which I can then use to control the relay. If the temp is 20 and I put 19 it says cooling. If i put 21 it says heating. The problem is that when the temperature rises or lowers to match that temperature the relay does not switch to fix the temp.

//read value from serial
 while (Serial.available() > 0) {
    int inChar = Serial.read();
    
    if (isDigit(inChar)) {
      // convert the incoming byte to a char and add it to the string:
      inString += (char)inChar;
    }
   
   // if you get a newline, print the string, then the string's value:
    if (inChar == '\n') {
      Serial.print("New Temp:");
      Serial.println(inString.toInt());
      //Serial.print("String: ");
      //Serial.println(inString);
      // clear the string for new input:

NewTemp = inString.toInt();
Serial.println(NewTemp);
      inString = "";
    }
    
   if (fixtemp > NewTemp){
    digitalWrite(pinOut1, HIGH);
    digitalWrite(pinOut2, HIGH);
  lcd.setCursor(0, 2);
      lcd.print("COOLING");
  
  }
  else if (fixtemp < NewTemp){
    digitalWrite(pinOut2, HIGH);
    digitalWrite(pinOut1, LOW);

      lcd.setCursor(0, 3);
      lcd.print("HEATING");
      lcd.setCursor(0, 2);
  }

I don't know if I'm just not seeing something but I thought with the way it's written, the relays would be able to actuate by themselves. As it is now the relays only actuate when a temperature is entered through the serial monitor and does not activate to counteract if the temperature is going above or below the temperature indicated through serial. Hope that makes sense.

I'm using an Arduino Uno, KY-028 NTC Temperature sensor, Sim900 and a regular 5v operated 2 channel relay.

Entirety of Code:

#include <Wire.h>

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String textMessage;

int sensorPin = A0; // select the input pin for the potentiometer

int sensorValue = 0; // variable to store the value coming from the sensor

int pinOut1 = 10;
int pinOut2 = 11;

float i = 0;
float fixtemp = 20.;
int fixInput = 560;
float fixedDegreeValue = 5.5;


int incomingByte = 0;
String inString = ""; 

int inChar = "";
int NewTemp;

 
void setup () 
{
 

  Serial.begin (9600);
  SIM900.begin(9600);

 pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  lcd.begin(16,2);
  lcd.backlight();
  
  // Give time to your GSM shield log on to network
  delay(9000);
  Serial.print("SIM900 ready...");

  // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
 
}
 
void loop () 
{
lcd.setCursor(0, 0);
  lcd.print("TEMP CONTROL");


 sensorValue = analogRead(sensorPin);  
  if (sensorValue > fixInput ){ // ice cube
   // Serial.print(sensorValue);
    
    i = sensorValue - fixInput;
    i = i /fixedDegreeValue; 
    //Serial.print(sensorValue);
   // Serial.print(" and i = ");
   // Serial.println(i);
    Serial.print("Temperature = ");
    fixtemp = fixtemp - i;
    fixtemp = fixtemp - 71;
    Serial.print(fixtemp);
    Serial.println(" C");
  }  
  else if(sensorValue < fixInput) //thea cup
  {
     i =  fixInput - sensorValue;
    i = i /fixedDegreeValue; 
   // Serial.print(sensorValue);
   // Serial.print(" and i = ");
   // Serial.println(i);
    Serial.print("Temperature = ");
    fixtemp = fixtemp + i;
    fixtemp = fixtemp - 71;
    Serial.print(fixtemp);
    Serial.println(" C");
  }


 

  /*if(SIM900.available()>0){
    textMessage = SIM900.readString();
    Serial.print(textMessage);    
    delay(10);
  }*/
   
   //read value from serial
 while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string, then the string's value:
    if (inChar == '\n') {
      Serial.print("New Temp:");
      Serial.println(inString.toInt());
      //Serial.print("String: ");
      //Serial.println(inString);
      // clear the string for new input:
NewTemp = inString.toInt();
Serial.println(NewTemp);
      inString = "";
    }
    
   if (fixtemp > NewTemp){
    digitalWrite(pinOut1, HIGH);
    digitalWrite(pinOut2, HIGH);
  lcd.setCursor(0, 2);
      lcd.print("COOLING");
  
  }
  else if (fixtemp < NewTemp){
    digitalWrite(pinOut2, HIGH);
    digitalWrite(pinOut1, LOW);

    
  
  lcd.setCursor(0, 3);
      lcd.print("HEATING");
      lcd.setCursor(0, 2);
  } 

else if(inString = "OFF")
{
digitalWrite(pinOut1, LOW);
digitalWrite(pinOut2, LOW);

lcd.setCursor(0, 3);
 lcd.print("OFF");
 lcd.setCursor(0, 2);
}
//textMessage.indexOf("OFF")>=0
   
else { 
  digitalWrite (pinOut2, HIGH);
 digitalWrite(pinOut1, LOW);
lcd.setCursor(0, 3);
 lcd.print("SETTLED");
 lcd.setCursor(0, 2);
} 
 }
  fixtemp = 20;
  delay(5000);
}

There might be some unnecessary bits in there.

Can you explain the handling/use of the variable "fixtemp".
It seems to be manipulated and tested intensively in the loop() ?

. . . 

    fixtemp = fixtemp - i;
    fixtemp = fixtemp - 71;
. . .
    if (fixtemp > NewTemp) { . . .
. . .
     fixtemp = 20;

For someone who "knows nothing about code" you've put together an impressive system integrating GSM connectivity over serial, an LCD display and a temperature sensor.

6v6gt:
Can you explain the handling/use of the variable "fixtemp".
It seems to be manipulated and tested intensively in the loop() ?

. . . 

fixtemp = fixtemp - i;
   fixtemp = fixtemp - 71;
. . .
   if (fixtemp > NewTemp) { . . .
. . .
    fixtemp = 20;





For someone who "knows nothing about code" you've put together an impressive system integrating GSM connectivity over serial, an LCD display and a temperature sensor.

The fixed temp is being used as the temperature to my knowledge. The only thing I can explain it as is a series of calculations using it's correlation between resistance and temperature to arrive at what seems to be a temperature value. In terms of why the person did it the way they did it, I am not sure. I got help on that part of the code from a video I watched on the KY-028 temperature sensor. Was the only helpful resource I could find.

I say I know nothing because if it wasn't for how comprehensive the arduino programming community is I would have never reached this far. It makes piecing together a code for a project very possible even for a newbie. Hopefully I can get over this final road block. Any help or suggestions really needed at this point.

I actually solved the problem in a way that I have no idea how to explain.

while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string, then the string's value:
    if (inChar == '\n') {
      Serial.print("New Temp:");
      Serial.println(inString.toInt());
      //Serial.print("String: ");
      //Serial.println(inString);
      // clear the string for new input:
NewTemp = inString.toInt();
Serial.println(NewTemp);
      inString = "";
    }
    
   if (fixtemp > NewTemp and NewTemp >0){
    digitalWrite(pinOut1, HIGH);
    digitalWrite(pinOut2, HIGH);
  lcd.setCursor(0, 2);
      lcd.print("COOLING");
  
  }
  else if (fixtemp < NewTemp and NewTemp >0){
    digitalWrite(pinOut2, HIGH);
    digitalWrite(pinOut1, LOW);

    
  
  lcd.setCursor(0, 3);
      lcd.print("HEATING");
      lcd.setCursor(0, 2);
  } 

else if(NewTemp == 0)
{
digitalWrite(pinOut1, LOW);
digitalWrite(pinOut2, LOW);

lcd.setCursor(0, 3);
 lcd.print("OFF");
 lcd.setCursor(0, 2);
}
//textMessage.indexOf("OFF")>=0
   
else { 
  digitalWrite (pinOut2, HIGH);
 digitalWrite(pinOut1, LOW);
lcd.setCursor(0, 3);
 lcd.print("SETTLED");
 lcd.setCursor(0, 2);
} 
 }
 if (fixtemp > NewTemp and NewTemp >0){
    digitalWrite(pinOut1, HIGH);
    digitalWrite(pinOut2, HIGH);
  lcd.setCursor(0, 2);
      lcd.print("COOLING");
 }
else if (fixtemp < NewTemp and NewTemp>0){
    digitalWrite(pinOut2, HIGH);
    digitalWrite(pinOut1, LOW);

    
  
  lcd.setCursor(0, 3);
      lcd.print("HEATING");
      lcd.setCursor(0, 2);
  } 
  
else if (NewTemp == 0) {  
  digitalWrite (pinOut2, LOW);
 digitalWrite(pinOut1, LOW);
lcd.setCursor(0, 3);
 lcd.print("OFF");
 lcd.setCursor(0, 2); 
}

By putting a copy of the code for the If, else if statements for relay control outside the while loop brackets.

I also made a couple changes to introduce an OFF state when the value entered through serial is 0. When the device is turned on it defaults to that 0 and shows "Temp Control OFF" on the lcd I assume because there is no value input yet into the serial monitor.

My only problem is that Say if it is Heating or Cooling and I put 0 to turn it off, it does turn it off and puts "OFF", but also puts the ending of the last word that was there. So it will either look like "OFFLING" or "OFFTING". Is there a way to introduce a part of code that clears the LCD so that this does not occur?