Hello and thanks, this is the code, still under construcktion. But it will run like that on a arduino nano with Oled display 128x64 Ic2. Then You can see the problem I´m struggling with.
Cheers !
#include <I2C_RTC.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
static DS3231 RTC;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BAT0_HEIGHT 8
#define BAT0_WIDTH 16
static const unsigned char PROGMEM bat0_bmp[] =
{ 0b00111111, 0b11111111,
0b00100000, 0b00000001,
0b01100000, 0b00000001,
0b11100000, 0b00000001,
0b11100000, 0b00000001,
0b01100000, 0b00000001,
0b00100000, 0b00000001,
0b00111111, 0b11111111
};
#define BAT25_HEIGHT 8
#define BAT25_WIDTH 16
static const unsigned char PROGMEM bat25_bmp[] =
{ 0b00111111, 0b11111111,
0b00100000, 0b00001111,
0b01100000, 0b00001111,
0b11100000, 0b00001111,
0b11100000, 0b00001111,
0b01100000, 0b00001111,
0b00100000, 0b00001111,
0b00111111, 0b11111111
};
#define BAT50_HEIGHT 8
#define BAT50_WIDTH 16
static const unsigned char PROGMEM bat50_bmp[] =
{ 0b00111111, 0b11111111,
0b00100000, 0b01111111,
0b01100000, 0b01111111,
0b11100000, 0b01111111,
0b11100000, 0b01111111,
0b01100000, 0b01111111,
0b00100000, 0b01111111,
0b00111111, 0b11111111
};
#define BAT75_HEIGHT 8
#define BAT75_WIDTH 16
static const unsigned char PROGMEM bat75_bmp[] =
{ 0b00111111, 0b11111111,
0b00100011, 0b11111111,
0b01100011, 0b11111111,
0b11100011, 0b11111111,
0b11100011, 0b11111111,
0b01100011, 0b11111111,
0b00100011, 0b11111111,
0b00111111, 0b11111111
};
#define BAT100_HEIGHT 8
#define BAT100_WIDTH 16
static const unsigned char PROGMEM bat100_bmp[] =
{ 0b00111111, 0b11111111,
0b00111111, 0b11111111,
0b01111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111111,
0b00111111, 0b11111111,
0b00111111, 0b11111111
};
#define BAT0_HEIGHT 32
#define BAT0_WIDTH 40
static const unsigned char PROGMEM radioaktiv_bmp[] =
{
0b00000000, 0b01000000, 0b00000000, 0b00000010, 0b00000000,
0b00000000, 0b11100000, 0b00000000, 0b00000111, 0b00000000,
0b00000001, 0b11110000, 0b00000000, 0b00001111, 0b10000000,
0b00000011, 0b11111000, 0b00000000, 0b00011111, 0b11000000,
0b00000111, 0b11111100, 0b00000000, 0b00111111, 0b11100000,
0b00000111, 0b11111110, 0b00000000, 0b01111111, 0b11100000,
0b00001111, 0b11111111, 0b00000000, 0b11111111, 0b11110000,
0b00001111, 0b11111111, 0b10000001, 0b11111111, 0b11110000,
0b00011111, 0b11111111, 0b10000001, 0b11111111, 0b11111000,
0b00011111, 0b11111111, 0b00000000, 0b11111111, 0b11111000,
0b00011111, 0b11111110, 0b00011000, 0b01111111, 0b11111000,
0b00111111, 0b11111110, 0b01111110, 0b01111111, 0b11111100,
0b00000111, 0b11111100, 0b01111110, 0b00111111, 0b11100000,
0b00000000, 0b11111100, 0b11111111, 0b00111111, 0b00000000,
0b00000000, 0b00001100, 0b11111111, 0b00110000, 0b00000000,
0b00000000, 0b00000000, 0b01111110, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b01111110, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00011000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b11000011, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b11111111, 0b00000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000001, 0b11111111, 0b10000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000011, 0b11111111, 0b11000000, 0b00000000,
0b00000000, 0b00000111, 0b11111111, 0b11100000, 0b00000000,
0b00000000, 0b00000111, 0b11111111, 0b11100000, 0b00000000,
0b00000000, 0b00000000, 0b01111110, 0b00000000, 0b00000000
};
const int button1 = 10;
const int button2 = 11;
const int button3 = 12;
const int led = 13;
int hour;
int poti1 = A0;
int Analog = 0;
int prozent = 0;
int x = 0;
int m10s = 0;
int m10smem = 0;
void setup() {
Serial.begin(57600);
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(button3,INPUT);
pinMode(led,OUTPUT);
RTC.begin();
//RTC.setHourMode(CLOCK_H12);
// RTC.setHourMode(CLOCK_H24);
/* This will set the clock: uncomment if done !
RTC.setDay(29);
RTC.setMonth(9);
RTC.setYear(2023);
RTC.setHours(23);
RTC.setMinutes(55);
RTC.setSeconds(00);
RTC.setWeek(39);
//RTC.setDate(23,09,29);
//RTC.setTime(22,42,20);/
*/
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.drawBitmap(40, 14, radioaktiv_bmp, 40, 32, WHITE);
display.display();
delay(2000);
//Timer als counter:
TCCR1A = 0; // Init Timer1A
TCCR1B = 0; // Init Timer1B
TCCR1B |= B00000111; // External Clock on T1 PinB5 (RISING)
TCNT1 = 0;
}
void loop() {
display.clearDisplay();
char count[8];
sprintf(count,"%02i",TCNT1);
// Analogwert wurde formatiert ! display.println(analog);
Analog = analogRead(poti1);
//Gerade/ungerade sekunden erkennen:
if(RTC.getSeconds() % 2 == 0) {
x = 1;
}else {
x = 0;
}
switch (x) {
case 0:
m10s = m10s +1;
break;
case 1:
m10smem = m10smem +1;
break;
}
if (digitalRead(button1) == HIGH) {
clockset();
}
// Format time for display:
char stunden[8];
sprintf(stunden,"%02i",RTC.getHours());
char minuten[8];
sprintf(minuten,"%02i",RTC.getMinutes());
char sekunden[8];
sprintf(sekunden,"%02i",RTC.getSeconds());
// make a string for the display : display.println(zeit) !
String zeit = String(stunden) + ":" + String(minuten) + ":" + String(sekunden);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(zeit);
display.setCursor(0, 50);
display.println(x);
display.setCursor(30, 50);
display.println(m10s);
display.setCursor(70, 50);
display.println(m10smem);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(50,30);
display.println(count);
batterie();
display.display(); // Show initial text
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (digitalRead(button1) == HIGH) {
// turn LED on:
digitalWrite(led, HIGH);
} else {
// turn LED off:
digitalWrite(led, LOW);
}
}
// void to set the RTC by buttons
void clockset() {
hour = RTC.getHours();
hour = hour +1;
if (hour > 23) {
hour = 0;
}
RTC.setHours(hour);
delay(250);
}
//void vor battery status !
void batterie() {
Analog = analogRead(poti1);
if (Analog < 254) {
display.drawBitmap(112, 0, bat0_bmp, 16, 8, WHITE);
}
if (Analog > 249) {
display.drawBitmap(112, 0, bat25_bmp, 16, 8, WHITE);
}
if (Analog > 499) {
display.drawBitmap(112, 0, bat50_bmp, 16, 8, WHITE);
}
if (Analog > 749) {
display.drawBitmap(112, 0, bat75_bmp, 16, 8, WHITE);
}
if (Analog > 988) {
display.drawBitmap(112, 0, bat100_bmp, 16, 8, WHITE);
}
}
/*
switch (RTC.getWeek())
{
case 1:
Serial.print("SUN");
break;
case 2:
Serial.print("MON");
break;
case 3:
Serial.print("TUE");
break;
case 4:
Serial.print("WED");
break;
case 5:
Serial.print("THU");
break;
case 6:
Serial.print("FRI");
break;
case 7:
Serial.print("SAT");
break;
}
Serial.print(" ");
Serial.print(RTC.getDay());
Serial.print("-");
Serial.print(RTC.getMonth());
Serial.print("-");
Serial.print(RTC.getYear());
Serial.print(" ");
Serial.print(RTC.getHours());
Serial.print(":");
Serial.print(RTC.getMinutes());
Serial.print(":");
Serial.print(RTC.getSeconds());
Serial.print(" ");
if (RTC.getHourMode() == CLOCK_H12)
{
if(RTC.getMeridiem() == HOUR_AM)
Serial.println(" AM");
if (RTC.getMeridiem() == HOUR_PM)
Serial.println(" PM");
}
delay(10);
*/