首页 文章

Arduino无法同时在LED屏幕和串行监视器中输出

提问于
浏览
2

我试图在使用LiquidCrystal库的LED屏幕和串行监视器(后来到txt文件或类似的东西)上使用Arduino输出 .

在我的代码中,我注释掉Serial.begin(9600)然后屏幕输出正确,但是一旦我包含它,串行监视器输出正常但屏幕翻转并输出乱码 . 我是相当新的,我知道有一些基本的我不知道9600应该增加,因为可能需要这么多的力量?

#include <LiquidCrystal.h>
#include <DHT.h>
#include <math.h>

/*
  * Cannot do both screens and log in the console.
  * Currently Serial 9600 is commented out, to allow to print on the screen
  * Need Fixing
*/

#include "DHT.h"

#define DHTPIN 8     // what digital pin we're connected to


#define DHTTYPE DHT11   // DHT 11
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

void setup() {
  //Serial.begin(9600);
  Serial.println("Temperature Recorder");

  dht.begin();

  // Now LiquidCrystal led monitor stuff
  lcd.begin(16,2);
  lcd.setCursor(2,0);
  lcd.print("** Wanet **");
  delay(1500);
  lcd.setCursor(1,1);
  lcd.print("Motherfuckers.");
  delay(3000);
  lcd.clear();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(1000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print(" | Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
  Serial.println("------------------------------------");

  //   led screen printing
  lcd.setCursor(0,0);
  lcd.print("Temp:  Humidity:");
  lcd.setCursor(0,1);
  lcd.print(t);
  lcd.print("   ");
  lcd.print(round(f));
  lcd.print("%");

  delay(5000);
  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("The world");
  lcd.setCursor(4,1);
  lcd.print("OURS");
  delay(6000);
}

干杯

2 回答

  • 3

    使用其他Arduino引脚作为LCD显示屏 . D1与串行通信共享 .

  • 0

    On the docs of Arduino's Serial

    串行它通过USB在数字引脚0(RX)和1(TX)以及计算机上进行通信 . 因此,如果使用这些功能,则不能将引脚0和1用于数字输入或输出 .

    您有两个选项,或者您不使用这两个引脚

    喜欢这个 LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

    或者如果您必须拥有这些引脚,您可以考虑使用像softwareSerial这样的库,它可以模拟您选择的一对引脚的串行通信 . 但是通过USB的串行监视器无论如何都无法工作 .

相关问题