Sebuahhobi98

timer

Apr 15th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <bitBangedSPI.h>
  3. #include <MAX7219_Dot_Matrix.h>
  4. #include <Wire.h>
  5. #include "RTClib.h"
  6. #define Y now.year()
  7. #define M now.month()
  8. #define D now.day()
  9. #define HD daysOfTheWeek[now.dayOfTheWeek()]
  10. #define H now.hour()
  11. #define m now.minute()
  12. #define s now.second()
  13. #define bHours 7
  14. int hours=0;
  15. const byte chips = 8;
  16. // 12 chips (display modules), hardware SPI with load on D10
  17. MAX7219_Dot_Matrix display (chips, 10);  // Chips / LOAD
  18. char message [100];
  19. char waktu [100];
  20. char tgl [100];
  21. /*5v Arduino —- VCC Dot Matrix
  22. GND Arduino —- GND Dot Matrix
  23. 13 Arduino —– CLK Dot Matrix
  24. 10 Arduino —– CS Dot Matrix
  25. 11 Arduino —– DIN Dot Matrix*/
  26.  
  27. #if defined(ARDUINO_ARCH_SAMD)
  28. // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  29.    #define Serial SerialUSB
  30. #endif
  31. RTC_DS3231 rtc;
  32.  
  33. char daysOfTheWeek[7][12] = {"Ahad", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
  34.  
  35.  
  36. void setup (){
  37.   display.begin ();
  38.   #ifndef ESP8266
  39.     while (!Serial); // for Leonardo/Micro/Zero
  40.   #endif
  41.   Serial.begin(57600);
  42.   if (! rtc.begin()) {
  43.     Serial.println("Couldn’t find RTC");
  44.     while (1);
  45.   }
  46.   if (rtc.lostPower()) {
  47.     Serial.println("RTC is NOT running!");
  48.     // following line sets the RTC to the date & time this sketch was compiled
  49.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  50.     // This line sets the RTC with an explicit date & time, for example to set
  51.     // January 21, 2014 at 3am you would call:
  52.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  53.   }
  54.   pinMode(bHours,INPUT_PULLUP);
  55. } //END Set Up
  56.  
  57.  
  58. unsigned long lastMoved = 0;
  59. unsigned long MOVE_INTERVAL = 10;  // mS
  60. unsigned long lastMoved1 = 0;
  61. unsigned long MOVE_INTERVAL1 = 1;  // mS
  62.  
  63. int  messageOffset;
  64. int  messageOffset1;
  65. void updateDisplay (){
  66.   DateTime now = rtc.now();
  67.   sprintf (message, "Jual Pulsa");
  68.   display.sendSmooth (message, messageOffset);
  69.  
  70.   //sprintf (tgl, "%d-%d-%d",D,M,Y); //tgl-bulan-tahun
  71.   // next time show one pixel onwards
  72.   if (messageOffset++ >= (int) (strlen (message) * 8))
  73.     messageOffset = - chips * 8;
  74.   }  // end of updateDisplay
  75. void updateJam (){
  76.   DateTime now = rtc.now();
  77.   int h=now.hour();
  78.   int stateHours = digitalRead(bHours);
  79.  
  80.   if(stateHours==LOW){
  81.     hours=hours+1;
  82.     h=h+1;
  83.     Serial.println(h);
  84.   }
  85.   sprintf (waktu, "%d:%d:%d", h,m,s); //waktu
  86.   if(h<10)
  87.     sprintf (waktu, "0%d:%d:%d", h,m,s); //ubah jam
  88.    
  89.   if(m<10)
  90.     sprintf (waktu, "%d:0%d:%d", h,m,s);
  91.   if(s<10)
  92.     sprintf (waktu, "%d:%d:0%d", h,m,s);
  93.    
  94.   if(m<10 && h<10 && s<10)
  95.     sprintf (waktu, "0%d:0%d:0%d", h,m,s);
  96.   else if(m<10 && h<10)
  97.     sprintf (waktu, "0%d:0%d:%d", h,m,s);
  98.   else if(m<10 && s<10)
  99.     sprintf (waktu, "%d:0%d:0%d", h,m,s);
  100.   else if(h<10 && s<10)
  101.     sprintf (waktu, "0%d:%d:0%d", h,m,s);
  102.  
  103.     display.sendString(waktu);
  104.  
  105. }  // end of updateJam
  106. int state=0;
  107. void loop (){
  108.  
  109.   // update display if time is up
  110.   if(millis() - lastMoved >= MOVE_INTERVAL && state==0){
  111.     updateJam();
  112.     //lastMoved = millis ();
  113.     state=0;
  114.   }else if(millis() - lastMoved >= MOVE_INTERVAL1 && state==1){
  115.     //updateJam();
  116.     updateDisplay ();
  117.     lastMoved = millis();
  118.     state=0;
  119.   }/**/
  120. }  // end of loop
Add Comment
Please, Sign In to add comment