cancel
Showing results for 
Search instead for 
Did you mean: 

Thermostat Function Problem

USolar
Associate III

Hello Everyone!

I have the problem with function below.

When I have a Hyst>0 function is working well.

Below Set Point, the function returns 0, and above SetPoint+Hyst the function returns 1.

Something similar as Thermostat is operating.

But when Hyst<0, the function do not work as planned to.

Above Set Point, the function returns 0, but when the Temperature just drops below Set Point, the function returns 1.

Idea was, that function returns 1 below SetPoint+Hyst (actually is SetPoint-Hyst, because Hyst<0).

When I put second part of function (Case Hyst<0) in the while loop all works fine.

I can't find where is my mistake hide.

Will be appreciate for any help

Iwan

uint8_t Thermostat(int Temperature, int SetPoint, int Hyst)
{
	uint8_t Output,Start,Stop;
	//+++++++++++++++++++++++++++++++++++++++++
	//+++++_Case_Hyst>0_+++++++++++++++++++++++
	if(Hyst>0)
	{
		if(Temperature>(SetPoint+Hyst))
		{	Start=1;
			Stop=0;
		}
		else if(Temperature<SetPoint)
		{	Start=0;
			Stop=1;
		}
	}
	//+++++++++++++++++++++++++++++++++++++++++
	//+++++_Case_Hyst<0_+++++++++++++++++++++++
	if(Hyst<0)
	{
		if(Temperature<(SetPoint+Hyst))
		{	Start=1;
			Stop=0;
		}
		else if(Temperature>SetPoint)
		{	Start=0;
			Stop=1;
		}
	}
	//+++++++++++++++++++++++++++++++++++++++++
	
	//+++++_Send_the_Result_+++++++++++++++++++
	Output=Start*(!Stop);
	return Output;
}

4 REPLIES 4
TDK
Guru

A negative hysteresis value makes no sense. How are you expecting it to behave in such a case?

You're not handling hysteresis here either, since you're not storing the previous value of temperature.

A proper hysteresis would be something like this:

uint8_t GetOutput(int value, int setpoint, int threshold) {
  static uint8_t output = 1;
  if (value >= set_point + threshold) {
    output = 1;
  }
  if (value < set_point) {
    output = 0;
  }
  return output ;
}

If you feel a post has answered your question, please click "Accept as Solution".
USolar
Associate III

0693W00000GX4qLQAT.pngTDK sorry, you are right it is not Hysteresis.

I mixed up with it.

It's just Thermostat with Set Point, which has Differential value for start and stop.

Depend on this Differential value, it should works in opposite ways.

I corrected my code, do not confusing with Hysteresis.

In attached picture, shown the principle how the Thermostat should work.

I implemented it in Function, but have problem with Differential<0. It's operating like differential is equal to 0.

When Differential>0, all works fine.

I know, it's looks like simple task, but I stuck with it and cannot find the mistake.

uint8_t Thermostat(int Temperature, int SetPoint, int Diff)
{
	uint8_t Output,Start,Stop;
	//+++++++++++++++++++++++++++++++++++++++++
	//+++++_Case_Diff>0_+++++++++++++++++++++++
	if(Diff>0)
	{
		if(Temperature>(SetPoint+Diff))
		{	Start=1;
			Stop=0;
		}
		else if(Temperature<SetPoint)
		{	Start=0;
			Stop=1;
		}
	}
	//+++++++++++++++++++++++++++++++++++++++++
	//+++++_Case_Diff<0_+++++++++++++++++++++++
	if(Diff<0)
	{
		if(Temperature<(SetPoint+Diff))
		{	Start=1;
			Stop=0;
		}
		else if(Temperature>SetPoint)
		{	Start=0;
			Stop=1;
		}
	}
	//+++++++++++++++++++++++++++++++++++++++++
 
	//+++++_Send_the_Result_+++++++++++++++++++
	Output=Start*(!Stop);
	return Output;
}

TDK
Guru

You need to store the current start/stop values in a static variable and only change the values when it exceeds the higher or lower threshold.

If you feel a post has answered your question, please click "Accept as Solution".
USolar
Associate III

TDK thank you for advices.

I found another solution how to store previous start/stop values.

I add one more Function parameter, the previous Output value.

By that value I can determine the start/stop value.

It works as should to.

uint8_t Thermostat(int Temperature, int SetPoint, int Diff,uint8_t Prev_Output)
{
	uint8_t Output;
	static uint8_t Start=1;
	static uint8_t Stop=0;
	if(!Prev_Output)
	{	Start=0;
		Stop=1;
	}
	//+++++++++++++++++++++++++++++++++++++++++
	//+++++_Case_Diff>0_+++++++++++++++++++++++
	if(Diff>0)
	{
		if(Temperature>(SetPoint+Diff))
		{	Start=1;
			Stop=0;
		}
		else if(Temperature<SetPoint)
		{	Start=0;
			Stop=1;
		}
	}
	//+++++++++++++++++++++++++++++++++++++++++
	//+++++_Case_Diff<0_+++++++++++++++++++++++
	if(Diff<0)
	{
		if(Temperature<(SetPoint+Diff))
		{	Start=1;
			Stop=0;
		}
		else if(Temperature>SetPoint)
		{	Start=0;
			Stop=1;
		}
	}
	//+++++++++++++++++++++++++++++++++++++++++
 
	//+++++_Send_the_Result_+++++++++++++++++++
	Output=Start&&(!Stop);
	return Output;
}