{"id":140,"date":"2013-11-10T13:14:44","date_gmt":"2013-11-10T13:14:44","guid":{"rendered":"http:\/\/paulsarduino.co.uk\/?page_id=140"},"modified":"2013-11-10T13:14:44","modified_gmt":"2013-11-10T13:14:44","slug":"rtc-clock","status":"publish","type":"page","link":"https:\/\/paulsarduino.co.uk\/?page_id=140","title":{"rendered":"RTC Clock"},"content":{"rendered":"<pre>#include \r\n#include \"RTClib.h\"\r\n\r\nRTC_DS1307 RTC;\r\n\r\n\/*\r\n\r\nUses 3.5 digit display GL-3P402 without additional hardware - rely on POV for user to see time :)\r\n\r\nPinout:\r\n\r\n   D3   Se    Sb    Sd     Sc    D0  Sa\r\n   |    |      |     |      |    |    |\r\n\r\n     Dig3     Dig2       Dig1  Dot  Dig0 \r\n\r\n   |    |      |     |      |    |\r\n   Sf   D2    Sg    D1     Dot+  Dot-\r\n\r\nS = segment\r\nD = digit\r\n\r\nSegments:\r\n\r\n         aaa\r\n        b   c\r\n        b   c\r\n         ddd\r\n        e   f\r\n        e   f\r\n         ggg \r\n\r\nTinyRTC I2C module connected as follows: \r\n\r\nTinyRTC VCC - Arduino 5V\r\nTinyRTC GND - Arduino GND\r\nTinyRTC SDA - Arduino A4\r\nTinyRTC SCL - Arduino A5\r\n\r\n*\/\r\n\r\n\/\/ pins for selecting segments\r\nint   pinA = 4;\r\nint   pinB = 5;\r\nint   pinC = 6;\r\nint   pinD = 7;\r\nint   pinE = 8;\r\nint   pinF = 9;\r\nint   pinG = 10;\r\nint   pinDot = 13;\r\n\r\n\/\/ pins for selecting digits\r\nint   dig0 = 12;\r\nint   dig1 = 11;\r\nint   dig2 = 3;\r\nint   dig3 = 2;\r\nint   dot = 1;\r\n\r\n\/\/ init\r\nvoid setup()\r\n{\r\n  \/\/ init Wire library for I2C comms\r\n  Wire.begin();\r\n  \/\/ init Clock library\r\n  RTC.begin();\r\n\r\n  \/\/ if its not already running start it\r\n  if (! RTC.isrunning()) {\r\n    \/\/ reset the halt bit to make sure it runs\r\n    RTC.adjust(DateTime(0,0));\r\n    \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\r\n    RTC.adjust(DateTime(__DATE__, __TIME__));\r\n  }\r\n\r\n  \/\/ configure all as outputs - this is COMMON CATHODE display so inverse of what you expect\r\n  pinMode(pinA,OUTPUT);\r\n  pinMode(pinB,OUTPUT);\r\n  pinMode(pinC,OUTPUT);\r\n  pinMode(pinD,OUTPUT);\r\n  pinMode(pinE,OUTPUT);\r\n  pinMode(pinF,OUTPUT);\r\n  pinMode(pinG,OUTPUT);\r\n  pinMode(pinDot,OUTPUT);\r\n\r\n  \/\/ we use INPUT_PULLUP to isolate unselected digits by sinking the signals\r\n  pinMode(dig0,INPUT_PULLUP);\r\n  pinMode(dig1,INPUT_PULLUP);\r\n  pinMode(dig2,INPUT_PULLUP);\r\n  pinMode(dig3,INPUT_PULLUP);\r\n\r\n  \/\/ dot is independent - not multiplexed\r\n  pinMode(dot,OUTPUT);\r\n}\r\n\r\nvoid loop()\r\n{\r\n  \/\/ get the latest time\r\n  DateTime now = RTC.now();\r\n\r\n  int iM1 = now.minute() % 10;\r\n  int iM2 = now.minute() \/ 10;\r\n  int iH2 = now.hour() % 10;\r\n  int iH1 = now.hour() \/ 10;\r\n  int iS = now.second();\r\n\r\n  \/\/ enable one digit at a time and send the number to it\r\n  pinMode(dig0,OUTPUT);\r\n  DoNum(iM1);\r\n  pinMode(dig0,INPUT_PULLUP);\r\n\r\n  pinMode(dig1,OUTPUT);\r\n  DoNum(iM2);\r\n  pinMode(dig1,INPUT_PULLUP);\r\n\r\n  pinMode(dig2,OUTPUT);\r\n  DoNum(iH2);\r\n  pinMode(dig2,INPUT_PULLUP);\r\n\r\n  pinMode(dig3,OUTPUT);\r\n  if (iH1 == 0)\r\n  {\r\n    blank();\r\n  }\r\n  else\r\n  {\r\n    DoNum(iH1);\r\n  }\r\n  pinMode(dig3,INPUT_PULLUP);\r\n\r\n  digitalWrite(pinDot, (iS &amp; 1) ? LOW : HIGH);\r\n}\r\n\r\n\/\/ blank is no signals\r\nvoid blank()\r\n{\r\n  outNum(B00000000);\r\n}\r\n\r\n\/\/ output a number - send binary pattern in sequence to pins\r\n\/\/ each bit relates to one segment - top bit not used\r\nvoid DoNum(int i)\r\n{\r\n  switch(i)\r\n  {\r\n    case 0:\r\n      outNum(B01110111);\r\n      break;\r\n\r\n    case 1:\r\n      outNum(B00010010);\r\n      break;\r\n\r\n    case 2:\r\n      outNum(B01011101);\r\n      break;\r\n\r\n    case 3:\r\n      outNum(B01011011);\r\n      break;\r\n\r\n    case 4:\r\n      outNum(B00111010);\r\n      break;\r\n\r\n    case 5:\r\n      outNum(B01101011);\r\n      break;\r\n\r\n    case 6:\r\n      outNum(B01101111);\r\n      break;\r\n\r\n    case 7:\r\n      outNum(B01010010);\r\n      break;\r\n\r\n    case 8:\r\n      outNum(B01111111);\r\n      break;\r\n\r\n    case 9:\r\n      outNum(B01111011);;\r\n      break;\r\n  }\r\n}\r\n\r\nvoid outNum(byte bNum)\r\n{\r\n  \/\/ cycle through all pins and set to correct thing for pin\r\n  \/\/ delay if we turn it on so users eye can see it\r\n  int iSeg[7] = { pinA, pinB, pinC, pinD, pinE, pinF, pinG };\r\n  for (int i=0; i&lt;8; i++)\r\n  {\r\n    bNum = bNum &lt;&lt; 1;\r\n    if (bNum &amp; 128)\r\n    {\r\n      digitalWrite(iSeg[i], LOW);\r\n      delayMicroseconds(1600);\r\n    }\r\n    digitalWrite(iSeg[i], HIGH);\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include #include &#8220;RTClib.h&#8221; RTC_DS1307 RTC; \/* Uses 3.5 digit display GL-3P402 without additional hardware &#8211; rely on POV for user to see time :) Pinout: D3 Se Sb Sd Sc D0 Sa | | | | | | | Dig3 Dig2 Dig1 Dot Dig0 | | | | | | Sf D2 Sg D1 Dot+ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":16,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-140","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=140"}],"version-history":[{"count":1,"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/140\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/140\/revisions\/141"}],"up":[{"embeddable":true,"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/16"}],"wp:attachment":[{"href":"https:\/\/paulsarduino.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}