【问题标题】:Arduino stops my code if I put LCD loop codes如果我输入 LCD 循环代码,Arduino 会停止我的代码
【发布时间】:2020-03-31 20:35:07
【问题描述】:

我对 Arduino 有点陌生,英语不是我的第一个语言,所以可能会出现一些错误。

我只是将做一个项目作为一种爱好来保持时间。我正在尝试获取空气温度、水温和水流量。对于我正在使用的这些数据

LM35DZ、DS18B20、YF-S201

尽管我从其他项目中获得了一些代码,但它们运行良好。我正在从串行监视器获取这些数据,并且我有一个 LCD 来查看这些数据。

在我输入 LCD 代码后,void setup 部分工作得很好。我得到了 A: H: S: / 它们应该在的位置(当您查看设置中我的代码的 LCD 部分时,您会明白我在说什么)。对于循环部分,当我将 lcd 代码放在末尾时它不起作用,并且在串行监视器上它在计算水流时停止(如果我删除这些代码,串行监视器工作得很好)。

https://imgur.com/a/zqUDfw5

我把我的整个代码放在你需要看的地方。你可以直接跳到问题。

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
DeviceAddress thermometerAddress;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);

//havasicaklik
int lm35Pin = A0;
int okunan_deger = 0;
float sicaklik_gerilim = 0;
float sicaklik = 0;
float susicakligi = 0;

//suakis
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                              
#define hallsensor 2    //The pin location of the sensor

//potansiyometre
#define pts A1
float ptsck = 30;

//lcd
LiquidCrystal_I2C lcd(0x27, 16, 2);

void rpm () {    //This is the function that the interupt calls
NbTopsFan++;  //This function measures the rising and falling edge of the
}

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

  //susicaklik
  tempSensor.begin();
  tempSensor.setResolution(thermometerAddress, 9);

  //suakis
  pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached

  //lcd
  lcd.begin();
  //yükleme ekranı
  lcd.home();
  lcd.print(" Berke  Karasah");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("Yukleniyor"); //This is just to make it look like it's loading
  for (int k = 0;k < 2; k++) {
    for (int i = 0;i < 7; i++) {
    delay(75);
    lcd.print(".");
    }
  lcd.setCursor(10,1);
  lcd.print("      ");
  lcd.setCursor(10,1);
  }
  lcd.clear();
  lcd.home();        // This part where the datas should be*******
  lcd.print("A: ");     //Calc: Water flow
  lcd.setCursor(7,0);
  lcd.print("H:");     //sicaklik: Air temp
  lcd.setCursor(0,1);
  lcd.print("S: ");    //susicakligi: Water temp
  lcd.setCursor(9,1);
  lcd.print("/");      //ptsck: Potentiometer value
}

void loop() {

  delay(1000);

  //havasicaklik
  okunan_deger = analogRead(lm35Pin);
  sicaklik_gerilim = (5000.0 / 1023.0) * okunan_deger;
  sicaklik = sicaklik_gerilim / 10.0;
  Serial.print("Hava sicakligi: ");
  Serial.print(sicaklik);

  Serial.print("\t");

  //susicaklik
  tempSensor.requestTemperatures();
  Serial.print("Su sicakligi: ");
  susicakligi = tempSensor.getTempCByIndex(0);
  Serial.print(susicakligi);

  Serial.print("\t");

  //potansiyometre
  ptsck = analogRead(pts);
  ptsck = map(ptsck, 0,1023,0,50);
  Serial.print("Hedef sicaklik: ");
  Serial.print(ptsck);

  Serial.print("\t");

  //suakis
  NbTopsFan = 0;    //Set NbTops to 0 ready for calculations
  sei();            //Enables interrupts
  delay(1000);      //Wait 1 second
  cli();            //Disable interrupts
  Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate
  Serial.print("Akis hizi: ");
  Serial.print(Calc, DEC); //Prints the number calculated above
  Serial.print(" L/s"); //Prints "L/hour" and returns a  new line

  Serial.print("\n");

  //lcd        !!!!!!!!!!!!!!!!!!!THIS IS THE PROBLEM!!!!!!!!!!!!!!!!!!!!!!!!!!!
  lcd.setCursor(3,0);
  lcd.print(Calc);
  lcd.setCursor(10,0);
  lcd.print(sicaklik);
  lcd.setCursor(3,1);
  lcd.print(susicakligi);
  lcd.setCursor(11,1);
  lcd.print(ptsck);


}

【问题讨论】:

  • 你可以找到问题的解决方案here

标签: loops arduino arduino-uno lcd


【解决方案1】:

尝试在以下内容之前添加lcd.clear()

  lcd.setCursor(3,0);
  lcd.print(Calc);
  lcd.setCursor(10,0);
  lcd.print(sicaklik);
  lcd.setCursor(3,1);
  lcd.print(susicakligi);
  lcd.setCursor(11,1);
  lcd.print(ptsck);

之后是delay(100);

【讨论】:

  • 没有任何改变。它仍然像屏幕截图一样卡住。顺便说一句,我不想​​清除我的液晶显示器,我想将这些值放在液晶显示器的空白部分。您也可以在照片中看到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 1970-01-01
  • 2021-05-07
  • 2015-08-19
  • 1970-01-01
相关资源
最近更新 更多