Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3. #include <SPI.h>             //to SD library
  4. #include <SD.h>              //to SD library
  5. #include <LiquidCrystal.h>   //to Display library
  6. #include <Wire.h>            //to BMP180 library
  7. #include <Adafruit_BMP085.h> //to BMP180 library
  8.  
  9. #define delayBounce 80
  10. #define pinBotao 7 //Define o Botão A na porta 7. Botão de troca de menu
  11. #define pinLED1 13 //Led que indica a posiçao de troca e alerta
  12. #define pinLED2 A8
  13. #define buzzer 8
  14. #define chipSelect 10  //Posicion of pin cs from shield SD
  15.  
  16. Adafruit_BMP085 bmp180;
  17. SoftwareSerial serial1(A3, A2); // RX, TX
  18. LiquidCrystal lcd(9, 6, 5, 4, 3, 2);
  19. TinyGPS gps1;
  20.  
  21.  
  22. int ANO;
  23. byte MES, DIA, HORA, MIN;
  24. double Lat, Lon;
  25. bool Botao; //  Botao
  26. bool estadoAntBotao;
  27. bool estadoBotao;
  28. short contador = 1;
  29. bool imprime;
  30. float temperatura, pressao;
  31. unsigned long delay1 = 0;   //Contador para o intervalo de tempo
  32. unsigned long delay2 = 0;   //Contador para o intervalo de tempo
  33. unsigned long delay3 = 0;   //Contador para o intervalo de tempo
  34.  
  35. void setup() {
  36.  
  37.    pinMode(pinLED1, OUTPUT);
  38.    pinMode(pinBotao, INPUT_PULLUP);
  39.    
  40.    serial1.begin(9600);
  41.    Serial.begin(230400);
  42.    lcd.begin(16, 2);
  43.    bmp180.begin();
  44.    
  45.    Serial.println("Aguardando satélites...");
  46.  
  47.  
  48. //------------ TESTE DO CARTÃO -----------------
  49.    if (!SD.begin(chipSelect)) {
  50.      Serial.println("Problema no cartao");
  51.      return;
  52.      }
  53.      Serial.println("Cartão inicializado");
  54.  
  55.  /*for (int n=0; n<3; n++){
  56.     digitalWrite(buzzer, HIGH);   // turn the LED on (HIGH is the voltage level)
  57.     delay(100);                       // wait for a second
  58.     digitalWrite(buzzer, LOW);    // turn the LED off by making the voltage LOW
  59.     delay(500);
  60.     }
  61.     lcd.clear();  
  62.     lcd.setCursor(0, 0);
  63.     lcd.print("Tracker Meteo V2");
  64.     lcd.setCursor(5, 1);
  65.     lcd.print("Maza");
  66.     delay(1500);
  67.     lcd.setCursor(0, 0);
  68.     lcd.print("    Buscando    ");
  69.     lcd.setCursor(0, 1);
  70.     lcd.print("    Satelites   ");
  71.     Serial.println("Buscando Satelites");
  72.     delay(1500);
  73.     lcd.clear();
  74.     */
  75. }
  76.  
  77. void loop() {
  78.  
  79.   String dataString = "";
  80.  
  81.   bool recebido = false;
  82.   while (serial1.available()) {
  83.      char cIn = serial1.read();
  84.      recebido = gps1.encode(cIn);
  85.   }
  86.  
  87.   if (recebido) {
  88.      Serial.println("----------------------------------------");
  89.      
  90.      //Latitude e Longitude
  91.      long latitude, longitude;
  92.      gps1.get_position(&latitude, &longitude);    
  93.  
  94.      if (latitude != TinyGPS::GPS_INVALID_F_ANGLE) {
  95.         Serial.print("Latitude dentro do if recebido: ");
  96.         Lat = float(latitude) / 100000, 6;
  97.         Serial.println(Lat);
  98.         dataString += String(float(latitude) / 100000, 6) + ", ";
  99.      }
  100.  
  101.      if (longitude != TinyGPS::GPS_INVALID_F_ANGLE) {
  102.         Serial.print("Longitude dentro do if recebido: ");
  103.         Lon = float(longitude) / 100000, 6;
  104.         Serial.println(Lon);
  105.      }
  106.  
  107.  
  108.      //Dia e Hora
  109.      int ano;
  110.      byte mes, dia, hora, minuto, segundo, centesimo;
  111.      gps1.crack_datetime(&ano, &mes, &dia, &hora, &minuto, &segundo, &centesimo);
  112.      DIA = dia;
  113.      MES = mes;
  114.      ANO = ano;
  115.      HORA = hora;
  116.      MIN = minuto;
  117.      
  118.      Serial.print("Data (GMT) dentro do if: ");
  119.      Serial.print(dia);
  120.      Serial.print("/");
  121.      Serial.print(mes);
  122.      Serial.print("/");
  123.      Serial.println(ano);
  124.  
  125.      Serial.print("Horario (GMT) dentro do if: ");
  126.      Serial.print(hora);
  127.      Serial.print(":");
  128.      Serial.print(minuto);
  129.      Serial.print(":");
  130.      Serial.print(segundo);
  131.      Serial.print(":");
  132.      Serial.println(centesimo);
  133.  
  134.   imprime = true;
  135.   temperatura = bmp180.readTemperature();
  136.   pressao = bmp180.readPressure();
  137.  
  138.   }
  139.  
  140.   if (imprime){
  141.   Serial.println("____________________________________________");
  142.   Serial.print("Latitude fora do if recebido: ");
  143.   Serial.println(Lat, 6);
  144.  
  145.   Serial.print("Longitude fora do if recebido: ");
  146.   Serial.println(Lon, 6);
  147.  
  148.   Serial.print("Data (GMT) FORA do if: ");
  149.   Serial.print(DIA);
  150.   Serial.print("/");
  151.   Serial.print(MES);
  152.   Serial.print("/");
  153.   Serial.println(ANO);
  154.   Serial.println(contador);
  155.   Serial.print("Temperatura: ");
  156.   Serial.println(temperatura);
  157.   Serial.print("Pressão: ");
  158.   Serial.println(pressao);
  159.   //Serial.println(contador);
  160.  
  161.   dataString += String(Lat, 6) + ", ";
  162.   dataString += String(Lon, 6) + ", ";
  163.   dataString += String(DIA) + "/";
  164.   dataString += String(MES) + "/";
  165.   dataString += String(ANO) + ", ";
  166.   dataString += String(HORA) + ":";
  167.   dataString += String(MIN) + ", ";
  168.   dataString += String(temperatura) + ", ";
  169.   dataString += String(pressao);
  170.  
  171.   lcd.clear();
  172.   if (contador == 1){
  173.   lcd.setCursor(0, 0);
  174.   lcd.print("Lat:");
  175.   lcd.setCursor(5, 0);
  176.   lcd.print(Lat, 6);
  177.   lcd.setCursor(0, 1);
  178.   lcd.print("Lon:");
  179.   lcd.setCursor(5, 1);
  180.   lcd.print(Lon, 6);
  181.   }
  182.   if(contador==2){
  183.   lcd.setCursor(0, 0);
  184.   lcd.print("HORA GMT:");
  185.   lcd.setCursor(11, 0);
  186.   lcd.print(HORA);
  187.   lcd.setCursor(13, 0);
  188.   lcd.print(":");
  189.   lcd.setCursor(14, 0);
  190.   lcd.print(MIN);  
  191.   lcd.setCursor(0, 1);
  192.   lcd.print("DATA GMT:");
  193.   lcd.setCursor(11, 1);
  194.   lcd.print(DIA);
  195.   lcd.setCursor(13, 1);
  196.   lcd.print("/");
  197.   lcd.setCursor(14, 1);
  198.   lcd.print(MES);    
  199.   }
  200.  
  201.   if(contador==3){
  202.   lcd.setCursor(0, 0);
  203.   lcd.print("Temp:");
  204.   lcd.setCursor(9, 0);
  205.   lcd.print(temperatura, 2);
  206.   lcd.setCursor(0, 1);
  207.   lcd.print("Pressao:");
  208.   lcd.setCursor(9, 1);
  209.   lcd.print(pressao, 2);  
  210.   }
  211.  
  212.   imprime = 0;
  213. }
  214.  
  215. //------------------------------------------------------------------------
  216.     if ((millis() - delay2) >= delayBounce) {
  217.        estadoBotao = digitalRead(pinBotao);
  218.          if (!estadoBotao && estadoAntBotao){
  219.           lcd.clear();
  220.           if(contador<3){
  221.             contador++;
  222.             }else{
  223.             contador=1;
  224.             }
  225.             }
  226.             delay2 = millis();
  227.          }
  228.           estadoAntBotao = estadoBotao;
  229. //------------------------------------------------------------------------------
  230.   if ((millis() - delay1) > 1000) {
  231.       Serial.println("Passou a mills");
  232.       File dataFile = SD.open("datalog.txt", FILE_WRITE);
  233.           // if the file is available, write to it:
  234.           if (dataFile) {
  235.             dataFile.println("dataString");
  236.             dataFile.close();
  237.             Serial.print("Gravou dado no cartão: ");
  238.             Serial.println("dataString");
  239.           }else {
  240.             //Sinal sonoro/visual em caso de problema na gravação. (Led Vermelho. Liga e mantem ligado se isso ocorrer)
  241.             Serial.println("Não gravou no cartão");
  242.             //digitalWrite(pinLED1, HIGH);
  243.             }
  244.             //dataString = "";
  245.       delay1 = millis();
  246.  
  247.   }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement