Arduino - oled

 
Vista:

oled

Publicado por aaa (1 intervención) el 19/09/2023 22:50:59
hola estimados, tengo un problema estoy queriendo hacer un proyecto de arduino que muestre la hora actual en una pantalla oled 128x64 usando un SD1302. tengo un codigo base que es el ejemplo de la libreria RtcDS1302.h o rtc by makuna y funciona pero como no tiene programada una pantalla oled intente programarla (AVISO: no se programar mucho pero algo se). a la hora de cargar el codigo que programe, el codigo sube pero la pantalla oled no muestra nada. aqui dejo el codigo de la libreria y abajo el codigo con la pantalla que no funciona

codigo de la libreria:

// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

#include <RtcDS1302.h>

ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

void setup ()
{
Serial.begin(57600);

Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);

Rtc.Begin();

RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();

if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing

Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}

if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}

if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}

RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}

void loop ()
{
RtcDateTime now = Rtc.GetDateTime();

printDateTime(now);
Serial.println();

if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}

delay(1000); // ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
char datestring[26];

snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}








codigo que programe:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RtcDS1302.h>

// Configuración de la pantalla OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Pin de reset (no se utiliza)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

ThreeWire myWire(4, 5, 2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

void setup() {
// Inicializa la comunicación I2C para la pantalla OLED
Wire.begin();
display.begin(0x3C, OLED_RESET);
display.display();
delay(2000); // Puedes ajustar el tiempo de visualización de la pantalla de inicio
display.clearDisplay();
display.display();

Serial.begin(57600);

Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);

Rtc.Begin();

RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();

if (!Rtc.IsDateTimeValid()) {
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}

if (Rtc.GetIsWriteProtected()) {
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}

if (!Rtc.GetIsRunning()) {
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}

RtcDateTime now = Rtc.GetDateTime();
if (now < compiled) {
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
} else if (now > compiled) {
Serial.println("RTC is newer than compile time. (this is expected)");
} else if (now == compiled) {
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}

void loop() {
RtcDateTime now = Rtc.GetDateTime();

// Borra la pantalla OLED antes de mostrar la hora
display.clearDisplay();

// Formatea la hora
char timeString[9];
snprintf_P(timeString,
countof(timeString),
PSTR("%02u:%02u:%02u"),
now.Hour(),
now.Minute(),
now.Second());

// Muestra la hora en la pantalla OLED
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Hora:");
display.setCursor(40, 0); // Ajusta la posición según tu pantalla
display.print(timeString);
display.display();

// Muestra la hora en el monitor serial
printDateTime(now);
Serial.println();

if (!now.IsValid()) {
Serial.println("RTC lost confidence in the DateTime!");
}

delay(1000); // Un segundo
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt) {
char datestring[26];

snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second());
Serial.print(datestring);
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder