当电位器太低时 LCD 会自行清除

问题描述

我正在制作一个需要 lcd 的项目,并且该项目在我的面包板上运行良好以进行测试。我将它移到我的最终产品中,但现在我的液晶显示器在几分钟后或当电位计值太低时停止显示文本。真正引起我注意的一件事是,如果电位计完全高,则液晶显示器不会自行清除。当我降低它时,它再次发生。我在这个项目中也有大约每 250 毫秒的刷新率。这是我的项目的代码及其原理图。

示意图 -

schematic

代码 -

#include <LiquidCrystal.h>
#include <RTClib.h>
#include <Wire.h>

LiquidCrystal lcd(12,11,5,4,3,6);
int lcdPrevIoUsTime = 0;

int tempPin = A6;
int tempVal;
float tempVoltage;
int temperatureC;
int temperatureF;
int tempPrevIoUsTime = 0;

int IR_Pin = 2;
int IR_Val;
int prevIoUsIRTime = 0;
volatile float rev = 0;
int oldtime = 0;
int time;
int rpm;
unsigned int mph;
float kmh;

int PrevIoUsSerialTime = 0;
int PrevIoUsClearTime = 0;

int SettingPin = 10;
int SettingSelector;
bool SystemConversion = false;
bool SystemFlag = false;
int PrevIoUsSelectorTime = 0;
bool SelectorPause = false;
bool PrevIoUsSelectorPauseTime = 0;

RTC_DS1307 rtc;
String returnTime() {
  DateTime Now = rtc.Now();
  int hrs = Now.hour();
  String result;

  if (hrs == 0 && hrs != 12) {
    hrs = 12;
  } else if (hrs == 12 && hrs != 0) {
    hrs = 12;
  } else if (hrs < 12 && hrs != 0) {
    hrs = hrs;
  } else if (hrs > 12 && hrs != 0) {
    hrs = hrs - 12;
  }

  result += hrs;
  result += ':';
  result += Now.minute(),DEC;
  result += ':';
  result += Now.second(),DEC;
  result += " ";

  return result;
  result = "";
}

void InterruptServiceRoutine() { rev++; }

void setup() {
  Serial.begin(9600);
  Wire.begin();

  pinMode(IR_Pin,INPUT);
  pinMode(SettingPin,INPUT);
  attachInterrupt(digitalPinToInterrupt(IR_Pin),InterruptServiceRoutine,RISING);

  lcd.begin(16,2);
  lcd.clear();

  bool RTC_Flag = false;
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    RTC_Flag = true;
  }

  if (!rtc.isrunning() && RTC_Flag == false) {
    Serial.println("RTC is NOT running,Time manually set...");
    rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  }

  if (rtc.isrunning() && rtc.begin()) {
    Serial.println("Welcome to Comyar's Bike Speedometer!! It is currently " + returnTime());
    Serial.println("----------------------------------");
    Serial.println();
  }

  lcd.print("Welcome To ");
  lcd.setCursor(0,1);
  lcd.print("Comyar Bike Tool");
  delay(5000);
  lcd.clear();
}

void loop() {
    SettingSelector = digitalRead(SettingPin);
    int CurrentSelectorTime = millis();
    int CurrentSelectorPauseTime = millis();
    int SelectorSampleRate = 1000;
    int SelectorPauseSampleRate = 500;

    if(CurrentSelectorPauseTime - PrevIoUsSelectorPauseTime >= SelectorPauseSampleRate){
      if(SelectorPause == true && SettingSelector == LOW){
        SelectorPause = false;
      }
        PrevIoUsSelectorPauseTime = CurrentSelectorPauseTime;
    }

    if(CurrentSelectorTime - PrevIoUsSelectorTime >= SelectorSampleRate && SelectorPause == false){
      if(SettingSelector == HIGH){
        if(SystemConversion == false){
          SystemConversion = true;
          SystemFlag = true;
          SelectorPause = true;
        }
        if(SystemConversion == true && SystemFlag == false){
          SystemConversion = false;
          SelectorPause = true;
        }
        SystemFlag = false;
      }
      PrevIoUsSelectorTime = CurrentSelectorTime;
    }

    int IR_Rate = 1000;
    int CurrentIRTime = millis();

    if (CurrentIRTime - prevIoUsIRTime >= IR_Rate) {  
      detachInterrupt(digitalPinToInterrupt(IR_Pin));
      int IR_Val = digitalRead(IR_Pin);
      time = millis() - oldtime; 
      rpm = (rev/time) * 60000.0/3; 
      oldtime = millis(); 
      rev = 0;
      mph = (60 * (rpm * (20 * 3.14)))/63360.0;
      kmh = mph * 1.609;
      attachInterrupt(digitalPinToInterrupt(IR_Pin),RISING);

      prevIoUsIRTime = CurrentIRTime;
    }

    int tempSampleRate = 500;
    int tempCurrentTime = millis();

    if (tempCurrentTime - tempPrevIoUsTime >= tempSampleRate) {
      tempVal = analogRead(A6);
      tempVoltage = (tempVal / 1024.0) * 5.0;
      temperatureC = (tempVoltage - .5) * 100;
      temperatureC-=4;
      temperatureF = (temperatureC * 1.8) + 32;

      tempPrevIoUsTime = tempCurrentTime;
    }

    int lcdSampleRate = 25;
    int lcdCurrentTime = millis();

    if (lcdCurrentTime - lcdPrevIoUsTime >= lcdSampleRate) {
      lcd.setCursor(0,1);
      lcd.print("S:");

      lcd.setCursor(6,0);
      lcd.print("T:" + returnTime());

      if(SystemConversion == false){
        lcd.setCursor(0,0);
        lcd.print("F:");
        lcd.setCursor(2,0);
        lcd.print(String(temperatureF) + "*");
      }else{
        lcd.setCursor(0,0);
        lcd.print("C:");
        lcd.setCursor(2,0);
        lcd.print(String(temperatureC) + "*");
      }

      lcdPrevIoUsTime = lcdCurrentTime;
    }

    int SerialSampleRate = 25;
    int SerialCurrentTime = millis();

    if(SerialCurrentTime - PrevIoUsSerialTime >= SerialSampleRate){
      if(rpm > 0){
        Serial.println("rpm: " + String(rpm));
        Serial.println("mph: " + String(mph));
      
        lcd.setCursor(2,1);
        if(SystemConversion == false){
          lcd.print(String(mph) + "mph");
        }else{
          lcd.print(String((int)(kmh)) + "kmh");
        }

        lcd.setCursor(8,1);
        lcd.print("R:" + String(rpm) + "rpm");
      }else{
        lcd.setCursor(2,1);
        if(SystemConversion == false){
          lcd.print(String(mph) + "mph");
        }else{
          lcd.print(String((int)(kmh)) + "khm");
        }

        lcd.setCursor(8,1);
       lcd.print("R:" + String(rpm) + "rpm");

       PrevIoUsSerialTime = SerialCurrentTime;
    }

    int ClearSampleRate = 250;
    int ClearCurrentTime = millis();

    if(ClearCurrentTime - PrevIoUsClearTime >= ClearSampleRate){
      lcd.clear();
      PrevIoUsClearTime = ClearCurrentTime;
    }
  }
}

解决方法

原来我有一个便宜的电位器。我更换了它,它和新的一样好用。