代码之家  ›  专栏  ›  技术社区  ›  T Draper

在“if”中编辑全局变量,并在“else if”中重置回初始值

  •  0
  • T Draper  · 技术社区  · 7 年前

    我刚开始编程Arduino,我正在尝试手动设置时钟。我用一个旋转传感器来选择小时,然后用一个按钮来确认小时,然后用交换来更改分钟。但是,当前,当我更改小时并选择适当的分钟值时,小时值会重置回00。

    我使用的是Genuino Uno板,带有Grove旋转传感器、LCD显示屏和按钮,以及基座屏蔽。

    我希望有人能澄清我的错误,并提出为什么我的变量在编辑小时和分钟之间重置。

    #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;
        }
      }
    }
    

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Delta_G    7 年前

    在名为hour的if语句中,似乎有两个不同的变量。全局int名为hour,局部字符串名为hour。局部字符串将覆盖全局int。您永远不会为名为hour的全局int变量指定除0以外的任何值。在同一范围内有两个同名的变量肯定会引起混淆。给这两个不同的名字,我想你会发现问题所在。