2013-11-23 2 views
0

저는 Arduino 또는 코딩을 처음 접했고 몇 가지 시도를하고 있습니다. 나는 버튼 누름으로 스크롤 할 수있는 기본 메뉴를위한 코드를 사용하고있다. 이것은 잘 작동하지만 첫 번째 메뉴에 온도를 표시하고 싶습니다.루프 외부 LCD 온도 인쇄

온도를 계산하는 코드는 루프()에 있으며 메뉴를 사용자 정의하는 코드는 setup()loop()보다 앞에 있습니다. lcd.print(temperatureC)을 사용하여 LCD에 온도를 인쇄하고 싶지만 temperatureC은 loop()에서만 선언 되었기 때문에 사용할 수 없습니다.

이 문제를 해결할 수있는 방법이 있습니까? 나는 이것에 아주 새롭다.

#include <LiquidCrystal.h> 

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 >= 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 

답변

0
(빛 독서 코드에서 분리 -이 다른 방법을) 자신의 방법으로 온도를 읽을 수있는 코드를 이동

...

float getTemperature() { 
    int sensorVal = analogRead(tempPin); 
    delay(5); 
    float voltage = (sensorVal) * aref_voltage; 
    voltage /= 1024.0; 
    float temperatureC = (voltage - .5) * 100; 
    temperatureC = round(temperatureC * 2.0)/2.0; 
    return temperatureC; 
} 

그런 다음에이 메소드를 호출 showTemperature() 메소드.