# [exemplo] rumo ao DEEC

#### Objetivo:

<div class="pointer-container" id="bkmrk-%C2%A0"><div class="pointer anim is-page-editable"><svg class="svg-icon" data-icon="link" role="presentation" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"></svg><div class="input-group inline block"> <button class="button outline icon" data-clipboard-target="#pointer-url" title="Copy Link" type="button"><svg class="svg-icon" data-icon="copy" role="presentation" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"></svg></button></div><svg class="svg-icon" data-icon="edit" role="presentation" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"></svg></div></div>Chegar ao DEEC - FCTUC via sistema de posicionamento global com Arduino.

##### Requisitos

<table border="1" id="bkmrk-%5Ba%5D-%5Bb%5D-%5Bc%5D" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 33.3051%;">*[![MMnvvKD.png](https://i.imgur.com/MMnvvKD.png)](https://kb.deec.uc.pt/books/deec/page/instalacao-irK)*</td><td style="width: 33.3051%;">*[![w4QdtAr.png](https://i.imgur.com/w4QdtAr.png)](https://kb.deec.uc.pt/books/deec/page/exemplo-modulo-gps)*</td><td style="width: 33.3051%;">*[![h8eDfvF.png](https://i.imgur.com/h8eDfvF.png)](https://kb.deec.uc.pt/books/deec/page/exemplo-i2c-oled)*</td></tr><tr><td class="align-center" style="width: 33.3051%;">[\[a\]](https://kb.deec.uc.pt/books/deec/page/instalacao-irK)</td><td class="align-center" style="width: 33.3051%;">[\[b\]](https://kb.deec.uc.pt/books/deec/page/exemplo-modulo-gps)</td><td class="align-center" style="width: 33.3077%;">[\[c\]](https://kb.deec.uc.pt/books/deec/page/exemplo-i2c-oled)</td></tr></tbody></table>

##### \[exemplo\] rumo ao DEEC

<table border="1" id="bkmrk-" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 49.9589%;">*[![tracker1.jpg](https://kb.deec.uc.pt/uploads/images/gallery/2024-04/scaled-1680-/tracker1.jpg)](https://kb.deec.uc.pt/books/deec/page/instalacao-irK)*</td><td style="width: 49.9589%;">[![tracker2.jpg](https://kb.deec.uc.pt/uploads/images/gallery/2024-04/scaled-1680-/tracker2.jpg)](https://kb.deec.uc.pt/uploads/images/gallery/2024-04/tracker2.jpg)</td></tr></tbody></table>

##### Set-up

<table border="1" id="bkmrk--0" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 33.3051%;">*[![gxq9zCf.png](https://i.imgur.com/gxq9zCf.png)](https://i.imgur.com/gxq9zCf.png)*</td><td style="width: 33.3051%;">*[![ZcZapjK.png](https://i.imgur.com/ZcZapjK.png)](https://i.imgur.com/ZcZapjK.png)*</td><td style="width: 33.3077%;">*[![f9H7Yyg.png](https://i.imgur.com/f9H7Yyg.png)](https://i.imgur.com/f9H7Yyg.png)*</td></tr></tbody></table>

<table border="1" id="bkmrk-arduino-compat%C3%ADvel-u" style="border-collapse: collapse; width: 100%; height: 836.562px;"><tbody><tr><td colspan="4" style="width: 100.082%;">*[![luAWx2A.png](https://i.imgur.com/luAWx2A.png)](https://i.imgur.com/luAWx2A.png)*</td></tr><tr><td class="align-center" style="width: 25.0824%; height: 46.25px;">Arduino compatível UNO</td><td style="width: 25.0824%; height: 46.25px;">  
</td><td class="align-center" style="width: 24.9588%; height: 46.25px;">módulo GPS</td><td class="align-center" style="width: 24.9588%; height: 46.25px;">ecrã OLED</td></tr><tr><td class="align-right" rowspan="2" style="height: 58.9166px; width: 25.0824%;">3.3V  
</td><td class="align-center" style="width: 25.0824%;"><span style="color: #e03e2d;">------------</span></td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">  
</td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">VCC</td></tr><tr><td class="align-center" style="width: 25.0824%; height: 29.4583px;"><span style="color: #e03e2d;">------------</span></td><td class="align-center" style="width: 24.9588%; height: 29.4583px;"><span style="color: #000000;">V</span></td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">  
</td></tr><tr><td class="align-right" rowspan="2" style="width: 25.0824%; height: 58.9166px;">GND  
</td><td class="align-center" style="width: 25.0824%; height: 29.4583px;"><span style="color: #000000;">------------</span></td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">  
</td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">GND</td></tr><tr><td class="align-center" style="width: 25.0824%; height: 29.4583px;"><span style="color: #000000;">------------</span></td><td class="align-center" style="width: 24.9588%; height: 29.4583px;"><span style="color: #000000;">G</span></td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">  
</td></tr><tr><td class="align-right" style="width: 25.0824%;">2</td><td class="align-center" style="width: 25.0824%;"><span style="color: #f1c40f;">------------</span></td><td class="align-center" style="width: 24.9588%;">R</td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">  
</td></tr><tr style="height: 29.4583px;"><td class="align-right" style="width: 25.0824%;">3</td><td class="align-center" style="width: 25.0824%;"><span style="color: #843fa1;">------------</span></td><td class="align-center" style="width: 24.9588%;">T</td><td class="align-center" style="width: 24.9588%; height: 29.4583px;">  
</td></tr><tr><td class="align-right" style="width: 25.0824%;">A4</td><td class="align-center" style="width: 25.0824%;"><span style="color: #2dc26b;">------------</span></td><td class="align-center" style="width: 24.9588%;">  
</td><td class="align-center" style="width: 24.9588%;">SDA</td></tr><tr><td class="align-right" style="width: 25.0824%;">A5</td><td class="align-center" style="width: 25.0824%;"><span style="color: #3598db;">------------</span></td><td class="align-center" style="width: 24.9588%;">  
</td><td class="align-center" style="width: 24.9588%;">SDL</td></tr></tbody></table>

##### Programa

```C
#include <SoftwareSerial.h>
#include <TinyGPSPlus.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED display TWI address

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

SoftwareSerial gpsSerial(2, 3);  // RX, TX
TinyGPSPlus gps;
bool selector = false;

void setup() {
  // inicializa ecrã
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.display();

  // imprime linha de texto
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(27, 30);
  display.print(F("..."));

  // actualiza ecrã
  display.display();

  gpsSerial.begin(9600);
  Serial.begin(115200);
}

void loop() {

  while (gpsSerial.available() > 0){
    if (gps.encode(gpsSerial.read()))
      displayInfo();
  }

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
}

void displayInfo() {
  bool chalk_a = true;
  bool chalk_b = true;
  selector = false;
  Serial.print(F("Location: "));

  if (gps.location.isValid()) {
    Serial.print(gps.location.lat(), 12);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 12);

    if (chalk_a) {
      chalk_a = false;
    } 

  } else {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid()) {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  } else {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid()) {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());

    if (chalk_b) {
      chalk_b = false;
    }
  } else {
    Serial.print(F("INVALID"));
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  if (!chalk_a) {
    static const double DEECFCTUC_LAT = 40.18639388156963, DEECFCTUC_LON = -8.416520235329802;
    unsigned long distanceKmToDEEC =
    (unsigned long)TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      DEECFCTUC_LAT, 
      DEECFCTUC_LON);

    if (distanceKmToDEEC > 1000) {
      distanceKmToDEEC = distanceKmToDEEC / 1000;
      selector = true;
    } 

    Serial.println(distanceKmToDEEC);

    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(30, 35);
    display.print(distanceKmToDEEC);
    display.setCursor(90, 35);
    if (selector) {
      display.print(F("km"));
    } else {
      display.print(F("m"));
    }

    display.setTextSize(1);
    display.setTextColor(WHITE);
  } else {
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(40, 35);
    display.print(F("..."));
    display.setCursor(90, 35);
    if (selector) {
      display.print(F("km"));
    } else {
      display.print(F("m"));
    }

    display.setTextSize(1);
    display.setTextColor(WHITE);
  }

  display.setCursor(5, 5);
  display.print(F("agora"));
  display.setCursor(5, 20);
  display.print(F("faltam"));
  display.setCursor(5, 55);
  display.print(F("para chegar ao DEEC"));

  if (!chalk_b) {

    display.setCursor(50, 5);
    display.print(gps.time.hour());
    display.setCursor(63, 5);
    display.print(":");
    display.setCursor(70, 5);
    display.print(gps.time.minute());
    display.setCursor(83, 5);

  }
  else {
    display.setCursor(50, 5);
    display.print(F("00"));
    display.setCursor(63, 5);
    display.print(":");
    display.setCursor(70, 5);
    display.print(F("00"));
    display.setCursor(83, 5);

  }
  display.display();
  Serial.println();
}
```

#### Outros exemplos

<table border="1" id="bkmrk-%5Ba%5D-%5Bb%5D-%5Bc%5D-0" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 33.3051%;">*[![MMnvvKD.png](https://i.imgur.com/MMnvvKD.png)](https://kb.deec.uc.pt/books/deec/page/instalacao-irK)*</td><td style="width: 33.3051%;">*[![w4QdtAr.png](https://i.imgur.com/w4QdtAr.png)](https://kb.deec.uc.pt/books/deec/page/exemplo-modulo-gps)*</td><td style="width: 33.3051%;">*[![h8eDfvF.png](https://i.imgur.com/h8eDfvF.png)](https://kb.deec.uc.pt/books/deec/page/exemplo-i2c-oled)*</td></tr><tr><td class="align-center" style="width: 33.3051%;">[\[a\]](https://kb.deec.uc.pt/books/deec/page/instalacao-irK)</td><td class="align-center" style="width: 33.3051%;">[\[b\]](https://kb.deec.uc.pt/books/deec/page/exemplo-modulo-gps)</td><td class="align-center" style="width: 33.3077%;">[\[c\]](https://kb.deec.uc.pt/books/deec/page/exemplo-i2c-oled)</td></tr></tbody></table>

\[a\] DEEC - FCTUC. Kit Arduino Uno &gt; Instalação. url: [https://kb.deec.uc.pt/books/deec/page/instalacao-irK](https://kb.deec.uc.pt/books/deec/page/instalacao-irK) (acedido em 10/04/2024).

\[b\] DEEC - FCTUC. Kit Arduino Uno &gt; \[exemplo\] módulo GPS. url: [https://kb.deec.uc.pt/books/deec/page/exemplo-modulo-gps](https://kb.deec.uc.pt/books/deec/page/exemplo-modulo-gps) (acedido em 10/04/2024).

\[c\] DEEC - FCTUC. Kit Arduino Uno &gt; \[exemplo\] I2C OLED. url: [https://kb.deec.uc.pt/books/deec/page/exemplo-i2c-oled](https://kb.deec.uc.pt/books/deec/page/exemplo-i2c-oled) (acedido em 10/04/2024).