if function issue

You have some "if(stock >= t)" statements, they should probably have been the other way around:

if (temperature >= threshold)
{
  turnFanOn();
}
else
{
  turnFanOff();
}