Skip to content

Instantly share code, notes, and snippets.

@jschoch
Created November 6, 2019 21:43
Show Gist options
  • Save jschoch/1788051355b8845404bcc27b7162cb79 to your computer and use it in GitHub Desktop.
Save jschoch/1788051355b8845404bcc27b7162cb79 to your computer and use it in GitHub Desktop.
clock
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 9
Adafruit_SSD1306 display(OLED_RESET);
#include <Encoder.h>
#include <DS3231M.h>
DS3231M_Class DS3231M; // Create an instance of the DS3231M
int bled = 13;
int o1pin = 5;
int o2pin = 4;
int btn1;
int btn2;
int btn3;
int enca = 3;
int encb = 2;
Encoder myEnc(enca, encb);
long oldPosition = -999;
long newPosition = 0;
int o1 = 0;
int o2 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(o2pin, INPUT);
pinMode(o1pin, INPUT);
//0x3c
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
display.display();
display.println("initializing clock");
while (!DS3231M.begin()) { // Initialize RTC communications
display.println("clock not found...");
display.display();
delay(1000);
} // of loop until device is located
digitalWrite(bled, HIGH);
delay(500);
// Clear the buffer.
display.clearDisplay();
// draw a single pixel
display.drawPixel(10, 10, WHITE);
// Show the display buffer on the hardware.
// NOTE: You _must_ call display after making any drawing commands
// to make them visible on the display hardware!
display.println("setup done");
display.display();
Serial.println("setup done");
}
DateTime now;
void drawState(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(o1);
display.print("-");
display.println(o2);
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
Serial.print(o1);
Serial.print("-");
Serial.println(o2);
}
display.println(newPosition);
now = DS3231M.now();
display.print(now.month());
display.print("/"); display.print(now.day());
display.print("/"); display.print(now.year());
display.print(" "); display.print(now.hour());
display.print(":"); display.print(now.minute());
display.print(" "); display.println(now.second());
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
newPosition = myEnc.read();
o1 = digitalRead(o1pin);
o2 = digitalRead(o2pin);
drawState();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment