Change fan speed in a if loop

Hey,
this is my first Arduino project. everything is working fine but now i want to change the speed of the fan in the program (e.g. between 27°C and 30°C only 50% of the full speed and above that 100%). with a potentiometer i could do it but i want to have it completely automated. and yeah i know the program isn´t the best
first i´ll give you the code and you will finde a picture of the whole circuit. if you need any additional infos let me know.

#include  <LiquidCrystal.h>       

LiquidCrystal lcd(12,11,5,4,3,2);
int inputValue = 7; 
int analogPin = A0;
int readValue = A0;
float temperature = A0; 
int light=1;
int fan=6;

void setup(){
  lcd.begin (16,2);
  pinMode (light, INPUT);
  pinMode (fan, OUTPUT);
  Serial.begin(9600);  
}

void loop(){
  readValue = analogRead(analogPin);
  temperature = (readValue * 0.0049);
  temperature = temperature * 100; 
  int Lux=analogRead(light);
  int clearLCD();// zeile nötig ?
  Serial.println(temperature);              
  delay(1000);
  Serial.println(Lux);               
  delay(1000);
  if (Lux > 511){
    lcd.setCursor(0, 0);
    lcd.print("Bright");
  }
  else{
    lcd.setCursor(0, 0);
    lcd.print("Dark  ");
  }
  if (temperature > 27){
    digitalWrite(fan, HIGH );
    lcd.setCursor(0, 1);
    lcd.print(temperature);
    lcd.print("C Fan: ON ");
  }
  else{
    digitalWrite(fan, LOW);
    lcd.setCursor(0, 1);
    lcd.print(temperature);
    lcd.print("C Fan: OFF");
  }
}

thakns
Janis

An "if" is not a loop, because it doesn't....loop.

int readValue = A0;
float temperature = A0;

Why bother giving them values?

Have you tried PWM control of your fan using analogWrite ?

yeah sorry you see im not so good at it :smiley:

yeah i thought about pwm but all tutorials i was reading didn´t really helped. could you give me an example for pwm with analog write? would be very kind of you

//
PS
solved the problem thx alot. this topic can be closed now