Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
169
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Dicky B_Mz https://dickybmz.wordpress.com/
  2. // Dicky B_Mz http://myblogdickybmz.blogspot.co.id/
  3. // Mikrokontroler Arduino ATmega328
  4. // RTC DS1307
  5. // 4 buah Seven Segment Common Anoda/Catodha
  6. // 3 buah tombol
  7. // Buzzer 5V
  8.  
  9.  
  10. #include "Wire.h"
  11. #include "RTClib.h"
  12. #include "EEPROM.h"
  13.  
  14. RTC_DS1307 RTC;
  15.  
  16. int hours;
  17. int hh;
  18. int mm;
  19. int ss;
  20. int timecc;
  21. int buzer = 30;
  22.  
  23. int segmentA = 6;
  24. int segmentB = 7;
  25. int segmentC = 8;
  26. int segmentD = 9;
  27. int segmentE = 10;
  28. int segmentF = 11;
  29. int segmentG = 12;
  30. int digitOne = A3; //A0
  31. int digitTwo = A2; //A1
  32. int digitThree = A1; //A3
  33. int digitFour = A0; //A2
  34. int hoursButton = 5;
  35. int minsButton = 3;
  36. int enter = 4;
  37. int pin=17;
  38. int relay=1;
  39. int ap=16;
  40.  
  41. byte value;
  42. byte value2;
  43. byte value3;
  44.  
  45. void setup() {
  46. pinMode(buzer,OUTPUT);
  47. pinMode(pin,OUTPUT);
  48. pinMode(relay,OUTPUT);
  49.  
  50. pinMode(segmentA, OUTPUT);
  51. pinMode(segmentB, OUTPUT);
  52. pinMode(segmentC, OUTPUT);
  53. pinMode(segmentD, OUTPUT);
  54. pinMode(segmentE, OUTPUT);
  55. pinMode(segmentF, OUTPUT);
  56. pinMode(segmentG, OUTPUT);
  57. pinMode(digitOne, OUTPUT);
  58. pinMode(digitTwo, OUTPUT);
  59. pinMode(digitThree, OUTPUT);
  60. pinMode(digitFour, OUTPUT);
  61.  
  62. pinMode(hoursButton, INPUT_PULLUP);
  63. pinMode(minsButton, INPUT_PULLUP);
  64. pinMode(enter, INPUT_PULLUP);
  65. pinMode(ap,OUTPUT);
  66.  
  67. Wire.begin();
  68. RTC.begin();
  69.  
  70.  
  71. if (! RTC.isrunning()) {
  72.  
  73. RTC.adjust(DateTime(__DATE__, __TIME__));
  74. }
  75.  
  76.  
  77.  
  78. Wire.beginTransmission(0X68);
  79. Wire.write(0x07);
  80. Wire.write(0x10);
  81. Wire.endTransmission();
  82.  
  83. digitalWrite(buzer,HIGH);
  84. delay(1000);
  85. digitalWrite(buzer,LOW);
  86. value = EEPROM.read(0);
  87. value2 = EEPROM.read(1);
  88. if(value>23){
  89. EEPROM.write(0, 0);
  90. }
  91. if(value2>59){
  92. EEPROM.write(1, 0);
  93. }
  94.  
  95. }
  96.  
  97. void loop() {
  98.  
  99. aman:
  100. value = EEPROM.read(0);
  101. value2 = EEPROM.read(1);
  102. value3 = EEPROM.read(2);
  103. DateTime now = RTC.now();
  104. hours = now.hour();
  105. mm = now.minute();
  106. ss = now.second();
  107. if(hours>23){
  108. digitalWrite(pin,HIGH);
  109. }
  110. else digitalWrite(pin,LOW);
  111.  
  112. hh = hours;
  113.  
  114. if (hours < 1) {
  115. hh = 00;
  116. }
  117.  
  118. if ((hours > 24) && (hours < 24)) {
  119. hh = hours - 24;
  120. }
  121.  
  122. timecc = hh*100+mm;
  123. showNum(timecc);
  124.  
  125. if (digitalRead(hoursButton) == LOW) {
  126. mati();
  127.  
  128. delay(200);
  129. hours++;
  130. ss = 00;
  131.  
  132. if (hours > 23) {
  133. hours = 00;
  134. }
  135.  
  136. updateRTC();
  137. }
  138.  
  139.  
  140. if (digitalRead(minsButton) == LOW) {
  141. mati();
  142. //debounce
  143. delay(200);
  144.  
  145. mm++;
  146. ss = 00;
  147.  
  148. if (mm >59) {
  149. mm = 00;
  150. }
  151.  
  152. updateRTC();
  153. }
  154.  
  155. if (digitalRead(enter) == LOW){
  156. mati();
  157. delay(200);
  158. if(digitalRead(enter)==LOW){
  159. if(value3==1){
  160. for(int a=0; a<500; a++){
  161. mati();
  162. digitalWrite(digitOne,LOW);
  163. digitalWrite(digitTwo,HIGH);
  164. digitalWrite(digitThree, LOW);
  165. digitalWrite(digitFour,LOW);
  166. digitalWrite(segmentA,LOW);
  167. digitalWrite(segmentB,LOW);
  168. digitalWrite(segmentC,LOW);
  169. digitalWrite(segmentD,LOW);
  170. digitalWrite(segmentE,LOW);
  171. digitalWrite(segmentF,LOW);
  172. digitalWrite(segmentG,HIGH);
  173. delay(4);
  174. mati();
  175. digitalWrite(digitOne,LOW);
  176. digitalWrite(digitTwo,LOW);
  177. digitalWrite(digitThree, HIGH);
  178. digitalWrite(digitFour,LOW);
  179. digitalWrite(segmentA,LOW);
  180. digitalWrite(segmentB,HIGH);
  181. digitalWrite(segmentC,HIGH);
  182. digitalWrite(segmentD,HIGH);
  183. digitalWrite(segmentE,LOW);
  184. digitalWrite(segmentF,LOW);
  185. digitalWrite(segmentG,LOW);
  186. delay(4);
  187. mati();
  188. digitalWrite(digitOne,LOW);
  189. digitalWrite(digitTwo,LOW);
  190. digitalWrite(digitThree, LOW);
  191. digitalWrite(digitFour,HIGH);
  192. digitalWrite(segmentA,LOW);
  193. digitalWrite(segmentB,HIGH);
  194. digitalWrite(segmentC,HIGH);
  195. digitalWrite(segmentD,HIGH);
  196. digitalWrite(segmentE,LOW);
  197. digitalWrite(segmentF,LOW);
  198. digitalWrite(segmentG,LOW);
  199. delay(4);
  200.  
  201. }
  202. EEPROM.write(2, 0);
  203. goto aman;
  204. }
  205. else{
  206. for(int a=0; a<500; a++){
  207. mati();
  208. digitalWrite(digitOne,LOW);
  209. digitalWrite(digitTwo,LOW);
  210. digitalWrite(digitThree, HIGH);
  211. digitalWrite(digitFour,LOW);
  212. digitalWrite(segmentA,LOW);
  213. digitalWrite(segmentB,LOW);
  214. digitalWrite(segmentC,LOW);
  215. digitalWrite(segmentD,LOW);
  216. digitalWrite(segmentE,LOW);
  217. digitalWrite(segmentF,LOW);
  218. digitalWrite(segmentG,HIGH);
  219. delay(4);
  220. mati();
  221. digitalWrite(digitOne,LOW);
  222. digitalWrite(digitTwo,LOW);
  223. digitalWrite(digitThree, LOW);
  224. digitalWrite(digitFour,HIGH);
  225. digitalWrite(segmentA,LOW);
  226. digitalWrite(segmentB,LOW);
  227. digitalWrite(segmentC,LOW);
  228. digitalWrite(segmentD,HIGH);
  229. digitalWrite(segmentE,LOW);
  230. digitalWrite(segmentF,LOW);
  231. digitalWrite(segmentG,HIGH);
  232. delay(4);
  233. }
  234. EEPROM.write(2, 1);
  235. goto aman;
  236. }
  237. }
  238. timecc=value*100+value2;
  239. while(digitalRead(enter)==HIGH){
  240. timecc=value*100+value2;
  241. showNum(timecc);
  242. if(digitalRead(hoursButton)==LOW){
  243. mati();
  244. delay(200);
  245. value++;
  246. if(value>23){
  247. value=00;
  248. }
  249. EEPROM.write(0, value);
  250. }
  251.  
  252. if(digitalRead(minsButton)==LOW){
  253. mati();
  254. delay(200);
  255. value2++;
  256. if(value2>59){
  257. value2=00;
  258. }
  259. EEPROM.write(1, value2);
  260. }
  261. }
  262. mati();
  263. delay(200);
  264. }
  265. if(EEPROM.read(2)==1){
  266.  
  267. if(value==hours&&value2==mm){
  268. for(int a=0;a<56;a++){
  269. showNum(timecc);
  270. if(a>0&&a<4){
  271. digitalWrite(buzer,HIGH);
  272. }
  273. else if(a>4&&a<8){
  274. digitalWrite(buzer,LOW);
  275. }
  276. else if(a>8&&a<12){
  277. digitalWrite(buzer,HIGH);
  278. }
  279. else if(a>12&&a<16){
  280. digitalWrite(buzer,LOW);
  281. }
  282. else if(a>16&&a<20){
  283. digitalWrite(buzer,HIGH);
  284. }
  285. else if(a>20&&a<24){
  286. digitalWrite(buzer,LOW);
  287. }
  288. else if(a>24&&a<28){
  289. digitalWrite(buzer,HIGH);
  290. }
  291. else if(a>28&&a<56){
  292. digitalWrite(buzer,LOW);
  293. }
  294. }
  295.  
  296. }
  297. }
  298.  
  299.  
  300. }
  301.  
  302.  
  303. void showNum(int n) {
  304.  
  305.  
  306. mati();
  307. int n1 = (n%10000)/1000;
  308. if (n > 999) {
  309. printNumber(n1,1);
  310. }
  311. delay(4);
  312. mati();
  313.  
  314. int n2 = (n%1000)/100;
  315. printNumber(n2,2);
  316. delay(4);
  317. mati();
  318.  
  319. int n3 = (n%100)/10;
  320. printNumber(n3,3);
  321. delay(4);
  322. mati();
  323.  
  324. int n4 = n%10;
  325. printNumber(n4,4);
  326. delay(3);
  327.  
  328. }
  329.  
  330. void printNumber(int n, int d) {
  331.  
  332.  
  333. if(d==1) {
  334. digitalWrite(digitOne,HIGH);
  335. digitalWrite(digitTwo,LOW);
  336. digitalWrite(digitThree,LOW);
  337. digitalWrite(digitFour,LOW);
  338.  
  339.  
  340. if ((hours == 12) || (hours == 22) || (hours == 23)) {
  341.  
  342. }
  343. else {
  344.  
  345. }
  346. }
  347.  
  348. if(d==2) {
  349. digitalWrite(digitOne,LOW);
  350. digitalWrite(digitTwo,HIGH);
  351. digitalWrite(digitThree, LOW);
  352. digitalWrite(digitFour,LOW);
  353.  
  354.  
  355. if ((hours > 12) && (hours < 22)) {
  356.  
  357. }
  358. else {
  359.  
  360. }
  361. }
  362.  
  363. if(d==3) {
  364. digitalWrite(digitOne,LOW);
  365. digitalWrite(digitTwo,LOW);
  366. digitalWrite(digitThree,HIGH);
  367. digitalWrite(digitFour,LOW);
  368.  
  369. }
  370.  
  371. if(d==4) {
  372. digitalWrite(digitOne,LOW);
  373. digitalWrite(digitTwo,LOW);
  374. digitalWrite(digitThree,LOW);
  375. digitalWrite(digitFour,HIGH);
  376.  
  377. }
  378.  
  379.  
  380. switch(n) {
  381. case 0:
  382. digitalWrite(segmentA,HIGH);
  383. digitalWrite(segmentB,HIGH);
  384. digitalWrite(segmentC,HIGH);
  385. digitalWrite(segmentD,HIGH);
  386. digitalWrite(segmentE,HIGH);
  387. digitalWrite(segmentF,HIGH);
  388. digitalWrite(segmentG,LOW);
  389. break;
  390.  
  391. case 1:
  392. digitalWrite(segmentA,LOW);
  393. digitalWrite(segmentB,HIGH);
  394. digitalWrite(segmentC,HIGH);
  395. digitalWrite(segmentD,LOW);
  396. digitalWrite(segmentE,LOW);
  397. digitalWrite(segmentF,LOW);
  398. digitalWrite(segmentG,LOW);
  399. break;
  400.  
  401. case 2:
  402. digitalWrite(segmentA,HIGH);
  403. digitalWrite(segmentB,HIGH);
  404. digitalWrite(segmentC,LOW);
  405. digitalWrite(segmentD,HIGH);
  406. digitalWrite(segmentE,HIGH);
  407. digitalWrite(segmentF,LOW);
  408. digitalWrite(segmentG,HIGH);
  409. break;
  410.  
  411. case 3:
  412. digitalWrite(segmentA,HIGH);
  413. digitalWrite(segmentB,HIGH);
  414. digitalWrite(segmentC,HIGH);
  415. digitalWrite(segmentD,HIGH);
  416. digitalWrite(segmentE,LOW);
  417. digitalWrite(segmentF,LOW);
  418. digitalWrite(segmentG,HIGH);
  419. break;
  420.  
  421. case 4:
  422. digitalWrite(segmentA,LOW);
  423. digitalWrite(segmentB,HIGH);
  424. digitalWrite(segmentC,HIGH);
  425. digitalWrite(segmentD,LOW);
  426. digitalWrite(segmentE,LOW);
  427. digitalWrite(segmentF,HIGH);
  428. digitalWrite(segmentG,HIGH);
  429. break;
  430.  
  431. case 5:
  432. digitalWrite(segmentA,HIGH);
  433. digitalWrite(segmentB,LOW);
  434. digitalWrite(segmentC,HIGH);
  435. digitalWrite(segmentD,HIGH);
  436. digitalWrite(segmentE,LOW);
  437. digitalWrite(segmentF,HIGH);
  438. digitalWrite(segmentG,HIGH);
  439. break;
  440.  
  441. case 6:
  442. digitalWrite(segmentA,HIGH);
  443. digitalWrite(segmentB,LOW);
  444. digitalWrite(segmentC,HIGH);
  445. digitalWrite(segmentD,HIGH);
  446. digitalWrite(segmentE,HIGH);
  447. digitalWrite(segmentF,HIGH);
  448. digitalWrite(segmentG,HIGH);
  449. break;
  450.  
  451. case 7:
  452. digitalWrite(segmentA,HIGH);
  453. digitalWrite(segmentB,HIGH);
  454. digitalWrite(segmentC,HIGH);
  455. digitalWrite(segmentD,LOW);
  456. digitalWrite(segmentE,LOW);
  457. digitalWrite(segmentF,LOW);
  458. digitalWrite(segmentG,LOW);
  459. break;
  460.  
  461. case 8:
  462. digitalWrite(segmentA,HIGH);
  463. digitalWrite(segmentB,HIGH);
  464. digitalWrite(segmentC,HIGH);
  465. digitalWrite(segmentD,HIGH);
  466. digitalWrite(segmentE,HIGH);
  467. digitalWrite(segmentF,HIGH);
  468. digitalWrite(segmentG,HIGH);
  469. break;
  470.  
  471. case 9:
  472. digitalWrite(segmentA,HIGH);
  473. digitalWrite(segmentB,HIGH);
  474. digitalWrite(segmentC,HIGH);
  475. digitalWrite(segmentD,HIGH);
  476. digitalWrite(segmentE,LOW);
  477. digitalWrite(segmentF,HIGH);
  478. digitalWrite(segmentG,HIGH);
  479. break;
  480. }
  481. }
  482.  
  483. byte decToBcd(byte val) {
  484. return ( (val/10*16) + (val%10) );
  485. }
  486.  
  487. byte bcdToDec(byte val) {
  488. return ( (val/16*10) + (val%16) );
  489. }
  490.  
  491. void updateRTC() {
  492. Wire.beginTransmission(104);
  493. Wire.write(0);
  494. Wire.write(decToBcd(ss));
  495. Wire.write(decToBcd(mm));
  496. Wire.write(decToBcd(hours));
  497. Wire.endTransmission();
  498. }
  499.  
  500. void mati(){
  501. digitalWrite(segmentA,HIGH);
  502. digitalWrite(segmentB,HIGH);
  503. digitalWrite(segmentC,HIGH);
  504. digitalWrite(segmentD,HIGH);
  505. digitalWrite(segmentE,HIGH);
  506. digitalWrite(segmentF,HIGH);
  507. digitalWrite(segmentG,HIGH);
  508. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement