我正在尝试使用 LiquidCrystal 库在 LED 屏幕上和串行监视器上输出 Arduino(稍后输出到 txt 文件或类似文件)。
在我的代码中,我注释掉了 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);
}
干杯
最佳答案
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 个引脚
像这样
液晶屏(2,3,4,5,6,7);
或者如果您必须拥有这些引脚,您可能会考虑使用像 softwareSerial 这样的库它模拟串行通信您选择的一对引脚。但通过 USB 的串行监视器无论如何都无法工作。
关于Arduino无法同时在LED屏和串口监视器上输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51335310/