#include "NMEA.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define LEN(arr) ((int)(sizeof(arr) / sizeof(arr)[0]))
// Config GPS
union {
char bytes[4];
float valor;
} velocidadeGPS;
float latitude;
float longitude;
// Use Serial1 for GPS communication on Arduino Mega
NMEA gps(GPRMC);
// Config LED Bar
const int analogPin = A0;
const int ledCount = 10;
int ledPins[] = {
13, 12, 11, 10, 9, 8, 7, 6, 5, 4
};
// Config Display OLED
const byte SCREEN_WIDTH = 128;
const byte SCREEN_HEIGHT = 64;
const int OLED_RESET = -1;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
Serial1.begin(9600); // GPS
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // or 0x3D for a different address
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Definindo todos os leds como saída
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// Simulação do sensor de nível de bateria no led bar
int sensorReading = analogRead(analogPin);
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
int porcentLevel = map(sensorReading, 0, 1023, 0, 100);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
} else {
digitalWrite(ledPins[thisLed], LOW);
}
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Nivel da bateria: ");
display.print(porcentLevel);
display.println("%");
// Use Serial1 for GPS data
while (Serial1.available()) {
char serialData = Serial1.read(); // Receives data from GPS serial port
if (gps.decode(serialData)) { // Checks if the GPS sentence is valid
if (gps.gprmc_status() == 'A') { // Checks if GPS status is 'A'
velocidadeGPS.valor = gps.gprmc_speed(KMPH); // Receives GPS speed in km/h
} else {
velocidadeGPS.valor = 0;
}
latitude = gps.gprmc_latitude();
longitude = gps.gprmc_longitude();
}
}
display.setCursor(0, 20);
display.print("latitude: ");
display.println(latitude, 6);
display.setCursor(0, 30);
display.print("Longitude: ");
display.println(longitude, 6);
display.setCursor(0, 40);
display.print("velocidade: ");
display.print(velocidadeGPS.valor, 2);
display.println("Km/h");
display.display();
delay(100);
}