Guest User

Skull Clock 2018

a guest
Jan 1st, 2019
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.93 KB | None | 0 0
  1. // Skull Clock 2018
  2.  
  3. //This code is for personal and hobby use only. Commercial use and sale of items using this code
  4. //is expressly forbidden
  5.  
  6. //Code is originally heavily based on Wordclockproject by Reddit user Wordclock
  7. //Original post by him here: https://imgur.com/a/sMtUi#ZDGCskd
  8. //His code to be found here: http://pastebin.com/fwnuVQ5z
  9.  
  10. //Also inspired by Reddit User u/themakermonster
  11. //His post found here https://www.reddit.com/r/DIY/comments/95aj3s/how_i_turned_a_skull_into_a_clock/
  12.  
  13. //Major modifications and improved features in this project include
  14.  
  15. //TIMEZONE CHANGE Ability - for BST (UK Currently) but easily adjustable for any potential timezone
  16. //using the excellent Timezone library linked below. Just copy and paste your own timezone
  17. //from the library and change ''local = UK.toLocal(utc, &tcr);'' to the correct timezone
  18. //abbreviation.
  19.  
  20. //AUTO EYE CALIBRATION - Original skull clock used stepper motors and kept time poorly, This project
  21. //uses S125 servos which can be set regardless of previous power cuts etc.
  22.  
  23. //MOVING JAW using a third servo to open and close a spring loaded jaw.
  24.  
  25. #include <DS3232RTC.h> //http://www.arduino.cc/playground/Code/Time
  26. #include <Time.h> //http://www.arduino.cc/playground/Code/Time
  27. #include <Timezone.h> //https://github.com/JChristensen/Timezone
  28. #include <Wire.h> //http://arduino.cc/en/Reference/Wire (supplied with the Arduino IDE)
  29. #include <TimeLib.h>
  30. #include <Streaming.h>
  31. #include <Servo.h>
  32.  
  33. unsigned long seconds = 1000L;
  34. unsigned long minutes = seconds * 60;
  35. unsigned long hours = minutes * 60;
  36.  
  37. //United Kingdom (London, Belfast)
  38. TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60}; //British Summer Time
  39. TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 0}; //Standard Time
  40. Timezone UK(BST, GMT);
  41.  
  42. //If TimeChangeRules are already stored in EEPROM, comment out the three
  43. //lines above and uncomment the line below.
  44. //Timezone myTZ(100); //assumes rules stored at EEPROM address 100
  45.  
  46. TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
  47. time_t utc, local;
  48.  
  49. Servo hourservo, minuteservo, jawservo; //create servos for attachment later
  50.  
  51. int pos = 0; //variable to store servo position
  52.  
  53. void setup(void)
  54. {
  55. Serial.begin(115200);
  56.  
  57. setSyncProvider(RTC.get); // the function to get the time from the RTC
  58. if(timeStatus()!= timeSet)
  59. Serial.println("Unable to sync with the RTC");
  60. else
  61. Serial.println("RTC has set the system time");
  62. }
  63.  
  64. void loop(void)
  65. {
  66. Serial.println();
  67. utc = now();
  68. printTime(utc, "UTC");
  69. local = UK.toLocal(utc, &tcr);
  70. printTime(local, tcr -> abbrev);
  71. delay(1000);
  72. static time_t tLast;
  73. time_t t;
  74. tmElements_t tm;
  75.  
  76. //check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
  77. if (Serial.available() >= 12) {
  78. //note that the tmElements_t Year member is an offset from 1970,
  79. //but the RTC wants the last two digits of the calendar year.
  80. //use the convenience macros from Time.h to do the conversions.
  81. int y = Serial.parseInt();
  82. if (y >= 100 && y < 1000)
  83. Serial << F("Error: Year must be two digits or four digits!") << endl;
  84. else {
  85. if (y >= 1000)
  86. tm.Year = CalendarYrToTm(y);
  87. else //(y < 100)
  88. tm.Year = y2kYearToTm(y);
  89. tm.Month = Serial.parseInt();
  90. tm.Day = Serial.parseInt();
  91. tm.Hour = Serial.parseInt();
  92. tm.Minute = Serial.parseInt();
  93. tm.Second = Serial.parseInt();
  94. t = makeTime(tm);
  95. RTC.set(t); //use the time_t value to ensure correct weekday is set
  96. setTime(t);
  97. Serial << F("RTC set to: ");
  98. printTime(t, local);
  99. Serial << endl;
  100. //dump any extraneous input
  101. while (Serial.available() > 0) Serial.read();
  102. }
  103. }
  104.  
  105. t = now();
  106. if (t != tLast) {
  107. tLast = t;
  108. if (second(t) == 0) {
  109. float c = RTC.temperature() / 4.;
  110. float f = c * 9. / 5. + 32.;
  111. Serial << F(" ") << c << F(" C ") << f << F(" F");
  112. }
  113. displayeyes(local);
  114.  
  115.  
  116. }
  117. }
  118.  
  119. void displayeyes(time_t local) {
  120.  
  121. if(minute(local) < 5) {
  122. showOnHour();
  123. } else if(minute(local) < 10) {
  124. showFivexxx();
  125. } else if(minute(local) < 15) {
  126. showTen();
  127. } else if(minute(local) < 20) {
  128. showQuarter();
  129. } else if(minute(local) < 25) {
  130. showTwenty();
  131. } else if(minute(local) < 30) {
  132. showTwentyFive();
  133. } else if(minute(local) < 35) {
  134. showHalfxxx();
  135. } else if(minute(local) < 40) {
  136. showThirtyFive();
  137. } else if(minute(local) < 45) {
  138. showForty();
  139. } else if(minute(local) < 50) {
  140. showFortyFive();
  141. } else if(minute(local) < 55) {
  142. showFifty();
  143. } else {
  144. showFiftyFive();
  145. }
  146.  
  147. if(hour(local) == 0 || hour(local) == 12) {
  148. showHourTwelve();
  149. }
  150. else if(hour(local) == 1 || hour(local) == 13) {
  151. showHourOne();
  152. }
  153. else if(hour(local) == 2 || hour(local) == 14) {
  154. showHourTwo();
  155. }
  156. else if(hour(local) == 3 || hour(local) == 15) {
  157. showHourThree();
  158. }
  159. else if(hour(local) == 4 || hour(local) == 16) {
  160. showHourFour();
  161. }
  162. else if(hour(local) == 5 || hour(local) == 17) {
  163. showHourFive();
  164. }
  165. else if(hour(local) == 6 || hour(local) == 18) {
  166. showHourSix();
  167. }
  168. else if(hour(local) == 7 || hour(local) == 19) {
  169. showHourSeven();
  170. }
  171. else if(hour(local) == 8 || hour(local) == 20) {
  172. showHourEight();
  173. }
  174. else if(hour(local) == 9 || hour(local) == 21) {
  175. showHourNine();
  176. }
  177. else if(hour(local) == 10 || hour(local) == 22) {
  178. showHourTen();
  179. }
  180. else if(hour(local) == 11 || hour(local) == 23) {
  181. showHourEleven();
  182. }
  183. else {
  184. showHourTwelve();
  185. }
  186.  
  187. if(hour(local) == 8 || hour(local) == 20 && minute(local)==0) {
  188. openjaw();
  189. openjaw();
  190. openjaw();
  191. openjaw();
  192. openjaw();
  193. openjaw();
  194. openjaw();
  195. openjaw();
  196. delay(1 * minutes);
  197. }
  198. if(hour(local) == 9 || hour(local) == 21 && minute(local)==0) {
  199. openjaw();
  200. openjaw();
  201. openjaw();
  202. openjaw();
  203. openjaw();
  204. openjaw();
  205. openjaw();
  206. openjaw();
  207. openjaw();
  208. delay(1 * minutes);
  209. }
  210. if(hour(local) == 10 || hour(local) == 22 && minute(local)==0) {
  211. openjaw();
  212. openjaw();
  213. openjaw();
  214. openjaw();
  215. openjaw();
  216. openjaw();
  217. openjaw();
  218. openjaw();
  219. openjaw();
  220. openjaw();
  221. delay(1 * minutes);
  222. }
  223. if(hour(local) == 11 && minute(local)==0) {
  224. openjaw();
  225. openjaw();
  226. openjaw();
  227. openjaw();
  228. openjaw();
  229. openjaw();
  230. openjaw();
  231. openjaw();
  232. openjaw();
  233. openjaw();
  234. openjaw();
  235. delay(1 * minutes);
  236. }
  237. if(hour(local) == 12 && minute(local)==0) {
  238. openjaw();
  239. openjaw();
  240. openjaw();
  241. openjaw();
  242. openjaw();
  243. openjaw();
  244. openjaw();
  245. openjaw();
  246. openjaw();
  247. openjaw();
  248. openjaw();
  249. openjaw();
  250. delay(1 * minutes);
  251. }
  252. if(hour(local) == 13 && minute(local)==0) {
  253. openjaw();
  254. delay(1 * minutes);
  255. }
  256. if(hour(local) == 14 && minute(local)==0) {
  257. openjaw();
  258. openjaw();
  259. delay(1 * minutes);
  260. }
  261. if(hour(local) == 15 && minute(local)==0) {
  262. openjaw();
  263. openjaw();
  264. openjaw();
  265. delay(1 * minutes);
  266. }
  267. if(hour(local) == 16 && minute(local)==0) {
  268. openjaw();
  269. openjaw();
  270. openjaw();
  271. openjaw();
  272. delay(1 * minutes);
  273. }
  274. if(hour(local) == 17 && minute(local)==0) {
  275. openjaw();
  276. openjaw();
  277. openjaw();
  278. openjaw();
  279. openjaw();
  280. delay(1 * minutes);
  281. }
  282. if(hour(local) == 18 && minute(local)==0) {
  283. openjaw();
  284. openjaw();
  285. openjaw();
  286. openjaw();
  287. openjaw();
  288. openjaw();
  289. delay(1 * minutes);
  290. }
  291. if(hour(local) == 19 && minute(local)==0) {
  292. openjaw();
  293. openjaw();
  294. openjaw();
  295. openjaw();
  296. openjaw();
  297. openjaw();
  298. openjaw();
  299. delay(1 * minutes);
  300. }
  301.  
  302. }
  303.  
  304.  
  305. void showOnHour() {
  306. minuteservo.attach(9);
  307. minuteservo.writeMicroseconds(2000);
  308. delay(2000);
  309. minuteservo.detach();
  310. }
  311.  
  312. void showFivexxx() {
  313. minuteservo.attach(9);
  314. minuteservo.writeMicroseconds(1950);
  315. delay(2000);
  316. minuteservo.detach();
  317. }
  318.  
  319. void showTen() {
  320. minuteservo.attach(9);
  321. minuteservo.writeMicroseconds(1890);
  322. delay(2000);
  323. minuteservo.detach();
  324. }
  325.  
  326. void showQuarter() {
  327. minuteservo.attach(9);
  328. minuteservo.writeMicroseconds(1815);
  329. delay(2000);
  330. minuteservo.detach();
  331. }
  332.  
  333. void showTwenty() {
  334. minuteservo.attach(9);
  335. minuteservo.writeMicroseconds(1737);
  336. delay(2000);
  337. minuteservo.detach();
  338. }
  339.  
  340. void showTwentyFive() {
  341. minuteservo.attach(9);
  342. minuteservo.writeMicroseconds(1670);
  343. delay(2000);
  344. minuteservo.detach();
  345. }
  346.  
  347. void showHalfxxx() {
  348. minuteservo.attach(9);
  349. minuteservo.writeMicroseconds(1570);
  350. delay(2000);
  351. minuteservo.detach();
  352. }
  353.  
  354. void showThirtyFive() {
  355. minuteservo.attach(9);
  356. minuteservo.writeMicroseconds(1485);
  357. delay(2000);
  358. minuteservo.detach();
  359. }
  360.  
  361. void showForty() {
  362. minuteservo.attach(9);
  363. minuteservo.writeMicroseconds(1395);
  364. delay(2000);
  365. minuteservo.detach();
  366. }
  367.  
  368. void showFortyFive() {
  369. minuteservo.attach(9);
  370. minuteservo.writeMicroseconds(1310);
  371. delay(2000);
  372. minuteservo.detach();
  373. }
  374.  
  375. void showFifty() {
  376. minuteservo.attach(9);
  377. minuteservo.writeMicroseconds(1220);
  378. delay(2000);
  379. minuteservo.detach();
  380. }
  381.  
  382. void showFiftyFive() {
  383. minuteservo.attach(9);
  384. minuteservo.writeMicroseconds(1120);
  385. delay(2000);
  386. minuteservo.detach();
  387. }
  388.  
  389. void showHourOne() {
  390. hourservo.attach(10);
  391. hourservo.writeMicroseconds(1950);
  392. delay(2000);
  393. hourservo.detach();
  394. }
  395.  
  396. void showHourTwo() {
  397. hourservo.attach(10);
  398. hourservo.writeMicroseconds(1890);
  399. delay(2000);
  400. hourservo.detach();
  401. }
  402.  
  403. void showHourThree() {
  404. hourservo.attach(10);
  405. hourservo.writeMicroseconds(1800);
  406. delay(2000);
  407. hourservo.detach();
  408. }
  409.  
  410. void showHourFour() {
  411. hourservo.attach(10);
  412. hourservo.writeMicroseconds(1737);
  413. delay(2000);
  414. hourservo.detach();
  415. }
  416.  
  417. void showHourFive() {
  418. hourservo.attach(10);
  419. hourservo.writeMicroseconds(1670);
  420. delay(2000);
  421. hourservo.detach();
  422. }
  423.  
  424. void showHourSix() {
  425. hourservo.attach(10);
  426. hourservo.writeMicroseconds(1570);
  427. delay(2000);
  428. hourservo.detach();
  429. }
  430.  
  431. void showHourSeven() {
  432. hourservo.attach(10);
  433. hourservo.writeMicroseconds(1485);
  434. delay(2000);
  435. hourservo.detach();
  436. }
  437.  
  438. void showHourEight() {
  439. hourservo.attach(10);
  440. hourservo.writeMicroseconds(1400);
  441. delay(2000);
  442. hourservo.detach();
  443. }
  444.  
  445. void showHourNine() {
  446. hourservo.attach(10);
  447. hourservo.writeMicroseconds(1310);
  448. delay(2000);
  449. hourservo.detach();
  450. }
  451.  
  452. void showHourTen() {
  453. hourservo.attach(10);
  454. hourservo.writeMicroseconds(1220);
  455. delay(2000);
  456. hourservo.detach();
  457. }
  458.  
  459. void showHourEleven() {
  460. hourservo.attach(10);
  461. hourservo.writeMicroseconds(1150);
  462. delay(2000);
  463. hourservo.detach();
  464. }
  465.  
  466. void showHourTwelve() {
  467. hourservo.attach(10);
  468. hourservo.writeMicroseconds(2000);
  469. delay(2000);
  470. hourservo.detach();
  471. }
  472.  
  473. void openjaw() {
  474. jawservo.attach(11);
  475. jawservo.writeMicroseconds(1350);
  476. delay(1500);
  477. jawservo.writeMicroseconds(1060);
  478. delay(1500);
  479. }
  480.  
  481. //Function to print time with time zone
  482. void printTime(time_t t, char *tz)
  483. {
  484. sPrintI00(hour(t));
  485. sPrintDigits(minute(t));
  486. sPrintDigits(second(t));
  487. Serial.print(' ');
  488. Serial.print(dayShortStr(weekday(t)));
  489. Serial.print(' ');
  490. sPrintI00(day(t));
  491. Serial.print(' ');
  492. Serial.print(monthShortStr(month(t)));
  493. Serial.print(' ');
  494. Serial.print(year(t));
  495. Serial.print(' ');
  496. Serial.print(tz);
  497. Serial.println();
  498. }
  499.  
  500. //Print an integer in "00" format (with leading zero).
  501. //Input value assumed to be between 0 and 99.
  502. void sPrintI00(int val)
  503. {
  504. if (val < 10) Serial.print('0');
  505. Serial.print(val, DEC);
  506. return;
  507. }
  508.  
  509. //Print an integer in ":00" format (with leading zero).
  510. //Input value assumed to be between 0 and 99.
  511. void sPrintDigits(int val)
  512. {
  513. Serial.print(':');
  514. if(val < 10) Serial.print('0');
  515. Serial.print(val, DEC);
  516. }
Add Comment
Please, Sign In to add comment