// Pass Plus Task Question1B - Simulation:
#include <Wire.h>
#include "RTClib.h"
#include "U8glib.h"
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <string.h>
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1
int hrposy[12]{17, 19, 24, 32, 39, 44, 47, 44, 39, 31, 24, 19};
int hrposx[12]{66, 73, 78, 81, 78, 73, 65, 58, 53, 51, 53, 58};
int minposy[60]{12, 12, 12, 12, 13, 14, 15, 17, 18, 20, 22, 23,
25, 27, 29, 32, 34, 36, 38, 40, 42, 43, 45, 46,
48, 49, 50, 51, 51, 51, 52, 51, 51, 51, 50, 49,
48, 46, 45, 43, 41, 40, 38, 36, 34, 31, 29, 27,
25, 23, 21, 20, 18, 17, 15, 14, 13, 12, 12, 12};
int minposx[60]{ 66, 68, 70, 72, 74, 76, 77, 79, 80, 82, 83, 84,
85, 85, 85, 86, 85, 85, 85, 84, 83, 82, 80, 79,
77, 75, 74, 72, 70, 68, 65, 63, 61, 59, 57, 55,
54, 52, 51, 49, 48, 47, 46, 46, 46, 46, 46, 46,
46, 47, 48, 49, 51, 52, 54, 56, 57, 59, 61, 63};
int secposy[60]{2, 2, 2, 3, 4, 6, 7, 9, 11, 14, 17, 19, 22, 25,
28, 32, 35, 38, 41, 44, 47, 49, 52, 54, 56, 57,
59, 60, 61, 61, 62, 61, 61, 60, 59, 57, 56, 54,
52, 49, 46, 44, 41, 38, 35, 31, 28, 25, 22, 19,
16, 14, 11, 9, 7, 6, 4, 3, 2, 2};
int secposx[60]{66, 69, 72, 75, 78, 81, 83, 86, 88, 90, 91, 93,
94, 95, 95, 96, 95, 95, 94, 93, 91, 90, 88, 86,
83, 80, 78, 75, 72, 69, 65, 62, 59, 56, 53, 50,
48, 45, 43, 41, 40, 38, 37, 36, 36, 36, 36, 36,
37, 38, 40, 41, 43, 45, 48, 51, 53, 56, 59, 62};
int hour{};
int minute{};
int seconds{0};
int time[4]{};
char secsArr [4]{};
char minsArr [4]{};
char hrArr [4]{};
U8GLIB_SSD1306_128X64 display(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
RTC_DS1307 RTC;
void setup() {
//set the switch
pinMode(11, INPUT);
pinMode(12, INPUT);
display.setFont(u8g_font_tpssb);
Serial.begin(115200);
if (! RTC.begin()) //start clock
{
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
Serial.println("Enter time in format of 4 ints for hours and minutes.");
while(Serial.available()<4){}
if(Serial.available()>=4)
{
for(int i=0;i<4;i++)
{
time[i] = Serial.read()-'0';
}
}
hour= 10*time[0]+time[1];
minute= 10*time[2]+time[3];
//year //mont //day //hr //min //sec
RTC.adjust(DateTime(2022, 8, 18, hour, minute, 0));
display.setColorIndex(1);
}
void loop() {
int secs{RTC.now().second()};
int mins{RTC.now().minute()};
int hours{RTC.now().hour()};
sprintf (secsArr, "%d", secs);
sprintf (minsArr, "%d", mins);
sprintf (hrArr, "%d", hour);
char timeDisplay[9]{};
for(int i{0};i<8;i++)
{
if(i==2 || i==5)timeDisplay[i]=':';
else timeDisplay[i]='0';
}
if(secs>9)
{
timeDisplay[6]=secsArr[0];
timeDisplay[7]=secsArr[1];
}
else timeDisplay[7]=secsArr[0];
if(mins>9)
{
timeDisplay[3]=minsArr[0];
timeDisplay[4]=minsArr[1];
}
else timeDisplay[7]=minsArr[0];
if(hour>9)
{
timeDisplay[0]=hrArr[0];
timeDisplay[1]=hrArr[1];
}
else timeDisplay[7]=hrArr[0];
timeDisplay[8]='\0';
// put your main code here, to run repeatedly:
if(hours>=12) hours=hours-12;
display.firstPage();
do {
if(digitalRead(11)==HIGH){
display.drawCircle(64,32,30);
// draw hr hand
display.drawLine(64,32,hrposx[hours],hrposy[hours]);
// draw hr minutes
display.drawLine(64,32,minposx[mins],minposy[mins]);
// draw hr seconds
display.drawLine(64,32,secposx[secs],secposy[secs]);
}
else if(digitalRead(12)==HIGH){
display.drawStr(25, 50, timeDisplay);
}
} while ( display.nextPage() );
delay(1000);
}