Slave loop not working unless connected to master

Hi,

I have two arduinos, one is a slave and one is a master. I am using a master to set a target temperature and slave a slave to maintain that temperature value. Right now the loop of the slave will not run unless it is connected to the master. As soon as serial connection is broken, the loop stops and nothing happens. How can i get the loop of the slave to continue regardless of the master being connected or not?

I think it has to do with the while function but with my skills am not able to figure it out. Ive pasted the code to the slave (which needs to loop no matter what) i dont think the master is relevent in this problem but I can post that code if needed.

Thank you for your help and time!

#include<Wire.h>
int temperatureWrite; //tempeature to set
int thermister = 0;
const int pwm = 5;

//Voltage divider 
int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 100;
float R2 = 0; //calculate this
float temperatureRead; //solve for this y=mx+b
float buffer= 0;

//PID 
float PID_error;
float previous_error;
float Totaltime, Time, Previoustime;
int PID_value = 0;

int P = 20.0; int PID_P = 0;
int I = 0.3; int PID_I = 0;
int D = 2.0; int PID_D = 0;


void setup()
{
Serial.begin(9600);
pinMode(pwm, OUTPUT);
Wire.begin(69);                
Wire.onReceive(receiveEvent);
}

void loop()
 //ohm calculation
 {
raw= analogRead(thermister);
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
 }
 
//master serial data
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
    char temperatureWrite = Wire.read(); // receive byte as a character
    Serial.print(temperatureWrite);   
 }
      
int temperatureWrite = Wire.read();    
  temperatureWrite = temperatureWrite * 25;
  


//y=5(x)-210 
temperatureRead = (5 * R2) - 210;
//Calc P&I
PID_error = temperatureRead - temperatureWrite;
PID_P = P * PID_error;
if(-10 < PID_error <10)
{
  PID_I = PID_I + (I * PID_error);
}
//Calculate D
Previoustime = Time;
Time = millis();
Totaltime = (Time - Previoustime)/1000;
PID_D = D*((PID_error - previous_error)/Totaltime);
PID_value = PID_P + PID_I + PID_D;

//auto adjust
if(PID_value < 0) {
 PID_value = 0; 
 }
if(PID_value > 255) {
  PID_value = 255;
}

analogWrite(pwm, 255-PID_value);
previous_error = PID_error;

Serial.println("----");
Serial.print("Vout: "); 
Serial.print(Vout);
Serial.println("");
Serial.print("Ohms: ");
Serial.print(R2);
Serial.println("");
Serial.print("-");
Serial.println("");
Serial.print("Calculated Temperature: ");
Serial.print(temperatureRead);
Serial.println("");
Serial.print("Target Temperature: ");
Serial.print(temperatureWrite);
Serial.println("");
Serial.print("-");
Serial.println("");
Serial.print("PWM Value: ");
Serial.print(255 - PID_value);
Serial.println("");
Serial.println(thermister);
Serial.println("");
Serial.println("----");
delay(1000);

}

Never use while, always use if.

void loop ()
{
  if (Serial.available () > 0)
    process_char (Serial.read ()) ;
  ... do other stuff ...
}

For this to work the process_char function needs to maintain its own state in between calls, but it won't block anything.

Thanks for you reply!

Unfortunatley that did not work and its still doing the same thing. I have tried if as well as else and added the entire loop as the both if and else. This is what i have so far, it still only loops when there is connection to the master. I should also note that it’s also completly ignoring my delay so something is being wonky.

#include<Wire.h>
int temperatureWrite; //tempeature to set
int thermister = 0;
const int pwm = 5;

//Voltage divider 
int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 100;
float R2 = 0; //calculate this
float temperatureRead; //solve for this y=mx+b
float buffer= 0;

//PID controller stuff
float PID_error;
float previous_error;
float Totaltime, Time, Previoustime;
int PID_value = 0;

int P = 20.0; int PID_P = 0;
int I = 0.3; int PID_I = 0;
int D = 2.0; int PID_D = 0;


void setup()
{
Serial.begin(9600);
pinMode(pwm, OUTPUT);
Wire.begin(69);                
Wire.onReceive(receiveEvent); 
}

void loop()
 //ohm calculation
 {
raw= analogRead(thermister);
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
 }
 
//master serial data
void receiveEvent(int howMany) 
{
  if (Wire.available () > 0) { 
    char tempeatureWrite ( Wire.read() ); // THIS IS WHERE THE ISSUE IS (i think)
    Serial.print(temperatureWrite);   
  }
  else {
  int temperatureWrite = Wire.read();    
  temperatureWrite = temperatureWrite * 25;

   


//y=5(x)-210 
temperatureRead = (5 * R2) - 210;
//Calc P&I
PID_error = temperatureRead - temperatureWrite;
PID_P = P * PID_error;
if(-10 < PID_error <10)
{
  PID_I = PID_I + (I * PID_error);
}
//Calculate D
Previoustime = Time;
Time = millis();
Totaltime = (Time - Previoustime)/1000;
PID_D = D*((PID_error - previous_error)/Totaltime);
PID_value = PID_P + PID_I + PID_D;

//auto adjust
if(PID_value < 0) {
 PID_value = 0; 
 }
if(PID_value > 255) {
  PID_value = 255;
}

analogWrite(pwm, 255-PID_value);
previous_error = PID_error;

Serial.println("----");
Serial.print("Vout: "); 
Serial.print(Vout);
Serial.println("");
Serial.print("Ohms: ");
Serial.print(R2);
Serial.println("");
Serial.print("-");
Serial.println("");
Serial.print("Calculated Temperature: ");
Serial.print(temperatureRead);
Serial.println("");
Serial.print("Target Temperature: ");
Serial.print(temperatureWrite);
Serial.println("");
Serial.print("-");
Serial.println("");
Serial.print("PWM Value: ");
Serial.print(255 - PID_value);
Serial.println("");
Serial.println(thermister);
Serial.print("Master Sensor Data: ");
Serial.print(temperatureWrite);
Serial.println("");
Serial.println("----");
delay(0);
  }
}

Bump