CaReCoiN

Reloj con calculadora de semana

Apr 15th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <virtuabotixRTC.h>    
  4. int num;
  5. char* diaSem[7]={"Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa"};
  6. char* amPM[2]={" AM", " PM"};
  7. byte posA;
  8. byte posB;
  9. int wA, wB, wC, wD, wE, wF, wG, wN, wS, numSem;
  10.  
  11. //Inciando el RTC y el LCD
  12. virtuabotixRTC RTC(6, 7, 8);
  13. LiquidCrystal_I2C lcd(0x27, 16, 2);
  14.  
  15. void setup()  {      
  16.   lcd.begin();
  17.   lcd.backlight();
  18.  
  19. //RTC.setDS1302Time(00, 39, 23, 5, 15, 4, 2016); //Ajuste inicial del reloj ss:mm:H L dd/mm/aaaa
  20.  
  21. //Iniciar los pines de ajuste
  22. pinMode(9, INPUT);
  23. pinMode(10, INPUT);
  24. pinMode(11, INPUT);
  25. }
  26.  
  27. void loop()  {    
  28.   RTC.updateTime();
  29.   imprimirSemana();
  30.   imprimirDMA();
  31.   imprimirDiaSem();
  32.   imprimirHora();
  33.   verificarAjuste();
  34. }                                                                                                                                                                            
  35.  
  36. void imprimirSemana(){
  37.   if (RTC.month < 3) calculoSemEneMar();
  38.   else calculoSemAbrDic();
  39.   calculoSem();
  40.   lcd.setCursor(1, 0);
  41.   lcd.print("W");
  42.   lcd.print(numSem);
  43. }
  44.  
  45. void imprimirDMA(){
  46.   num = RTC.dayofmonth;
  47.   lcd.setCursor(5, 0);
  48.   if (num < 10){
  49.     lcd.print("0");
  50.     lcd.print(num);
  51.     }
  52.   else lcd.print(num);
  53.   lcd.print("/");
  54.   num = RTC.month;
  55.   if (num < 10){
  56.     lcd.print("0");
  57.     lcd.print(num);
  58.     }
  59.   else lcd.print(num);
  60.   lcd.print("/");
  61.   num = RTC.year;
  62.   lcd.print(num);
  63.  }
  64.  
  65. void imprimirDiaSem(){
  66.   lcd.setCursor(1, 1);
  67.   lcd.print(diaSem[RTC.dayofweek]);
  68.   lcd.print(" ");  
  69. }
  70.  
  71. void imprimirHora(){
  72.   lcd.setCursor(4, 1);
  73.   num = RTC.hours;
  74.   if (num > 12) num = num - 12;
  75.   if (num == 0) num = 12;
  76.   if (num < 10) lcd.print("0");
  77.   lcd.print(num);
  78.   lcd.print(":");
  79.   num = RTC.minutes;
  80.   if (num < 10) lcd.print("0");
  81.   lcd.print(num);
  82.   lcd.print(":");
  83.   num = RTC.seconds;
  84.   if (num < 10) lcd.print("0");
  85.   lcd.print(num);
  86.   if (RTC.hours < 12) num = 0;
  87.   if (RTC.hours > 11) num = 1;
  88.   lcd.print(amPM[num]);
  89. }
  90.  
  91. void calculoSemEneMar(){
  92.   wA = RTC.year;
  93.   wB = ((wA/4) - (wA/100) + (wA/400));
  94.   wC = (((wA-1)/4) - ((wA-1)/100) + ((wA-1)/400));
  95.   wS = wB - wC;
  96.   wE = 0;
  97.   wF = ((RTC.dayofmonth) - 1 + (31 * (RTC.month - 1)));
  98. }
  99.  
  100. void calculoSemAbrDic(){
  101.   wA = RTC.year;
  102.   wB = ((wA/4) - (wA/100) + (wA/400));
  103.   wC = (((wA-1)/4) - ((wA-1)/100) + ((wA-1)/400));
  104.   wS = wB - wC;
  105.   wE = wS + 1;
  106.   wF = ((RTC.dayofmonth) + ((153 * (RTC.month - 3) + 2) / 5) + 58 + wS);
  107. }
  108.  
  109. void calculoSem(){
  110.   wG = ((wA + wB) % 7);
  111.   wD = ((wF + wG - wE) % 7);
  112.   wN = wF + 3 - wD;
  113.   num = 364 + wS;
  114.   if (wN < 0) numSem = (53 - ((wG - wS) / 5));
  115.   else if (wN > num) numSem = 1;
  116.   else numSem = ((wN / 7) + 1);  
  117. }
  118.  
  119. void verificarAjuste(){
  120.    if (digitalRead(9) == HIGH) {
  121.        obtenerVariables();
  122.        num = RTC.seconds + 1;
  123.        if (num == 60) num = 0;
  124.        RTC.setDS1302Time(num, wB, wC, wD, wE, wF, wG); //Ajuste inicial del reloj ss:mm:H L dd/mm/aaaa
  125.    }
  126.    if (digitalRead(10) == HIGH) {
  127.        obtenerVariables();
  128.        num = RTC.minutes + 1;
  129.        if (num == 60) num = 0;
  130.        RTC.setDS1302Time(wA, num, wC, wD, wE, wF, wG); //Ajuste inicial del reloj ss:mm:H L dd/mm/aaaa
  131.    }
  132.    if (digitalRead(11) == HIGH) {
  133.        obtenerVariables();
  134.        num = RTC.hours + 1;
  135.        if (num == 24) num = 0;
  136.        RTC.setDS1302Time(wA, wB, num, wD, wE, wF, wG); //Ajuste inicial del reloj ss:mm:H L dd/mm/aaaa
  137.    limpiarPantalla();
  138.    }
  139. }
  140.  
  141. void obtenerVariables(){
  142.   wA = RTC.seconds;
  143.   wB = RTC.minutes;
  144.   wC = RTC.hours;
  145.   wD = RTC.dayofweek;
  146.   wE = RTC.dayofmonth;
  147.   wF = RTC.month;
  148.   wG = RTC.year;
  149. }
  150.  
  151. void limpiarPantalla(){
  152.   lcd.setCursor(0, 0);
  153.   lcd.print("                ");
  154.   lcd.print("                ");
  155. }
Advertisement
Add Comment
Please, Sign In to add comment