2017-10-08 6 views
0

저는 Arduino 프로그래밍을 처음 사용하고 있으며 수동으로 시계를 설정할 수 있습니다. 시간을 선택하려면 로터리 센서를 사용하고 시간을 확인하고 분을 바꾸려면 버튼을 사용하고 있습니다. 그러나 현재 시간을 변경하고 적절한 분 값을 선택하면시 값이 00으로 다시 설정됩니다.전역 변수가 'if'에서 편집되고 'else if'에서 다시 초기 값으로 재설정됩니다.

Grove 회전 센서, LCD 디스플레이 및 버튼이있는 Genuino Uno 보드를 사용하고 있습니다. 기본 방패로 잘.

누군가 내 오류를 명확히하고 내 변수가 시간과 분을 편집하는 사이에 재설정되는 이유를 제안 할 수 있기를 바랍니다.

#include <rgb_lcd.h> 

rgb_lcd lcd; 

const int colorR = 255; 
const int colorG = 20; 
const int colorB = 147; 

const int analogInPin = 0; // Analog input pin that the rotary sensor is attached to 

int sensorValue = 0;  // value read from the rotary sensor 
int outputValue = 0; 
int button = 2; 
int hour = 00; 
int minute = 00; 
int button_state = 0; 
int minute_temp = 0; 
int hour_temp = 0; 

void setup() { 
    // set up the LCD's number of columns and rows: 
    Serial.begin(9600); 
    lcd.begin(16, 2); 
    pinMode(button, INPUT); 

    lcd.setRGB(colorR, colorG, colorB); 
    Serial.println("HELLO"); 

} 
void loop() { 
    int confirm = 0; 
    confirm = digitalRead(button); 
    if (button_state == 0) { 
    // read the analog in value: 
    sensorValue = analogRead(analogInPin); 
    // map it to the range of the analog out: 
    outputValue = map(sensorValue, 0, 1023, 0, 23); 
    // change the analog out value: 
    lcd.clear(); 
    lcd.setCursor(0, 0); 

    String hour = String(outputValue); 
    if (outputValue < 10) { 
     hour = ("0" + hour); 
    } 

    minute_temp = minute; 
    String minute = String(minute_temp); 
    if (minute_temp < 10) { 
     minute = ("0" + minute); 
    } 

    lcd.print(hour); 
    lcd.print(":"); 
    lcd.print(minute); 

    // wait 10 milliseconds before the next loop 
    // for the analog-to-digital converter to settle 
    // after the last reading: 
    delay(10); 
    if (confirm == HIGH){ 
     button_state = 1; 
     delay(1000); 
    } 
    } 
    else if (button_state == 1){ 
    // read the analog in value: 
    sensorValue = analogRead(analogInPin); 
    // map it to the range of the analog out: 
    outputValue = map(sensorValue, 0, 1023, 0, 59); 
    // change the analog out value: 
    lcd.clear(); 
    lcd.setCursor(0, 0); 

    String minute = String(outputValue); 
    if (outputValue < 10) { 
     minute = ("0" + minute); 
    } 

    hour_temp = hour; 
    String hour = String(hour_temp); 
    if (hour_temp < 10) { 
     hour = ("0" + hour); 
    } 

    lcd.print(hour); 
    lcd.print(":"); 
    lcd.print(minute); 

    // wait 10 milliseconds before the next loop 
    // for the analog-to-digital converter to settle 
    // after the last reading: 
    delay(10); 
    if (confirm == HIGH){ 
     button_state = 5; 
    } 
    } 
} 

고맙습니다.

답변

0

시간이라는 이름의 if 문 안에 두 개의 다른 변수가있는 것 같습니다. hour라고하는 전역 int가 있고 hour라는 로컬 String도 있습니다. 로컬 String은 글로벌 int를 섀도 잉합니다. hour라는 전역 int 변수에 0 이외의 값을 절대 입력하지 마십시오. 동일한 범위에서 동일한 이름을 가진 두 개의 변수를 갖는 것이 혼란을 막는 확실한 방법입니다. 그 두 개의 다른 이름을 줘서 문제가 보일 것 같아요.

+0

대단히 감사합니다 !! –