Advertisement
RuiViana

JO_Com_Tempo

Sep 16th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. #include "TimerOne.h"
  2. #define RELE1 2
  3. #define RELE2 3
  4. #define RELE3 4
  5. #define RELE4 5
  6. #define RELE5 6
  7. #define RELE6 7
  8. String comando;
  9. unsigned long tempo1 = 0;                       // Contagem de tempo1
  10. unsigned long tempo2 = 0;                       // Contagem de tempo2
  11. unsigned long tempo3 = 0;                       // Contagem de tempo3
  12. unsigned long tempo4 = 0;                       // Contagem de tempo4
  13. byte FlagA = 0;                                 // Flag para informa que tecla A foi digitada
  14. byte FlagE = 0;                                 // Flag para informa que tecla E foi digitada
  15. byte Fase = 0;                                  // Flag para a fase da Letra E
  16. //-----------------------------
  17. void setup()
  18. {
  19.   Serial.begin(9600);
  20.   pinMode(RELE1, OUTPUT);
  21.   pinMode(RELE2, OUTPUT);
  22.   pinMode(RELE3, OUTPUT);
  23.   pinMode(RELE4, OUTPUT);
  24.   pinMode(RELE5, OUTPUT);
  25.   pinMode(RELE6, OUTPUT);
  26.   digitalWrite(RELE1, LOW);
  27.   digitalWrite(RELE2, LOW);
  28.   digitalWrite(RELE3, LOW);
  29.   digitalWrite(RELE4, LOW);
  30.   digitalWrite(RELE5, LOW);
  31.   digitalWrite(RELE6, LOW);
  32.   Timer1.initialize(1000000);                   // initialize timer1, and set a 1 second period
  33.   Timer1.attachInterrupt(callback);             // attaches callback() as a timer overflow interrupt
  34. }
  35. //--------------------------------------
  36. void callback()                                 // Roda se deu 1 interrupt de Segundo
  37. {
  38.   tempo1++;                                     // incrementa 1 segundo em tempo1
  39.   tempo2++;                                     // incrementa 1 segundo em tempo2
  40.   tempo3++;                                     // incrementa 1 segundo em tempo3
  41.   tempo4++;                                     // incrementa 1 segundo em tempo4
  42. }
  43. //----------------------------
  44. void loop()
  45. {
  46.   while (Serial.available())
  47.   {
  48.     delay(10);
  49.     char  c = Serial.read();
  50.     comando += c;
  51.   }
  52.   if (comando.length() > 0)
  53.   {
  54.     //-------------------------
  55.     if (comando == "A")
  56.     {
  57.       digitalWrite(RELE1, HIGH);
  58.       FlagA = 1;                                // Indicador teclaA
  59.       tempo1 = 0;                               // Zera tempo1
  60.     }
  61.     //-------------------------
  62.     if (comando == "B")
  63.     {
  64.       digitalWrite(RELE2, HIGH);
  65.     }
  66.     else if (comando == "b")
  67.     {
  68.       digitalWrite(RELE2, LOW);
  69.     }
  70.     //-------------------------
  71.     if (comando == "C")
  72.     {
  73.       digitalWrite(RELE3, HIGH);
  74.     }
  75.     else if (comando == "c")
  76.     {
  77.       digitalWrite(RELE3, LOW);
  78.     }
  79.     //-------------------------
  80.     if (comando == "D")
  81.     {
  82.       digitalWrite(RELE4, HIGH);
  83.     }
  84.     else if (comando == "d")
  85.     {
  86.       digitalWrite(RELE4, LOW);
  87.     }
  88.     //-------------------------
  89.     if (comando == "E")
  90.     {
  91.       digitalWrite(RELE5, HIGH);
  92.       Fase = 1;                                 // Selciona fase1
  93.       FlagE = 1;                                // Indicador teclaE
  94.       tempo2 = 0;                               // Zera tempo2
  95.       tempo3 = 0;                               // Zera tempo3
  96.       tempo4 = 0;                               // Zera tempo4
  97.     }
  98.     else if (comando == "e")
  99.     {
  100.       digitalWrite(RELE5, LOW);
  101.     }
  102.     //-------------------------
  103.     if (comando == "F")
  104.     {
  105.       digitalWrite(RELE6, HIGH);
  106.     }
  107.     else if (comando == "f")
  108.     {
  109.       digitalWrite(RELE6, LOW);
  110.     }
  111.     //-------------------------
  112.     comando = "";
  113.   }
  114.   if (FlagA == 1)                               // Se tem Indicador teclaA
  115.   {
  116.  
  117.     if (tempo1 >= 180)                          // Se tempo1 chegou em 3 minutos
  118.     {
  119.       digitalWrite(RELE1, LOW);
  120.       FlagA = 0;                                // Indicador teclaA = 0
  121.     }
  122.   }
  123.   //--------------------------
  124.   if (FlagE == 1)                               // Se tem Indicador teclaE
  125.   {
  126.     switch (Fase)                               // Selciona fase
  127.     {
  128.       case 1:                                   // Fase 1
  129.         if (tempo2 >= 5)                        // Se tempo2 chegou em 5 minutos
  130.         {
  131.           digitalWrite(RELE5, LOW);
  132.           Fase = 2;                             // Muda para fase2 da Letra E
  133.           tempo3 = 0;                           // Zera tempo3
  134.         }
  135.         break;
  136.       case 2:
  137.         if (tempo3 >= 5)                        // Se tempo2 chegou em 5 minutos
  138.         {
  139.           digitalWrite(RELE5, HIGH);
  140.           Fase = 3;                             // Muda para fase3 da Letra E
  141.           tempo4 = 0;                           // Zera tempo4
  142.         }
  143.         break;
  144.       case 3:
  145.         if (tempo4 >= 5)                        // Se tempo2 chegou em 5 minutos
  146.         {
  147.           digitalWrite(RELE5, LOW);
  148.           Fase = 0;                             // Zera fase
  149.           FlagE = 0;                            // Zera Indicador teclaE
  150.         }
  151.         break;
  152.     }
  153.   }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement