【问题标题】:Refreshing an OLED display using Arduino使用 Arduino 刷新 OLED 显示屏
【发布时间】:2017-10-04 11:51:17
【问题描述】:

我正在构建一个使用 NodeMCU 模块和 128X64 OLED 显示器(I2C 接口)来呈现信息的自动浇水系统。我正在尝试让 OLED 刷新显示屏上的当前系统状态和时间。 为了简化 OLED 上的显示活动,我编写了以下函数,该函数接收短语指针并将短语和当前时间戳输出到显示器。

由于某种原因,我无法弄清楚,每次我在 loop() 段中调用 PrintToLCD() 函数时,都不会向显示器输出任何内容。我确实在串行监视器上看到了相关信息。

请指教。

#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);
}

【问题讨论】:

  • 可能与硬件有关。最近有很多与嵌入式项目相关的问题,我不太确定 SO 是否是解决此类问题的最佳场所,因为问题可能有各种与代码本身无关的原因。
  • 您好,感谢您的回答。我确实认为我做错了什么,因为一旦在循环之外一切正常。可能与我发送指向 PrintToLCD 显示器的指针有关。我还在四处寻找解决方案。

标签: string arduino adafruit


【解决方案1】:

经过研究发现,NODEMCU(特别是 ESP8266)的时钟速度为 80MHZ。 这显然是为了快速刷新 OLED 显示器。 正确的做法是使用Simple Timer库来设置同步定时器来刷新显示屏上的信息。

    SimpleTimer timer;         
    timer.setInterval(10000, refreshDisplay); 

定义一个 refreshDisplay 函数:

  void refreshDisplay()
    {
    display.clearDisplay();
    display.setCursor(0,0);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    digitalClockDisplay();
    display.println(...print something to display...);
    display.display();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多