Arduino不能同时在led屏和串口显示器上输出

Arduino can't output in both led screen and serial monitor at the same time

我正在尝试使用 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);
}

干杯

液晶显示器使用其他Arduino引脚。 D1与串口通讯共享。

On the docs of Arduino's Serial

Serial

It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.

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

像这样 LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

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