Project Help! lcd and gps problem

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! :smiley:

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;
    }
  }
}

output with commented LCD code in loop.PNG

output with LCD code in loop.PNG

Unless you understand what interrupts are, best not to attempt to use them. :grinning:

I watched all the interrupt lesson on this vid sir,

and i also noticed that the response of the gps is much faster than before without the interrupt sir, is it impossible sir to ask for the program if the gps if available then at the same time ask for the sim808 if a certain txt is there(proc_SIM808) then execute the LCD timer code sir (proc_Timer)?

Lots of problems with that code.

  1. Variables shared with interrupt routines must be declared volatile.

  2. Access to shared variables must be protected from corruption by interrupts, e.g. make a copy before use:

noInterrupts();
copy_of_shared_variable=shared_variable;
interrupts();
  1. Don't use Strings with Arduino, especially Uno. They cause memory problems and program crashes.

nao201431688:
I also noticed that the response of the GPS is much faster than before without the interrupt sir,

Not sure of your point. In what way is the "response ... faster" but yet your code does not work?

I am reluctant to spend time watching that video - LESSON 31: Understanding Arduino Arrays - YouTube as it is actually titled "Understanding Arduino Arrays"!

oh wronk link! sorry for that I mean this one
LESSON 30: Advanced Software Interrupt Techniques for Reading Serial Data on Arduino - YouTube his lessons 28 to 30 sir and also this one Arduino Workshop - Chapter 5 - Interrupts - YouTube

what i mean by that is the gps response sir its just before i have to wait for like 60 seconds to be able to receive gps coordinates sir but now it seems like i can receive gps data in a matter of 5 secs. anyway sir even though its much faster i still get the same problem with or without the interrupt sir! the LCD countdown still wont work with the GPS.

uhm sir jremington sorry sir, where do i put this code that you mentioned sir, its just I didn't see this kind of code on the vids that I watched sir. Is this code a must sir if interrupt is used in a program sir?

noInterrupts();
copy_of_shared_variable=shared_variable;
interrupts();

volatile sir? you mean sir like this i declared all the variable that i used inside the interrupt sir as volatile

volatile uint8_t flag = 0;
volatile char floatlat[10];
volatile char floatlon[11];
volatile float dlat, dlon;

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .

It will show how to display images and embed links properly int your post.

Tom... :slight_smile: