Hi guys! I’m working on an LCD that keeps doing countdown while the GPS keeps sending coordinates. I succesfully made the GPS and it sends a correct GPS coordinates. The problem is that the LCD code wont work together with the GPS code?
I tried using interrupt this time hoping that i can make the LCD countdown while the gps keep sending the coordinates since last time when i made the code without interrupt the same thing happen, the countdown stops when sim808 sends a coordinates.
can u guys give me some tips pls!
Thank you very much I will really appreciate any help from you guys!
I have:
sim808
Arduino UNO
16x2 LCD with I2c
#include <TimerOne.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <NMEAGPS.h>
//GPS VARIABLES
int flag = 0;
String UPDATE_T_sec = "$PMTK220,5000*1B\r\n";
static NMEAGPS gps;
static gps_fix fix;
char message[30] = {0};
char floatlat[10];
char floatlon[11];
float dlat, dlon;
String sim808data;
//LCD VARIABLES
int runtime;
int countminutes;
int countseconds = 0;
bool overdue = false;
bool runme = false;
//Loop Declarations
unsigned long previousMillis;
SoftwareSerial port(2, 3); //rxtx, txrx
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
//GPS INITIALIZE
port.begin(9600);
Serial.begin(115200);
delay(100);
gps_Start();
//GPS REFRESH RATE
port.print(UPDATE_T_sec);
Timer1.initialize(1000);
Timer1.attachInterrupt(readGPS);
//LCD INITIALIZE
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("DEVICE READY!");
}
void loop() {
//proc_SIM808(); // THE PROBLEM IS RIGHT HERE!
//proc_TIMER();
if (flag == 1) {
Timer1.stop();
Serial.println(floatlat);
Serial.println(floatlon);
flag = 0;
Timer1.restart();
}
}
//GPS C0DE - - - - - - - - - - -- - - - - - - - - - - - - -
void gps_Start() {
Serial.println(F("GPS STARTING."));
bool startup = false;
port.print("AT+CGNSPWR=1\r\n");
delay(500);
port.print("AT+CGNSTST=1\r\n");
delay(500);
port.println(F("GPS READY"));
while (!startup) {
if (port.available()) {
sim808data = port.readString();
Serial.println(sim808data);
startup = true;
}
}
}
void readGPS() {
if (gps.available(port)) {
fix = gps.read();
if (fix.valid.location) {
dlat = fix.latitude();
dlon = fix.longitude();
}
dtostrf(dlat, 9, 6, floatlat);
dtostrf(dlon, 10, 6, floatlon);
flag = 1;
}
}
//LCD CODE - - - - - - - - - - - - - - - - - - - - - - - - - -
void proc_SIM808() {
if (port.available()) {
sim808data = port.readString();
if (sim808data.indexOf("start") > 0) {
int value = sim808data.indexOf("start");
runtime = sim808data.substring(value + 5).toInt();
countseconds = 0;
runme = true;
}
if (sim808data.indexOf("stop") > 0) {
if (overdue)
{
lcd.setCursor(0, 1);
lcd.print("--DEVICE STOP!--");
} else {
lcd.setCursor(0, 1);
lcd.print("--DEVICE STOP!--");
}
runme = false;
overdue = false;
}
Serial.println(sim808data);
}
}
void proc_TIMER() {
if (runme) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000 ) {
lcd.clear();
if (runtime <= 0 && overdue) {
if (countseconds == 59)
{
runtime--;
countseconds = 0;
}
lcd.setCursor(0, 1);
lcd.print("Cost:");
lcd.print((runtime * -1));
countseconds++;
} else if (runtime >= 0) {
if (countseconds == 0) {
runtime--;
countseconds = 59;
if (runtime == -1) {
overdue = true;
runtime = 0;
countseconds = 1;
}
}
countseconds--;
}
lcd.setCursor(5, 0);
if (runtime == 0 && overdue) {
lcd.print("-" + runtime);
}
lcd.print(runtime);
lcd.print(":");
lcd.print(countseconds);
previousMillis = currentMillis;
}
}
}