Help . Activating the servo affects my temperature sensor reading.

This is part of my program . My temperature sensor is reading the temperature every sec , at room temperature
Everytime when timer reaches maxsec , my servo will activate. After activating the servo , my temperature reading will jump
from room temperature to 50-100 degree . i am to code the program to that if the room temperature > 35 deg , the servo will
open twice as long , but could not code it due to the high room temperature caused by servo. Is there anyway to stop the servo from
affecting the room temperature?

temp_in_kelvin = analogRead(0) * 0.004882812 * 100;
temp_in_celsius = temp_in_kelvin - 2.5 - 273.15;

 now = millis();
   if(now - then > interval)
   {
      sec = sec + 1;
      then = now;
  Serial.print(hour);
  Serial.print("Hour ");
  Serial.print(minute);
  Serial.print("Minute ");
  Serial.print(sec);
  Serial.print("Sec ");
  Serial.print("Celsius: ");
  Serial.println(temp_in_celsius);

 if (digitalRead(testMode) == HIGH  && digitalRead(inputPin1) == HIGH){   // Default Mode 48 hour.      
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    maxsec = 5;
    }
    
    
  
 else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == HIGH){    //24 Hour
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    maxsec = 10;
   }
   
   
   
 else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == LOW){     // 12 Hour  
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    maxsec = 15;
   }

  if (sec > maxsec) {
    sec = 0;
    minute = minute + 1;
    
     for(pos = 0; pos < 180; pos += 1)  
  {                                
    myservo.write(pos);               
    delay(5); 
  }
  for(pos = 180; pos>=1; pos-=1)    
  {                                
    myservo.write(pos);               
    delay(667);                       
  }

Are you using a seperate power supply for the servo?

That code you posted will not compile. Try again!

zoomkat:
Are you using a seperate power supply for the servo?

i am using the same 5v for everything , and that's probably why it's affecting my reading i suppose ? is there anyway to use the same supply without
affecting the reading?

PaulS:
That code you posted will not compile. Try again!

Hi Paul, That's just part of my program which i cut out . The full program is able to compile and work properly ,
except for the reading of temperature which fluctuates

Powering a servo directly from an arduino will probably cause significant swings in the voltage on the arduino, which would affect the voltage to/from the sensor, and possibly impact the arduino on board analog reference voltage.

Hi Paul, That's just part of my program which i cut out . The full program is able to compile and work properly, except for the reading of temperature which fluctuates

I'd like to have a dollar for every time someone posts a snippet of code where they are sure the problem is, when it turns out that it was somewhere else entirely.

PaulS:

Hi Paul, That's just part of my program which i cut out . The full program is able to compile and work properly, except for the reading of temperature which fluctuates

I'd like to have a dollar for every time someone posts a snippet of code where they are sure the problem is, when it turns out that it was somewhere else entirely.

please no, you'll be rich in no time :o