Skip to content

Instantly share code, notes, and snippets.

@goran-mahovlic
Created September 19, 2016 19:08
Show Gist options
  • Save goran-mahovlic/77e6754d295db45303ca30d02cd87484 to your computer and use it in GitHub Desktop.
Save goran-mahovlic/77e6754d295db45303ca30d02cd87484 to your computer and use it in GitHub Desktop.
#include "SFE_MicroOLED.h"
#define VIBRATE 7
#define BUTTON 4
Timer t;
boolean useAlarm = true;
int alarmHours = 7;
int alarmMinutes = 0;
SPI my_spi(P0_29, NC, P0_30);
MicroOLED my_oled(my_spi, P0_1, P0_0, P0_2);
const int CLOCK_SPEED = 1000;
// Use these variables to set the initial time
int hours = (__TIME__[0] - '0') * 10 + ( __TIME__[1] - '0') ;
int minutes = (__TIME__[3] - '0') * 10 + ( __TIME__[4] - '0') ;
int seconds = (__TIME__[7] - '0') * 10 + ( __TIME__[7] - '0') ;
// Simple function to increment seconds and then increment minutes
// and hours if necessary.
void updateTime()
{
seconds++; // Increment seconds
if (seconds >= 60) // If seconds overflows (>=60)
{
seconds = 0; // Set seconds back to 0
minutes++; // Increment minutes
if (minutes >= 60) // If minutes overflows (>=60)
{
minutes = 0; // Set minutes back to 0
hours++; // Increment hours
if (hours >= 24) // If hours overflows (>=12)
{
hours = 0; // Set hours back to 0
}
}
}
}
void setup() {
// put your setup code here, to run once:
my_oled.init(0, 8000000);
my_oled.setFontType(0);
my_oled.clear(PAGE);
my_oled.setCursor(0, 0);
my_oled.puts("Its Alive");
my_oled.display();
pinMode(VIBRATE, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
t.start();
while (t.read_ms() < 2000)
{
}
t.reset();
}
void loop()
{
if (useAlarm && hours == alarmHours && minutes == alarmMinutes) // not good this way, we will lose few seconds of clock when alarm starts becouse delays
{
digitalWrite(VIBRATE, HIGH);
delay(1000);
digitalWrite(VIBRATE, LOW);
delay(1000);
}
char txtBuf[9];
my_oled.command(DISPLAYOFF);
// Check if we need to update seconds, minutes, hours:
if (t.read_ms() >= CLOCK_SPEED)
{
updateTime(); // Add a second, update minutes/hours if necessary
while (!digitalRead(BUTTON)) //not good this way, every time we hold button time will not flow...
// turn display only if button is hold to get more working time on battery
{
my_oled.command(DISPLAYON);
my_oled.clear(PAGE); // Clear the frame buffer
sprintf(txtBuf, "%02d:%02d:%02d", hours, minutes, seconds);
my_oled.setCursor(5, 5);
my_oled.puts(txtBuf);
my_oled.setCursor(9, 20);
my_oled.puts("Smarty");
my_oled.display(); // Draw the memory buffer
}
t.reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment