I am at my wits end

Why isnt my code working.

#include <Servo.h>

int measurement;
int tempPin = 1;
int pos;
Servo myservo;

void setup(){
  Serial.begin(9600);
  myservo.attach(9);
}
void loop(){
  measurement = analogRead(tempPin);
  float mv = (measurement/1024.0)*5000;
  float celsius = mv/10;
  
  Serial.print("temperature= ");
  Serial.print(celsius);
  Serial.println();
  
  if (celsius > 30){
    pos+=10;
    myservo.write(pos);
  } else if(celsius < 30){
      pos-=10;
      myservo.write(pos);
  }
  delay(1000);
}

I am running the code in Proteus simulator since i havent bought the actual components to make it.
The temperature measurement part is working well.
The idea is that i want to control the servo based on temperature. For example, if temperature is below some set value, such as 30 in this case, i want the servo to rotate 10 degrees in one direction and to check if temperature is still below said 30 degrees Celsius, if it is below i want to increment said angle for another 10 degrees and this goes on for as long as the temperature is above 30 degrees Celsius.

If the temperature drops below said 30 degrees celsius, i want the servo to rotate in 10 degree increments in the opposite direction.

I am sorry for writing like this. This is my first project and i am at my wits end.

Do you mean something like:
"every time there is transition from 30 degrees C to *30.5 degrees C the servo should move 10 degrees (positive angle) and every time there is transition from 30 degrees C to *29.5 degrees C the servo should move 10 degrees (negative angle)"
*some Hysteresis included

No, i want to make it to check in every cycle what the temperature is, and if it is above 30 degrees, i want to increment the position of the servo by 10 degrees in one direction.
If temperature is below said 30 degrees, i want the position to get incremented in the negative direction.

The temperature is measured and compared to a value, and then the servos position gets incremented in either the positive or negative direction.

What temperature sensor? Schematic please.

You appear to have a loop iteration of 1 second.
If the temperature is higher than 30 degrees C you want the servo to rotate in a positive direction at the rate of 1 revolution in 36 seconds.
If the temperature is lower than 30 degrees C you want the servo to rotate in a negative direction t the rate of 1 revolution in 36 seconds.
At exactly 30 degrees C it stops ?

The temperature sensor is "lm35". The arduino board is "Arduino UNO R3", the servo is "motor-pwmservo". I also dont know how can i give you the schematic. I tried uploading the screenshot but it got stuck on "processing".

Well, the system doesnt have to be defined in a clear cut way, the project is supposed to control heating in a solar collector, the servo i supposed to pull the curtain over the collector in case the temperature exceeds some maximum value, in the manual for the solar collector it says that the temperature shouldnt exceed 170 degrees celsius, but that doesnt matter, its just a value for the code.

Also, since it is a solar heating, the "exactly 30 degree point" doesnt matter since it will go above it and then the servo will kick in.

Also, i plan to make said code check once every 10 minutes. But i put a delay of 1 second so i can check quickly if the code is working the right way or not.

Also, i guess i can just put the "<=" to avoid said "exactly a value between two signs" problem.

Does the simulated servo not move?

It just spins in the negative direction forever, regardless if the celsius value is above 30 or below 30.

So the servo is a continuous servo? AKA servo signal controlled gear motor?

Well, i actually dont know what that is. I saw a tutorial and it recommended the "motor-pwmservo" so i just picked it. I dont know what a "continuous servo" is. I only recently started learning.

I think you need to find a different simulated device.

If it is a regular servo that can be positioned by a servo signal, what is keeping it from hitting the end stops and stalling. Stalling the servo can kill it.

I wired up a real regular SG90 servo and real LM35 temperature sensor to my real Uno and using the modified version of your code tried it out. It works fine so I wonder if it is Proteus or the simulated servo. At any rate, this code works in the real world with real parts.

#include <Servo.h>

int measurement;
int tempPin = A1;
int pos;
Servo myservo;

void setup()
{
   Serial.begin(9600);
   myservo.attach(9); 
}
void loop()
{
   measurement = analogRead(tempPin);
   float mv = (measurement / 1024.0) * 5000;
   float celsius = mv / 10;

   Serial.print("temperature= ");
   Serial.print(celsius);
   Serial.println();

   if (celsius > 30)
   {
      pos += 10;
      if(pos >= 180)  // don't stall the motor
      {
         pos = 180;
      }
      myservo.write(pos);
   }
   else if (celsius < 30)
   {
      pos -= 10;
      if(pos <= 0)  // don't stall the motor
      {
         pos = 0;
      }
      myservo.write(pos);
   }
   delay(1000);
}

Note that I put in if statements to prevent the servo from trying to pass the end stops.

There are two main variants of servos. They look identical, but one isn't really a servo - you can't control it's position, only speed and direction. That isn't what you want, but it's what you have in your simulation.

Oh dang, i guess im stuck then? What should i do. Do you know of some better simulator because im not really in a position to buy things right now.

I have not used it but the Wokwi simulator seems to be popular with some of the knowledgeable members.

Edit: I see no LM35 temperature (or equivalent) sensor in the Wokwi sim.

I have managed to upload the picture so you can see if it changes anything.

That is how my Uno, LM35 and servo are wired.

Though if I were actually going to use the servo for anything but an unloaded demonstration, I would use an external power supply capable of over an Amp (per small servo) for it.

No, but there is a potentiometer which could be used to at least test the logic.

Just wire it as a voltage divider, one end to 5 volts, the other end to ground and the wiper to you analog input pin.

Turning the pot will be like reading a higher or lower temperature.

a7

Sorry for the off topic, but how difficult is it to add components to the sim? Just curious.