title: Printing temperature to LCD outside loop
body: <p>I'm new to Arduino or coding in general and I'm trying some things. I'm using code for a basic menu that you can scroll through with a button press. This works fine but I want it to display the temperature on the first menu.</p>
  
  <p>The code for calculating the temperature is in the loop() while the code for customizing the menu's is before <code>setup()</code> and <code>loop()</code>. I want to print the temperature to the LCD using <code>lcd.print(temperatureC)</code> but <code>temperatureC</code> cannot be used since it's only declared in loop().</p>
  
  <p>Is there any way I can work around this? I'm very new to this.</p>
  
  <pre><code>#include &lt;LiquidCrystal.h&gt;
  
  LiquidCrystal lcd(8,9,10,11,12,13);
  int tempPin = A0;
  int photocellPin = A1;
  const byte mySwitch = 7;
  #define aref_voltage 3.3
  
  
  // these "states" are what screen is currently being displayed.
  typedef enum 
    {
    POWER_ON, TEMPERATURE, LIGHTSENSOR, EXHAUST_FAN1, EXHAUST_FAN2,
    // add more here ...
  
    LAST_STATE  // have this last
    } states;
  
  byte state = POWER_ON;
  
  byte oldSwitch = HIGH;
  
  void powerOn ()
    {
    Serial.println ("Welcome!");
    lcd.setCursor(0,0);
    lcd.print("Welcome!");
    delay(2000);
  
    }
  
  void showTemperature ()
    {
    Serial.println ("Temperature");
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Temperature");
    lcd.setCursor(0,1);
    lcd.print(temperatureC);
  
  
  
  void setup()
  {
    Serial.begin(9600);  //Start the serial connection with the computer
                         //to view the result open the serial monitor 
    analogReference(EXTERNAL);
    pinMode (mySwitch, INPUT_PULLUP);
    lcd.begin(16, 2);
    lcd.clear();
    powerOn ();
  }
  
  void loop()
  { 
    int sensorVal = analogRead(tempPin);
    delay(5);
    int photocellVal = analogRead(photocellPin);
    delay(5);
    float voltage = (sensorVal) * aref_voltage;
    voltage /= 1024.0; 
    float temperatureC = (voltage - .5) * 100;
    temperatureC = round(temperatureC * 2.0) / 2.0;
  
    {
    byte switchValue = digitalRead (mySwitch);
  
    // detect switch presses
    if (switchValue != oldSwitch)
      {
      delay (100);   // debounce
  
      // was it pressed?
      if (switchValue == LOW)
        {
        state++;      // next state
        if (state &gt;= LAST_STATE)
          state = TEMPERATURE;  
  
        switch (state)
          {
          case POWER_ON:     powerOn ();         break;
          case TEMPERATURE:  showTemperature (); break;
          case LIGHTSENSOR:  showLightsensor (); break;
          case EXHAUST_FAN1: showExhaustFan1 (); break;
          case EXHAUST_FAN2: showExhaustFan2 (); break;
          }  // end of switch
        }  // end of switch being pressed
  
      oldSwitch = switchValue;  
      } // end of switch changing state
  
    }  // end of loop
  </code></pre>