Advertisement
RuiViana

Timer_GM

Aug 1st, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.88 KB | None | 0 0
  1. // Projeto estufa Rodrigo Araujo
  2.  
  3. #include <LiquidCrystal.h>                                      // incluindo o display lcd
  4. #include <string.h>
  5. #include <Wire.h>
  6. #include <DS1307.h>
  7. #include <avr/wdt.h>
  8. #include <EEPROM.h>
  9. #include <TimerOne.h>
  10.  
  11. char dateTime[20];
  12. int RTCValues[7];
  13. int acertahora[8];
  14. int programas[21];
  15.  
  16. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // definindo a pinagem do display lcd
  17. //int BackLight = 10;
  18. int delay_slide = 4000; // mudar para delay_menu
  19.  
  20. byte i = 0;
  21. byte p = 0;
  22. boolean m = 0;
  23.  
  24. boolean hsc = 0;
  25.  
  26. //logica dia semana
  27. byte ds = 0;
  28. byte diasemana = 0;
  29. //int programas[21];
  30. int linha = 0;
  31. byte secds = 0;
  32. //logica dia semana
  33.  
  34. //int d = 0;
  35. byte d1 = 0;
  36. byte menu = 0;
  37. byte estouro = 0;
  38. int Tecla = 1021;
  39.  
  40. /* Define valores dos botões */
  41. boolean RIGHT = 0;
  42. boolean UP = 0;
  43. boolean DOWN = 0;
  44. boolean LEFT = 0;
  45. boolean SELECT = 0;
  46. boolean NONE = 0;
  47.  
  48. // Definir dias da semana a ligar inicio
  49. //                DOM  SEG  TER  QUA  QUI  SEX  SAB  1=DOM  2=SEG  3=TER  4=QUA  5=QUI  6=SEX  7=SAB
  50. int dados[15][7] = {
  51.   { 1,   2,   3,   4,   5,   6,   7}, //DOM SEG  TER  QUA  QUI  SEX  SAB
  52.   { 1,   2,   3,   4,   5,   6,   0}, //DOM SEG  TER  QUA  QUI  SEX
  53.   { 0,   2,   3,   4,   5,   6,   7}, //SEG  TER  QUA  QUI  SEX SAB
  54.   { 1,   0,   0,   0,   0,   0,   0}, //DOM
  55.   { 0,   2,   0,   0,   0,   0,   0}, //SEG
  56.   { 0,   0,   3,   0,   0,   0,   0}, //TER
  57.   { 0,   0,   0,   4,   0,   0,   0}, //QUA
  58.   { 0,   0,   0,   0,   5,   0,   0}, //QUI
  59.   { 0,   0,   0,   0,   0,   6,   0}, //SEX
  60.   { 0,   0,   0,   0,   0,   0,   7}, //SAB
  61.   { 0,   2,   3,   4,   0,   0,   0}, //SEG TER QUAR
  62.   { 0,   0,   0,   0,   5,   6,   7}, //QUI SEX SAB ok
  63.   { 0,   2,   0,   4,   0,   6,   0}, //SEG QUA SEX  ok
  64.   { 0,   0,   3,   0,   5,   0,   7}, //TER QUIN SAB
  65.   { 1,   0,   0,   0,   0,   0,   7}
  66. }; //SAB DOM
  67.  
  68. // Definir dias da semana a ligar fim des semana
  69.  
  70. //--------------------------------------------------------
  71. void setup()
  72. {
  73.   pinMode(10, OUTPUT);
  74.   digitalWrite(10, HIGH);
  75.   lcd.begin(16, 2);
  76.   lcd.setCursor(0, 0);
  77.   lcd.print("Irrigacao Autom.");
  78.   delay(delay_slide);
  79.   lcd.clear();
  80.   delay(500);
  81.   //digitalWrite(10, LOW);
  82.   Serial.begin(9600);// initialize serial communications at 9600 bps
  83.   DS1307.begin();
  84.   Wire.begin();
  85.   Timer1.initialize(500000);
  86.   Timer1.attachInterrupt(interrup);
  87.   for (int x = 0; x < 21; x++) {
  88.     programas[x] = EEPROM.read(x); //grava eeprom posição 1
  89.     //Serial.print("Lendo numero da memoria programacao : ");
  90.     //Serial.println(programas[x]);
  91.   }
  92. }
  93. /***********  Lê os botões  **************/
  94. void LerTecla()
  95. {
  96.  
  97.   Tecla = analogRead(A0);
  98.   /***  Limites dos valores de cada botão  *****/
  99.   NONE = 0; RIGHT = 0; UP = 0; LEFT = 0; SELECT = 0;
  100.   if (Tecla < 1020)  // AS LINHAS QUE COMENTEI É PQ NÃO SÃO USADAS NO CODIGO ENTÃO NÃO TEM PORQUE PERDER TEMPO PROCESSANDO ESSES DADOS!
  101.   {
  102.     // if (Tecla <= 950 && Tecla >= 1100)  NONE = 1; // 1100 QUE VALOR É ESSE? A RESOLUÇÃO DO MICROCONTROLADOR SO VAI ATE 1023......
  103.     if (Tecla >= 0 && Tecla <= 20)      RIGHT = 1;
  104.     if (Tecla >= 50 && Tecla <= 200)    UP = 1;
  105.     if (Tecla >= 250 && Tecla <= 355)   DOWN = 1;
  106.     if (Tecla >= 450 && Tecla <= 550)   LEFT = 1;
  107.     if (Tecla >= 700 && Tecla <= 900)   SELECT = 1;
  108.     digitalWrite(10, HIGH);
  109.     delay(200);
  110.     Serial.println(Tecla);
  111.   }
  112. }
  113. //--------------------------------------------------------
  114. void interrup()
  115. {
  116.   d1 = d1++;
  117.   if (!menu == 0) {
  118.     estouro = estouro++;
  119.   }
  120.   if (!p == 0) {
  121.     estouro = estouro++;
  122.   }
  123.   if (!i == 0) {
  124.     estouro = estouro++;
  125.   }
  126.   if (!m == 0) {
  127.     estouro = estouro++; // era 1 e so m
  128.   }
  129.   if (!hsc == 0) {
  130.     estouro = estouro++; //logica dia semana
  131.   }
  132.   if (!ds == 0) {
  133.     estouro = estouro++; // logica dia semana
  134.   }
  135.   if (!diasemana == 0) {
  136.     estouro = estouro++;
  137.   }
  138. }
  139. //--------------------------------------------------------
  140. void menus()
  141. {
  142.   wdt_disable();
  143.   delay(500);
  144.   lcd.clear();
  145.   while (menu > 0)
  146.  
  147.   {
  148.     LerTecla();
  149.     lcd.setCursor(0, 0);
  150.  
  151.     if (menu == 1)
  152.     {
  153.       lcd.print("Acerta Data-Hora");  //alteração custela
  154.       if (SELECT)
  155.       {
  156.         i = 1;
  157.         menu = 0;
  158.         SELECT = 0;
  159.         acertartc();
  160.       }
  161.     }
  162.     if (menu == 2)
  163.     {
  164.       lcd.print("Dias Da Semana ");  //alteração custela
  165.       if (SELECT)
  166.       {
  167.         diasemana = 1;
  168.         menu = 0;
  169.         SELECT = 0;
  170.         diassemana();
  171.       }
  172.     }
  173.     if (UP && menu < 3)
  174.     {
  175.       menu++;  //alteração custela
  176.       estouro = 0;
  177.       UP = 0;
  178.     }
  179.     if (DOWN && menu > 0)
  180.     {
  181.       menu--;  //alteração custela
  182.       estouro = 0;
  183.       DOWN = 0;
  184.     }
  185.     if (estouro == 90)
  186.     {
  187.       menu = 0 ;
  188.       estouro = 0;
  189.     }
  190.   }
  191.   lcd.clear();
  192.   //digitalWrite(10, LOW);
  193. }
  194. //--------------------------------------------------------
  195. void diassemana()
  196.  
  197. {
  198.   wdt_disable(); // DESABILITA A FUNÇÃO WDT NESTA PARTE DO PROGRAMA.
  199.   delay(1000);
  200.   lcd.clear();
  201.   while ( diasemana > 0 )
  202.     //while( diasemana > 0 )
  203.   {
  204.     LerTecla();
  205.     lcd.setCursor(0, 0);
  206.  
  207.     if (diasemana == 1)
  208.     {
  209.       lcd.print(" Dias-Semana- 1 ");
  210.       linha = 0;
  211.       semana();
  212.     }
  213.     if (diasemana == 2)
  214.     {
  215.       lcd.print(" Dias-Semana- 2 ");
  216.       linha = 1;
  217.       semana();
  218.     }
  219.     if (diasemana == 3)
  220.     {
  221.       lcd.print(" Dias-Semana- 3 ");
  222.       linha = 2;
  223.       semana();
  224.     }
  225.     if (diasemana == 4)
  226.     {
  227.       lcd.print(" Dias-Semana- 4 ");
  228.       linha = 3;
  229.       semana();
  230.     }
  231.     if (diasemana == 5)
  232.     {
  233.       lcd.print(" Dias-Semana- 5 ");
  234.       linha = 4;
  235.       semana();
  236.     }
  237.     if (diasemana == 6)
  238.     {
  239.       lcd.print(" Dias-Semana- 6 ");
  240.       linha = 5;
  241.       semana();
  242.     }
  243.     if (diasemana == 7) {
  244.       lcd.print(" Dias-Semana- 7 ");
  245.       linha = 6;
  246.       semana();
  247.     }
  248.     if (diasemana == 8)
  249.     {
  250.       lcd.print(" Dias-Semana- 8 ");
  251.       linha = 7;
  252.       semana();
  253.     }
  254.     if (diasemana == 9)
  255.     {
  256.       lcd.print(" Dias-Semana- 9 ");
  257.       linha = 8;
  258.       semana();
  259.     }
  260.     if (diasemana == 10)
  261.     {
  262.       lcd.print(" Dias-Semana- 10");
  263.       linha = 9;
  264.       semana();
  265.     }
  266.     if (diasemana == 11)
  267.     {
  268.       lcd.print(" Dias-Semana- 11");
  269.       linha = 10;
  270.       semana();
  271.     }
  272.     if (diasemana == 12)
  273.     {
  274.       lcd.print(" Dias-Semana- 12");
  275.       linha = 11;
  276.       semana();
  277.     }
  278.     if (diasemana == 13)
  279.     {
  280.       lcd.print(" Dias-Semana- 13");
  281.       linha = 12;      
  282.       semana();
  283.     }
  284.     if (diasemana == 14)
  285.     {
  286.       lcd.print(" Dias-Semana- 14");
  287.       linha = 13;
  288.       semana();
  289.     }
  290.  
  291.     if (UP && diasemana < 15)
  292.     {
  293.       diasemana++;
  294.       estouro = 0;
  295.       UP = 0;
  296.     }
  297.     if (DOWN && diasemana > 0)
  298.     {
  299.       diasemana--;
  300.       estouro = 0;
  301.       DOWN = 0;
  302.     }
  303.     //if (SELECT){diasemana ++;estouro = 0; SELECT = 0;}
  304.     if (estouro == 90)
  305.     {
  306.       diasemana = 0 ;
  307.       estouro = 0;
  308.     }
  309.     delay(200);
  310.     //digitalWrite(10, LOW);
  311.  
  312.   }
  313.   lcd.clear();
  314. }
  315. //--------------------------------------------------------
  316. void mostrahora()
  317. {
  318.   DS1307.getDate(RTCValues);
  319.   sprintf(dateTime, "%02d:%02d:%02d%02d/%02d/%02d", RTCValues[4], RTCValues[5], RTCValues[6], RTCValues[2], RTCValues[1], RTCValues[0]);
  320.   delay(50);
  321.   lcd.setCursor(0, 0); lcd.print(dateTime);
  322.   delay(50);
  323. }
  324. //--------------------------------------------------------
  325. void informa()
  326. {
  327.   if (d1 <= 8)
  328.   {
  329.     lcd.setCursor(0, 1);
  330.     lcd.print("Irrigacao Autom.");
  331.   }
  332.   if (d1 >= 8 && d1 <= 10)
  333.   {
  334.     lcd.setCursor(0, 1);
  335.     lcd.print("                ");
  336.   }
  337.   if (d1 >= 11)
  338.   {
  339.     d1 = 0;
  340.   }
  341. }
  342. //--------------------------------------------------------
  343. void semana()
  344. {
  345.   lcd.setCursor(0, 1);
  346.   //for (int linha = 0; linha < 1; linha ++)
  347.   //for (int linha = 1; linha < 2; linha ++)
  348.   //for (int linha = 2; linha < 3; linha ++)
  349.   //for (int linha = 3; linha < 4; linha ++)
  350.   //for (int linha = 4; linha < 5; linha ++)
  351.   //for (int linha = 5; linha < 6; linha ++)
  352.   //for (int linha = 6; linha < 7; linha ++)
  353.   //for (int linha = 7; linha < 8; linha ++)
  354.   //for (int linha = 8; linha < 9; linha ++)
  355.   //for (int linha = 9; linha < 10; linha ++)
  356.   //for (int linha = 10; linha < 11; linha ++)
  357.   //for (int linha = 11; linha < 12; linha ++)
  358.   //for (int linha = 12; linha < 13; linha ++)
  359.   //for (int linha = 13; linha < 14; linha ++)
  360.   //for (int linha = 14; linha < 15; linha ++)
  361.   //  {
  362.   for (int coluna = 0; coluna <= 7; coluna++)
  363.   {
  364.     int month = (dados[linha][coluna]);
  365.     Serial.println(month);
  366.     switch (month) {
  367.  
  368.       case 0:  lcd.print("  ");
  369.         break;
  370.       case 1:  lcd.print("D ");
  371.         break;
  372.       case 2:  lcd.print("S ");
  373.         break;
  374.       case 3:  lcd.print("T ");
  375.         break;
  376.       case 4:  lcd.print("Q ");
  377.         break;
  378.       case 5:  lcd.print("Q ");
  379.         break;
  380.       case 6:  lcd.print("S ");
  381.         break;
  382.       case 7:  lcd.print("S ");
  383.         break;
  384.     }
  385.   }
  386.   // }
  387. }
  388. //--------------------------------------------------------
  389. void acertartc()
  390. {
  391.   wdt_disable(); // DESABILITA A FUNÇÃO WDT NESTA PARTE DO PROGRAMA.
  392.   delay(1000);
  393.   while ( i > 0 && i < 8)
  394.  
  395.   {
  396.     lcd.setCursor(0, 0);
  397.     LerTecla();
  398.     if (i == 1) {
  399.       lcd.print(" Entre com Ano  ");
  400.     }
  401.     if (i == 2) {
  402.       lcd.print(" Entre com Mes  ");
  403.     }
  404.     if (i == 3) {
  405.       lcd.print(" Entre com Dia  ");
  406.     }
  407.     if (i == 4) {
  408.       lcd.print(" Entre cm Semana");
  409.     }
  410.     if (i == 5) {
  411.       lcd.print(" Entre com Hora ");
  412.     }
  413.     if (i == 6) {
  414.       lcd.print(" Entre com Minut");
  415.     }
  416.     if (i == 7) {
  417.       lcd.print(" Entre com Segun");
  418.     }
  419.     if (UP) {
  420.       acertahora[i]++;
  421.       estouro = 0;
  422.       UP = 0;
  423.     }
  424.     if (DOWN && acertahora[i] >= 0) {
  425.       acertahora[i]--;
  426.       estouro = 0;
  427.       DOWN = 0;
  428.     }
  429.     if (SELECT) {
  430.       i ++; estouro = 0; SELECT = 0; lcd.clear();
  431.       delay(200);
  432.       //digitalWrite(10, LOW);
  433.     }
  434.  
  435.     if (acertahora[1] < 1 ) {
  436.       acertahora[1] = 13;
  437.     }
  438.     if (acertahora[2] > 12) {
  439.       acertahora[2] = 1;
  440.     }
  441.     if (acertahora[2] == 0) {
  442.       acertahora[2] = 12;
  443.     }
  444.     if (acertahora[3] > 31) {
  445.       acertahora[3] = 1;
  446.     }
  447.     if (acertahora[3] == 0) {
  448.       acertahora[3] = 31;
  449.     }
  450.     // 1 domigo 2 segunda 3 terca 4 quarta 5 quinta 6 sexta 7 sabado
  451.     if (acertahora[4] > 7) {
  452.       acertahora[4] = 1;
  453.       lcd.clear();
  454.     }
  455.     if (acertahora[4] == 0) {
  456.       acertahora[4] = 7;
  457.     }
  458.     if (acertahora[5] > 23) {
  459.       acertahora[5] = 0;
  460.     }
  461.     if (acertahora[5] == -1) {
  462.       acertahora[5] = 23;
  463.     }
  464.     if (acertahora[6] > 59) {
  465.       acertahora[6] = 0;
  466.     }
  467.     if (acertahora[6] == -1) {
  468.       acertahora[6] = 59;
  469.     }
  470.     if (acertahora[7] > 59) {
  471.       acertahora[7] = 0;
  472.     }
  473.     if (acertahora[7] == -1) {
  474.       acertahora[7] = 59;
  475.     }
  476.     if (i == 4) {
  477.       lcd.setCursor(2, 1);
  478.       lcd.print(fromNumberToWeekDay(acertahora[i], 2)); lcd.print("            ");
  479.     } //1=EN; 2=PT;);lcd.print("  ");}
  480.     else {
  481.       lcd.setCursor(8, 1);
  482.       lcd.print(acertahora[i], DEC); lcd.print("            ");
  483.     }
  484.     if (estouro == 90) {
  485.       i = 0 ;
  486.       estouro = 0;
  487.     }
  488.   }
  489.  
  490.   DS1307.setDate(acertahora[1], acertahora[2], acertahora[3], acertahora[4], acertahora[5], acertahora[6], acertahora[7]);
  491.   // AQUI ENVIAMOS OS DADOS DEFINIDOS PARA O REAL TIME CLOK
  492.   lcd.clear();
  493. }
  494.  
  495. //--------------------------------------------------------
  496. void loop()
  497. {
  498.   LerTecla();
  499.   if (SELECT)
  500.   {
  501.     menu = 1;
  502.     SELECT = 0;
  503.     menus();
  504.   }
  505.   //if(SELECT){diasemana =1; SELECT=0; diassemana();}
  506.   mostrahora();
  507.   informa();
  508.  
  509.   //  if(digitalRead(relay2)==HIGH && (deslbomba2/2) >= deslbomba){digitalWrite(relay2, LOW);deslbomba2 = 0;}
  510.   //Serial.print("Lendo pino conti");
  511.   //Serial.println(leitura);
  512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement