我正在使用 NodeMCU 模块和 128X64 OLED 显示器(I2C 接口)构建一个自动浇水系统来呈现信息。我正在尝试让 OLED 刷新显示屏上的当前系统状态和时间。为了简化 OLED 上的显示活动,我编写了以下函数,该函数接收短语指针并将短语和当前时间戳输出到显示器。
出于某种原因,我无法弄清楚,每次我在段PrintToLCD()
内调用函数时loop()
,都没有输出到显示器。我确实在串行监视器上看到了相关信息。
请指教。
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <DS3231.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
Serial.begin(9600);
Serial.println();
......some setup code.......
// initialize Oled display with the I2C addr 0x3D (for the 128x64)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false);
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splash screen
// internally, this will display the splash screen.
display.display();
delay(500);
// Clear the display buffer.
display.clearDisplay();
}
void loop() {
// Get UTC time
//--------------------------------------------------------
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) {
//update the display only if time has changed
prevDisplay = now();
digitalClockDisplay(timeString);
PrintToLCD(timeString,0,30);
//***this works fine and prints to display***
}
}
......some business logic code.......
PrintToLCD("Current watering Valve",0,30);
//This does not display any output
}
void PrintToLCD(const String& phrase, int XOffset, int YOffset) {
display.setCursor(XOffset,YOffset);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(phrase);
display.println(" ");
digitalClockDisplay(timeString);
display.println(timeString);
display.display();
delay(1000);
}
void digitalClockDisplay(String timeString) {
// digital clock display of the time
String sHour=String(hour());
String sMinute=String(minute());
String sSec=String(second());
String sDay=String(day());
String sMonth=String(month());
String sYear=String(year());
timeString=String(sDay + "/" + month() + "/" + year() + " "+ hour()+
":"+minute()+":"+second());
if (minute()<10) {
timeString=String(sDay + "/" + sMonth + "/" + year() + " "+ hour()+ ":0" + minute()+":"+second());
} else if (second()<10) {
timeString=String(sDay + "/" + sMonth + "/" + year() + " "+ hour()+ ":"+minute()+":0"+second());
}
Serial.println("Function: digitalClockDisplay");
Serial.println(timeString);
}