Created
May 18, 2012 20:46
-
-
Save mhauri/2727511 to your computer and use it in GitHub Desktop.
NTP synced RealTime Pomodoro Clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ntp_rtc_pomodoro | |
Needed Hardware | |
An Arduino or compatible board like: | |
http://arduino.cc/en/Main/ArduinoBoardEthernet | |
8 Digit Segment-Display with MAX7219 | |
http://www.play-zone.ch/de/8-zeichen-segment-display-mit-max7219.html | |
Adafruit DS1307 RTC Breakout Board - Kit | |
http://www.play-zone.ch/de/adafruit-ds1307-real-time-clock-breakout-board-kit.html | |
Created by: | |
05/17/2012 by Marcel Hauri, http://m83.ch | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <EthernetUdp.h> | |
#include <LedControl.h> | |
#include <Wire.h> | |
#include <RTClib.h> | |
// SDA: A5 | |
// SCL: A4 | |
RTC_DS1307 RTC; | |
// PIN 5: CS | |
// PIN 6: CLK | |
// PIN 9: DIN | |
LedControl lc=LedControl(9,6,5,1); | |
// Local MAC and IP Address | |
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0xEE, 0xA8 }; | |
IPAddress localIp(192,168,250,4); | |
// GMT | |
int gmt = 2; | |
// PIN's | |
const int ledPin = 8; | |
const int buzzerPin = 3; | |
// NTP IP (swisstime.ethz.ch) | |
IPAddress timeServer(129,132,2,21); | |
unsigned int localPort = 8888; | |
const int NTP_PACKET_SIZE = 48; | |
byte packetBuffer[NTP_PACKET_SIZE]; | |
EthernetUDP Udp; | |
EthernetClient client; | |
String currentLine = ""; | |
void setup() { | |
Wire.begin(); | |
RTC.begin(); | |
lc.shutdown(0,false); | |
lc.setIntensity(0,5); | |
lc.clearDisplay(0); | |
Serial.begin(9600); | |
setMessage(3,0); | |
if (!Ethernet.begin(mac)) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
setMessage(1, 1); | |
Ethernet.begin(mac, localIp); | |
} | |
pinMode(ledPin, OUTPUT); | |
if(Udp.begin(localPort)) { | |
setTime(); | |
} | |
} | |
void setSegment(int v, int a, int b) { | |
int ones; | |
int tens; | |
ones=v%10; | |
v=v/10; | |
tens=v%10; | |
lc.setDigit(0,a,(byte)ones,false); | |
lc.setDigit(0,b,(byte)tens,false); | |
} | |
void printTime() { | |
DateTime now = RTC.now(); | |
if(now.minute() == 25 || | |
now.minute() == 26 || | |
now.minute() == 27 || | |
now.minute() == 28 || | |
now.minute() == 29) { | |
int ts = now.minute() - 20; | |
int cts = ts; | |
if(ts == 6) { | |
cts = 4; | |
} else if(ts == 7) { | |
cts = 3; | |
} else if(ts == 8) { | |
cts = 2; | |
} else if(ts == 9) { | |
cts = 1; | |
} | |
digitalWrite(ledPin, HIGH); | |
setMessage(2,cts); | |
} else if(now.minute() == 55 || | |
now.minute() == 56 || | |
now.minute() == 57 || | |
now.minute() == 58 || | |
now.minute() == 59) { | |
int ts = now.minute() - 50; | |
int cts = ts; | |
if(ts == 6) { | |
cts = 4; | |
} else if(ts == 7) { | |
cts = 3; | |
} else if(ts == 8) { | |
cts = 2; | |
} else if(ts == 9) { | |
cts = 1; | |
} | |
digitalWrite(ledPin, HIGH); | |
setMessage(2,cts); | |
} else { | |
digitalWrite(ledPin, LOW); | |
setSegment(now.second(), 0, 1); | |
lc.setChar(0,2,'-',false); | |
setSegment(now.minute(), 3, 4); | |
lc.setChar(0,5,'-',false); | |
setSegment(now.hour(), 6, 7); | |
} | |
// set Time from NTP at Midnight | |
if(now.hour() == 00 && now.minute() == 00 && now.second() == 00) { | |
setTime(); | |
} | |
} | |
void setTime() { | |
sendNTPpacket(timeServer); | |
delay(1000); | |
if ( Udp.parsePacket() ) { | |
Udp.read(packetBuffer,NTP_PACKET_SIZE); | |
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); | |
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); | |
unsigned long secsSince1900 = highWord << 16 | lowWord; | |
const unsigned long seventyYears = 2208988800UL; | |
unsigned long epoch = (secsSince1900 - seventyYears) + (gmt*3600); | |
if(epoch > 0) { | |
RTC.adjust(epoch); | |
} | |
} else { | |
setMessage(1,2); | |
} | |
} | |
void setMessage(int m, int id) { | |
lc.clearDisplay(0); | |
if(m == 1) { | |
lc.setChar(0,7,'E',false); | |
lc.setRow(0,6,0x05); | |
lc.setRow(0,5,0x05); | |
lc.setRow(0,4,0x1D); | |
lc.setRow(0,3,0x05); | |
} | |
if(m == 2) { | |
lc.setRow(0,7,B01100111); // P | |
lc.setRow(0,6,B01110111); // A | |
lc.setRow(0,5,B00111110); // U | |
lc.setRow(0,4,B01011011); // S | |
lc.setRow(0,3,B01001111); // E | |
lc.setRow(0,2,B00000000); | |
lc.setRow(0,1,B00000000); | |
} | |
if(m == 3) { | |
lc.setRow(0,0,B00000001); | |
lc.setRow(0,1,B00000001); | |
lc.setRow(0,2,B00000001); | |
lc.setRow(0,3,B00000001); | |
lc.setRow(0,4,B00000001); | |
lc.setRow(0,5,B00000001); | |
lc.setRow(0,6,B00000001); | |
lc.setRow(0,7,B00000001); | |
} | |
if(id != 0) { | |
lc.setDigit(0,0,id,false); | |
} | |
} | |
// send an NTP request to the time server at the given address | |
unsigned long sendNTPpacket(IPAddress& address) { | |
memset(packetBuffer, 0, NTP_PACKET_SIZE); | |
packetBuffer[0] = 0b11100011; | |
packetBuffer[1] = 0; | |
packetBuffer[2] = 6; | |
packetBuffer[3] = 0xEC; | |
packetBuffer[12] = 49; | |
packetBuffer[13] = 0x4E; | |
packetBuffer[14] = 49; | |
packetBuffer[15] = 52; | |
Udp.beginPacket(address, 123); | |
Udp.write(packetBuffer,NTP_PACKET_SIZE); | |
Udp.endPacket(); | |
} | |
void loop() { | |
printTime(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment