Advertisement
orneto

lab5

Jun 6th, 2023 (edited)
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | None | 0 0
  1.  
  2. #include "Wire.h"
  3. #include <LiquidCrystal.h>
  4.  
  5. unsigned long millisTarefa1 = millis();
  6. unsigned long millisTarefa2 = millis();
  7. unsigned long millisTarefa3 = millis();
  8. unsigned long millisTarefa4 = millis();
  9. #define DS1307_ADDRESS 0x68
  10. byte zero = 0x00;
  11. String horas,minutos,segundos,horasa;
  12.  
  13. //                                      LCD  
  14. #define RS PM_0
  15. #define RW PM_1
  16. #define E PM_2
  17. #define D0 PK_0
  18. #define D1 PK_1
  19. #define D2 PK_2
  20. #define D3 PK_3
  21. #define D4 PK_4
  22. #define D5 PK_5
  23. #define D6 PK_6
  24. #define D7 PK_7
  25. #define SW1 PJ_0
  26. //                                       LEDS;7SEG
  27. # define a PQ_0
  28. # define b PQ_1
  29. # define c PQ_2
  30. # define d PQ_3
  31. # define e PA_4
  32. # define f PA_5
  33. # define g PA_6
  34. # define ponto PA_7
  35. # define Ts1 PB_4
  36. # define Ts2 PB_5
  37. # define LEDPAT PP_5
  38. #define PH0 PH_0
  39. #define PH1 PH_1
  40. #define PH2 PH_2
  41. #define PH3 PH_3
  42.  
  43. bool estadoLED = false;
  44.  
  45. #define buzz PL_4
  46.  
  47. LiquidCrystal lcd(RS,RW,E,D0,D1,D2,D3,D4,D5,D6,D7);  
  48.  
  49. byte linhas[4] = {PL_0, PL_1, PL_2, PL_3};
  50. byte colunas[4] = {PM_4, PM_5, PM_6, PM_7};
  51.  
  52.  
  53. const int n = 6;
  54. int i,j,k=0;
  55.  
  56. int portasdisplay[8]={a,b,c,d,e,f,g,ponto};
  57. int setesegmentos[11][8]={{1,1,1,1,1,1,0,0},
  58.                           {0,1,1,0,0,0,0,0},
  59.                           {1,1,0,1,1,0,1,0},
  60.                           {1,1,1,1,0,0,1,0},
  61.                           {0,1,1,0,0,1,1,0},
  62.                           {1,0,1,1,0,1,1,0},
  63.                           {1,0,1,1,1,1,1,0},
  64.                           {1,1,1,0,0,0,0,0},
  65.                           {1,1,1,1,1,1,1,0},
  66.                           {1,1,1,1,0,1,1,0},
  67.                           {0,0,0,0,0,0,1,0}};
  68.  
  69.  
  70.  
  71. void setup() {
  72.       Serial.begin(9600);
  73.       for(i=0;i<8;i++)
  74.       {
  75.         pinMode(portasdisplay[i],OUTPUT);
  76.       }
  77.       pinMode(Ts1,OUTPUT);
  78.       pinMode(Ts2,OUTPUT);
  79.       pinMode(LEDPAT,OUTPUT);
  80.       pinMode(buzz, OUTPUT);
  81.       lcd.begin(16, 2);
  82.       lcd.clear();
  83.       lcd.setCursor(0,0);
  84.       lcd.print("Defina os");
  85.       lcd.clear();
  86.       Wire.begin();
  87.       //setDateTime(); // caso queira ajustar a hora
  88.      
  89. }
  90. byte decToBcd(byte val){
  91.   return ( (val/10*16) + (val%10) );
  92. }
  93. byte bcdToDec(byte val)  {
  94.   return ( (val/16*10) + (val%16) );
  95. }
  96.  
  97. void setDateTime(){
  98.   byte segundo = 30;
  99.   byte minuto = 7;
  100.   byte hora = 14;
  101.   byte diasemana = 3;
  102.   byte dia = 06;
  103.   byte mes = 06;
  104.   byte ano = 23;
  105.   Wire.beginTransmission(DS1307_ADDRESS);
  106.   Wire.write(zero);
  107.   Wire.write(decToBcd(segundo));
  108.   Wire.write(decToBcd(minuto));
  109.   Wire.write(decToBcd(hora));
  110.   Wire.write(zero);
  111.   Wire.endTransmission();
  112. }
  113. bool PMbr;
  114. bool PMal;
  115.  
  116. int printahora(bool modo){
  117.   Wire.beginTransmission(DS1307_ADDRESS);
  118.   Wire.write(zero);
  119.   Wire.endTransmission();
  120.   Wire.requestFrom(DS1307_ADDRESS, 7);
  121.   int segundo = bcdToDec(Wire.read());
  122.   int minuto = bcdToDec(Wire.read());
  123.   int hora = bcdToDec(Wire.read() & 0b111111);    //Formato 24 horas
  124.    if (hora <= 12) {
  125.       PMbr = false;
  126.    }
  127.    if (hora > 12) {
  128.       PMbr = true;
  129.    }
  130.   int diasemana = bcdToDec(Wire.read());             //0-6 -> Domingo - Sábado
  131.   int dia = bcdToDec(Wire.read());
  132.   int mes = bcdToDec(Wire.read());
  133.   int ano = bcdToDec(Wire.read());
  134.   int horaa = hora+4;
  135.    if (horaa <= 12) {
  136.       PMal = false;
  137.    }
  138.    if (horaa > 12) {
  139.       PMal = true;
  140.    }
  141.   int resto = 0;
  142.   int converte=0;
  143.  
  144.   if(horaa>23){
  145.     resto = horaa - 24;
  146.     horaa = resto;}
  147.  // Serial.print(segundo);
  148.   //Serial.print(minuto);
  149.   //Serial.print(hora);
  150. //Exibe a data e hora. Ex.:   3/12/13 19:00:00
  151.   if(modo==1 && PMbr==1){
  152.     hora=hora-12;
  153.   }
  154.   if(modo==1 && PMal==1){
  155.     horaa=horaa-12;
  156.   }
  157.   if(hora<10)
  158.     horas="0"+String(hora);
  159.   else
  160.     horas=String(hora);
  161.     if(horaa<10)
  162.     horasa="0"+String(horaa);
  163.   else
  164.     horasa=String(horaa);
  165.   if(minuto<10)
  166.     minutos="0"+String(minuto);
  167.   else
  168.     minutos=String(minuto);
  169.   if(segundo<10)
  170.     segundos="0"+String(segundo);
  171.   else
  172.     segundos=String(segundo);
  173.  
  174.   lcd.setCursor(0,0);
  175.   lcd.print(horas);
  176.   lcd.print(":");
  177.   lcd.print(minutos);
  178.   lcd.print(":");
  179.   lcd.println(segundos);
  180.   lcd.setCursor(8,0);
  181.  
  182.   if(modo==1 && PMbr==1){
  183.     lcd.print(" PM BRA ");
  184.   }
  185.   if(modo==1 && PMbr==0){
  186.     lcd.print(" AM BRA ");
  187.   }
  188.   if(modo ==0){
  189.       lcd.print(" BRA     ");
  190.   }
  191.  
  192.   lcd.setCursor(0,1);
  193.   lcd.print(horasa);
  194.   lcd.print(":");
  195.   lcd.print(minutos);
  196.   lcd.print(":");
  197.   lcd.print(segundos);
  198.   lcd.setCursor(8,1);
  199.   if(modo==1 && PMal==1){
  200.     lcd.print(" PM BER ");
  201.   }
  202.   if(modo==1 && PMal==0){
  203.     lcd.print(" AM BER ");
  204.   }
  205.   if(modo ==0){
  206.       lcd.print(" BER     ");
  207.   }
  208.   return minuto;
  209. }
  210.  
  211.  
  212. void acendetodos(){
  213.   digitalWrite(a,HIGH);
  214.   digitalWrite(b,HIGH);
  215.   digitalWrite(c,HIGH);
  216.   digitalWrite(d,HIGH);
  217.   digitalWrite(e,HIGH);
  218.   digitalWrite(f,HIGH);
  219.   digitalWrite(g,HIGH);
  220.   digitalWrite(ponto,HIGH);
  221. }
  222.   void apagatodos(){
  223.   digitalWrite(a,LOW);
  224.   digitalWrite(b,LOW);
  225.   digitalWrite(c,LOW);
  226.   digitalWrite(d,LOW);
  227.   digitalWrite(e,LOW);
  228.   digitalWrite(f,LOW);
  229.   digitalWrite(g,LOW);
  230.   digitalWrite(ponto,LOW);
  231. }
  232.  
  233. int alarme(){
  234.    int i=0;
  235.    lcd.clear();  
  236.    lcd.setCursor(0, 0);
  237.    lcd.print("!!!alarme!!!");
  238.    while(i<5){    
  239.     if((millis() - millisTarefa4) < 500){
  240.       digitalWrite(LEDPAT, HIGH);
  241.       acendetodos();
  242.       tone(buzz,3000);}
  243.      
  244.     else{
  245.       digitalWrite(LEDPAT, LOW);
  246.       apagatodos();
  247.       noTone (buzz);
  248.     }
  249.     if((millis() - millisTarefa4) > 1000){
  250.         millisTarefa4 = millis();
  251.         i=i+1;
  252.     }
  253.    }
  254.    digitalWrite(LEDPAT, LOW);
  255.    apagatodos();
  256.    lcd.clear();
  257.    return 0;
  258. }
  259.  
  260. int trocaturno(bool modo){
  261.       int i = 0;
  262.       int x = 11;
  263.       int k=0;
  264.       while(x>0){
  265.         printahora(modo);
  266.         if((millis()-millisTarefa3)<1000){
  267.             if((millis() - millisTarefa2) < 10){
  268.               j= x / 10;
  269.               for(k=0;k<8;k++){
  270.                  digitalWrite(portasdisplay[k],setesegmentos[j][k]);
  271.               }
  272.               digitalWrite(Ts1,HIGH);              
  273.               digitalWrite(Ts2,LOW);
  274.             }
  275.             else{
  276.               j = x % 10;
  277.               for(k=0;k<8;k++){
  278.                  digitalWrite(portasdisplay[k],setesegmentos[j][k]);
  279.               }
  280.               digitalWrite(Ts2,HIGH);              
  281.               digitalWrite(Ts1,LOW);          
  282.             }
  283.             if(millis()-millisTarefa2>20){
  284.               millisTarefa2 = millis();
  285.             }
  286.             if((millis() - millisTarefa1) < 500){
  287.                 tone(buzz,6000);}
  288.      
  289.             else{
  290.                 noTone (buzz);
  291.             }
  292.             if((millis() - millisTarefa1) > 1000){
  293.                 millisTarefa1 = millis();
  294.             }          
  295.         }
  296.         else{
  297.           millisTarefa3=millis();
  298.           x=x-1;
  299.         }
  300.       }
  301.       digitalWrite(Ts1,LOW);              
  302.       digitalWrite(Ts2,LOW);  
  303.       return 1;
  304. }
  305. int on=1; //sistema ligado
  306. int minuto;
  307. bool modo=0;
  308. bool emergencia=false;
  309.  
  310. void loop() {
  311.     lcd.setCursor(0, 0);
  312.     char tcld = Serial.read();
  313.     lcd.clear();
  314.     switch(tcld){
  315.               case 'A':case 'a':
  316.                 on= 0;
  317.                 lcd.clear();
  318.                 lcd.clear();
  319.                 break;
  320.               case 'B':case 'b':    
  321.                 lcd.clear();            
  322.                 on= 1;
  323.                 break;
  324.               case 'C':case 'c':
  325.                 break;
  326.             }
  327.      while(on==1){
  328.  
  329.           tcld = Serial.read();
  330.           switch(tcld){
  331.             case 'A':case 'a':
  332.               lcd.clear();
  333.               lcd.clear();
  334.               on= 0;
  335.               break;
  336.             case 'B':case 'b':
  337.               on= 1;
  338.               break;
  339.             case 'C':case 'c':
  340.               modo=!modo;
  341.               break;
  342.            }    
  343.           minuto=printahora(modo);      
  344.           if(((minuto % 2) == 0) && segundos == "00"){
  345.             trocaturno(modo);
  346.           }
  347.           if(emergencia==true){
  348.             on=alarme();
  349.           }
  350.       }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement