I have a i2c OLED display and an RTC DS1307, both of them are in A4 and A5, and I do not know how to use both on the same project.
I had the RTC running in Serial, and now I bought a display I'm trying to use the two together.
Here's my Sketch:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <RTClib.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
RTC_DS1307 rtc;
char dia[7][14] = {"Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sabado"};
char mes[12][10] = {"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
String Agora[6];
String aText[2];
String zero(int n){
if(n < 10){
return "0" + String(n);
}else{
return String(n);
}
}
void agora(){
DateTime now = rtc.now();
Agora[1] = zero(now.hour());
Agora[2] = zero(now.minute());
Agora[3] = zero(now.second());
Agora[4] = zero(now.day());
Agora[5] = zero(now.month());
Agora[6] = String(now.year());
aText[1] = dia[now.dayOfTheWeek()];
aText[2] = mes[now.month() - 1];
}
void setup(){
Serial.begin(9600);
if (!rtc.begin()){
Serial.println("RTC nao encontrado!");
while(1);
}
if (! rtc.isrunning()){
Serial.println("RTC nao esta funcionando!");
rtc.adjust(DateTime(F(DATE), F(TIME)));
//rtc.adjust(DateTime(ano, mes, dia, hora, minuto, segundo));
}
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Iniciando...");
display.display();
delay(5000);
display.clearDisplay();
}
void loop(){
agora();
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(Agora[4] + "/" + Agora[5] + "/" + Agora[6]);
display.display();
delay(1000);
}
What should I do? Thank you.
UPDATE: I got yesterday, I was trying to use a library that handled the RTC when I had to deal with it using a wire library and address. It was very simple, I just needed an example ... But the sketch becomes too large ... Thanks for the help!