title: Beginner Arduino loop function and variable
body: <p>This is the revised code, the initial value is reading ridiculously high without the conversion!! How do I get the conversion to apply to the initialTemp as well temperatureC?</p>
  
  <p>The code you have helped with as far as I can tell is exactly what I was trying to achieve, but obviously.</p>
  
  <p>Thanks again!!</p>
  
  <pre><code>int pin_tempRead = 0;   // temperature sensor pin
  int coolLED = 2; // cooling LED digital pin
  int heatLED = 3; // heating LED digital pin
  float initialTemp;
  float cutOffTemp = 30; //cut off temperature = 30°C
  
  void setup()
  {
    Serial.begin(9600);  //Start the serial connection with the computer
    pinMode(heatLED, OUTPUT); //initialise as OUTPUT
    pinMode(coolLED, OUTPUT); //initialise as OUTPUT
    initialTemp = analogRead(pin_tempRead);   // read the initial temp
    Serial.print("Initial temperature: "); Serial.print(initialTemp); Serial.println("C"); //prints out starting temperature
  }
  
  void loop()                     // run over and over again
  {
    //getting the voltage reading from the temperature sensor
    float current_temp = analogRead(pin_tempRead);
  
    // converting that reading to voltage
    float voltage = current_temp * 5.0; voltage /= 1024.0;
  
    float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree with 500 mV offset
    //to degrees ((voltage - 500mV) times 100)
  
      if(temperatureC &gt; cutOffTemp) {
          // temp too high -&gt; turn on the cooling system
          digitalWrite(heatLED, LOW);
          digitalWrite(coolLED, HIGH);                          
      }else if (temperatureC &lt; initialTemp) {
          // temp too low -&gt; turn on the heating system
          digitalWrite(heatLED, HIGH);
          digitalWrite(coolLED, LOW);
      }
      Serial.print("Current pump temperature: ");
      Serial.print(temperatureC); Serial.println("C");
      delay(1000);
  }
  </code></pre>