// https://forum.arduino.cc/t/ds3231-24-hours-time-format/981645
#include <Wire.h>
#include <DS3231.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
// --------------
#define BTN_PIN 3
#define LED 12
#define dspTime 5000
// 0X3C+SA0 - 0x3C or 0x3D
#define SSDI2C_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1
// --------------
const int debounceDelay = 100; // milliseconds to wait until button input stable *
// --------------
int format = 24;
int hours;
int minutes;
int seconds;
bool h12Flag;
bool pmFlag;
bool oledMode;
// --------------
SSD1306AsciiWire oled;
DS3231 clk;
// --------------
void time_setup()
{
Serial.println(" ");
Serial.println("DS3231 based Clock and Calender");
//Read the time from serial monitor and display entries
Serial.print("Enter the hours : ");
while(Serial.available() == 0) { }
hours = Serial.parseInt();
Serial.println(hours);
Serial.print("Enter the minutes : ");
while(Serial.available() == 0) { }
minutes = Serial.parseInt();
Serial.println(minutes);
Serial.print("Enter the seconds : ");
while(Serial.available() == 0) { }
seconds = Serial.parseInt();
Serial.println(seconds);
//Print the resulting time
Serial.println("-------------------------------------------------------------");
Serial.print("The RTC is set to : ");
Serial.print(hours);
Serial.print(" : ");
Serial.print(minutes);
Serial.print(" : ");
Serial.println(seconds);
Serial.println("-------------------------------------------------------------");
return;
}
// --------------
void RTC_setup()
{
clk.setHour(hours);
clk.setMinute(minutes);
clk.setSecond(seconds);
clk.setClockMode(false);
Serial.println("RTC has been set with the new time.");
Serial.println("-------------------------------------------------------------");
return;
}
// --------------
void RTC_oled()
{
unsigned long startTime;
startTime = millis();
while (millis() <= (startTime + dspTime))
{
hours = clk.getHour(h12Flag,pmFlag);
minutes = clk.getMinute();
seconds = clk.getSecond();
oled.setCursor(1,1);
// display hours
if(hours<10)
{
oled.print(" 0");
}
else
{
oled.print(" ");
}
oled.print(hours);
oled.print(" : ");
// display minutes
if(minutes<10)
{
oled.print("0");
}
oled.print(minutes);
oled.print(" : ");
// display seconds
if(seconds<10)
{
oled.print("0");
}
oled.print(seconds);
if (oledMode == true)
{
oledMode = false;
break;
}
}
return;
}
// --------------
void wait(int waitTime)
{
unsigned long startTime;
startTime = millis();
while (millis() < (startTime + waitTime))
{
}
// do nothing
return;
}
// --------------
boolean debounce(int pin) // debounce returns true if the switch in the given pin is closed and stable
{
boolean state;
boolean previousState;
previousState = digitalRead(pin); // store switch state
for (int counter = 0; counter < debounceDelay; counter++)
{
delay(1); // wait for 1 millisecond
state = digitalRead(pin); // read the pin
if ( state != previousState)
{
counter = 0; // reset the counter if the state changes
previousState = state; // and save the current state
}
}
// here when the switch state has been stable longer than the debounce period
return state;
}
// --------------
void buttonPushed() // detect and debounce button push using assigned interrupt *
{
if (debounce(BTN_PIN)) // calls debounce procedure *
{
// digitalWrite(LED, HIGH); // Turn the button-LED on for feedback
oledMode = true;
// Serial.println(oledMode);
}
// delay(1000);
// digitalWrite(LED, LOW); // Turn the button-LED off
return;
}
// --------------
void setup()
{
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
attachInterrupt(digitalPinToInterrupt(BTN_PIN), buttonPushed, FALLING); // assign interrupt to button, procedure to be run and set state to LOW when pushed *
Serial.begin(115200);
Wire.begin();
// Wire.setClock(400000L);
#if RST_PIN >= 0
oled.begin(&Adafruit128x64, SSDI2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
oled.begin(&Adafruit128x64, SSDI2C_ADDRESS);
#endif // RST_PIN >= 0
oled.clear();
oled.setFont(Verdana12);
oledMode = false;
time_setup();
RTC_setup();
}
// --------------
void loop()
{
if (oledMode == true)
{
oledMode = false;
RTC_oled();
oled.clear();
}
}