简体   繁体   English

电位器过低时 LCD 会自行清零

[英]LCD clears itself when potentiometer is too low

I'm making a project that requires an lcd, and the project was working fine on my breadboard for testing.我正在制作一个需要 lcd 的项目,并且该项目在我的面包板上运行良好以进行测试。 I moved it to my final product, but now my lcd stops displaying text after a few minutes, or when the potetiometer value is too low.我把它移到我的最终产品上,但现在我的液晶显示器在几分钟后停止显示文本,或者当电位器值太低时。 One thing that really gets my attention is that if the potentiometer is fully high, the lcd doesn't clear by itself.真正引起我注意的一件事是,如果电位器完全高,液晶显示器不会自行清除。 When I lower it, it happens again.当我降低它时,它会再次发生。 I have a refresh rate in this project as well for about every 250 milliseconds.我在这个项目中也有大约每 250 毫秒的刷新率。 Here is the code for my project along with it's schematic.这是我的项目的代码及其原理图。

schematic -示意图 - 示意图

code -代码 -

#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), InterruptServiceRoutine, 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;
    }
  }
}

Turns out I had a cheap potentiometer.原来我有一个便宜的电位器。 I replaced it and it works good as new.我更换了它,它像新的一样好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM