#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
volatile int seconds, minutes, hours;
const int buttonPin = 2;
volatile int buttonState = 0;
//adjust values here, have to comment above as well
//volatile int seconds = 50;
//volatile int minutes = 2;
//volatile int hours = 1;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, CHANGE);
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
cli();//stop interrupts
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode (counter cleared to 0)
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
display.clearDisplay();
}
ISR(TIMER1_COMPA_vect){
seconds ++; //increment second counter
if(seconds == 60)
{
seconds = 0; //reset sec counter
minutes ++; //increment min counter
//Update minute counter
if(minutes == 60)
{
minutes = 0; //reset min counter
hours ++; //increment hr counter
//Update hour counter
if(hours == 24)
{
hours = 0; //reset hr counter
}
}
}
if (hours < 10) {
Serial.print("0"); // add leading zero for hours less than 10
}
Serial.print(hours);
Serial.print(":");
if (minutes < 10) {
Serial.print("0"); // add leading zero for minutes less than 10
}
Serial.print(minutes);
Serial.print(":");
if (seconds < 10) {
Serial.print("0"); // add leading zero for seconds less than 10
}
Serial.print(seconds);
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
display.clearDisplay();
display.setCursor(0,0);
if (hours < 10) {
display.print("0"); // add leading zero for hours less than 10
}
display.print(hours);
display.print(":");
if (minutes < 10) {
display.print("0"); // add leading zero for minutes less than 10
}
display.print(minutes);
display.print(":");
if (seconds < 10) {
display.print("0"); // add leading zero for seconds less than 10
}
display.print(seconds);
display.display();
}
void buttonInterrupt() {
buttonState = 1;
}