Alarm for negative temperature values

Hy,

I'm new in programming with the Arduino and I hope you can help.

I have a Arduino Mega 2560 + Ethernet Shield, connected with an DS18S20 temperature sensor, an LCD-Display and an piezo buzzer for my alarm.
Displaying the temperature (positive and negative values) on the LCD-Display works, also get the time by an ntp-server.
I also get a alarm tone with the piezo buzzer if the temperature is over or under a value I set before, but only if I have positive values for temperature in my 'if-instruction'.

My problem is, If I set negative temperature values in my 'if-instruction' to get an alarm it doesn't work!

This is the function for read the temperature and with the 'if-instruction'.

void readTemperature(int Interval)
{
  int HighByte, LowByte, TReading, SignBit, Tc_100, Temp, Fract;
  char buf[20];
 
  byte d, sensor;
  byte present = 0;
  byte data[12];
 
  for (sensor=0; sensor<MAX_DS1820_SENSORS; sensor++)
  {
    if (OneWire::crc8( addr[sensor], 7) != addr[sensor][7])
    {
      lcd.setCursor(0,0);
      lcd.print("CRC is not valid");
      return;
    }
 
    if ( addr[sensor][0] != 0x10)   
    {
      lcd.setCursor(0,0);
      lcd.print("Wrong Sensor");
      return;
    }
 
    ds.reset();
    ds.select(addr[sensor]);
    ds.write(0x44,1);         // start conversion, with parasite power on at the end
 
    delay(1000);
 
    present = ds.reset();
    ds.select(addr[sensor]);    
    ds.write(0xBE);          // Read Scratchpad
 
    for (d = 0; d < 9; d++)
    {
      data[d] = ds.read();
    }
 
    LowByte = data[0];
    HighByte = data[1];
    TReading = (HighByte << 8) + LowByte;
    SignBit = TReading & 0x8000;
    if (SignBit) // negative
    {
      TReading = (TReading ^ 0xffff) + 1; 
    }
    
    Tc_100 = (TReading*100/2);    
 
    Temp = Tc_100 / 100;
    Fract = Tc_100 % 100;
 
    sprintf(buf, "%c%d.%d\337C ",SignBit ? '-' : '+', Temp, Fract < 10 ? 0 : Fract);
 
    lcd.setCursor(0,0);    
    lcd.print(Sensorname[sensor]);   
    lcd.setCursor(8,0);
    lcd.print(buf);                  
 


    if(Temp <= -30)
    {
      lcd.setCursor(0,0);
      lcd.print(" WARNUNG: Temp. ");
      lcd.setCursor(0,1);
      lcd.print("To low!");
      tone (Buzzer, 700);
      delay (1000);
      tone (Buzzer, 350);
      delay (1000);
      tone (Buzzer, 700);
      delay (1000);
      tone (Buzzer, 350);
      delay (1000);
      tone (Buzzer, 700);
      delay (1000);
      tone (Buzzer, 350);
      delay (1000);  
      tone (Buzzer, 700);
      delay (1000);
      noTone (Buzzer);
      lcd.clear();    
    }
    
    else if(Temp > -30 && Temp < -10)
    {
      // All okay --> no Alarm!
    }
    

     if(Temp >= -10)
    {
      lcd.setCursor(0,0);
      lcd.print(" WARNUNG: Temp. ");
      lcd.setCursor(0,1);
      lcd.print("  To high!  ");
      tone (Buzzer, 700);
      delay (1000);
      tone (Buzzer, 350);
      delay (1000);
      tone (Buzzer, 700);
      delay (1000);
      tone (Buzzer, 350);
      delay (1000);
      tone (Buzzer, 700);
      delay (1000);
      tone (Buzzer, 350);
      delay (1000);  
      tone (Buzzer, 700);
      delay (1000);
      noTone(Buzzer);
      lcd.clear();
    } 
   
    if(DEBUG_MODE == true) Serial.println(Temp);
 
    delay(Interval);
  }
}

Hi, Im new to arduino myself but I do have a similar setup to you. You could try putting your temperature values (ie. -10,-30) into an array and try it that way. something like this;

int TempArray[3] = {-10,-30}  // TempArray[0] = -10, TempArray[1] = -30, TempArray[2] = null
if (Temp <= TempArray[1])   // TempArray[1] = -30 
{
blah, blah, blah
}

I was taught to not use numbers in my code and to basically define everything at the beginning and create arrays to help simplify things. Using arrays also helps you find change those values in 1 place rather than several.

Thank you for your answer:)
I changed my 'if-instruction' to this:

if((Temp >= 30) && SignBit)
{ code }

if(((Temp <= 10) && SignBit) |! (SignBit ))
{ code }

and now it works also :wink: