Integer remove digit after dot

Hi there,

I have a simple question for you, i am right now stuck with figuring out on how to remove the digits after the comma/dot in a integer. I am right now developing a speedometer that also needs to show the amount of satellites it found. right now it does display it. but it displays it like 4.00 when it has 4 satellites. i need it to just be 4.

How can i do this?

Any help would be appreciated!

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

The trick is not to imagine that your GPS can see fractional satellites in the first place.

Okay, here is the code i right now have

#include <MCUFRIEND_kbv.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include "Adafruit_GFX.h"
#include <TouchScreen.h>

uint8_t YP = A1;  // must be an analog pin, use "An" notation!
uint8_t XM = A2;  // must be an analog pin, use "An" notation!
uint8_t YM = 7;   // can be a digital pin
uint8_t XP = 6;   // can be a digital pin
uint16_t TS_LEFT = 970;
uint16_t TS_RT  = 217;
uint16_t TS_TOP = 936;
uint16_t TS_BOT = 189;

MCUFRIEND_kbv tft;
TinyGPS gps;
SoftwareSerial ss(52, 50);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;

void setup() {
  // put your setup code here, to run once:
  ss.begin(9600);
  Serial.begin(9600);
  tft.begin(0x9325);
  tft.setRotation(1);
  tft.fillScreen(0x0000);

  tft.setTextColor(0xFFFF);
  tft.setTextSize(2);
  tft.setCursor(80, 90);
  tft.print("GPS Speedometer");
  tft.setTextSize(1);
  tft.setCursor(100, 110);
  tft.print("Created by Sander");

  delay(5000);
}

void loop() {
  // put your main code here, to run repeatedly:
  tft.fillScreen(0x0000);
  char tmp[10];
  float flat, flon;
  unsigned long fix_age; // returns +- latitude/longitude in degrees
  gps.f_get_position(&flat, &flon, &fix_age);
  float fsat = gps.satellites();
  float falt = gps.f_altitude(); // +/- altitude in meters
  float fc = gps.f_course(); // course in degrees
  float fkmph = gps.f_speed_kmph(); // speed in km/hr
  if (fix_age == TinyGPS::GPS_INVALID_AGE)
  {
    tft.setTextColor(0xF800);
    tft.setTextSize(2);
    tft.setCursor(1, 1);
    tft.print("No GPS Fix!");
  }
  else
  {
    tft.setTextColor(0x07E0);
    tft.setTextSize(2);
    tft.setCursor(1, 1);
    tft.print("GPS Fix!");
  }
  if (fsat == TinyGPS::GPS_INVALID_SATELLITES)
  {
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 20);
    tft.print("Satellites: -");
  }
  else
  {
    //This is where i need help

    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 20);
    tft.print("Satellites: ");
    dtostrf(fsat, 1, 2, tmp);
    tft.setCursor(182, 20);
    tft.print(tmp);
    char tmp[10];
  }
  print_date(gps);
  smartdelay(1000);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
  {
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 40);
    tft.print("UTC: -");
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
            day, month, year, hour, minute, second);
    String strTemp1 = "UTC: ";
    String strTemp2 = strTemp1 + sz;
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 40);
    tft.print(strTemp2);
  }
  smartdelay(0);
}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

See reply #2

What data type does TinyGPS::satellites() return? (Hint: it's not a float.)

Pieter

AWOL:
The trick is not to imagine that your GPS can see fractional satellites in the first place.

I know it can't see that or that they even exist in the first place. But the .00 gets added automaticly

PieterP:
TinyGPS/TinyGPS.h at db4ef9c97af767e7345f5ccb277ac3bd1a2eb81f · mikalhart/TinyGPS · GitHub
What data type does TinyGPS::satellites() return? (Hint: it's not a float.)

Pieter

It is a short apparently, But how do i then use it correctly in my code?

You start with a short integer number, then you cast (convert) it to a floating point number by storing it in a variable of type float. By default, printing a float prints two decimal digits.

Pieter

PieterP:
You start with a short integer number, then you cast (convert) it to a floating point number by storing it in a variable of type float. By default, printing a float prints two decimal digits.

Pieter

Thanks! So all i have to do is just print the short?

Save it in a short variable, not a float.

So the code is now this, but all i get now is corrupted symbols after the satellites string. What is happening?

#include <MCUFRIEND_kbv.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include "Adafruit_GFX.h"
#include <TouchScreen.h>

uint8_t YP = A1;  // must be an analog pin, use "An" notation!
uint8_t XM = A2;  // must be an analog pin, use "An" notation!
uint8_t YM = 7;   // can be a digital pin
uint8_t XP = 6;   // can be a digital pin
uint16_t TS_LEFT = 970;
uint16_t TS_RT  = 217;
uint16_t TS_TOP = 936;
uint16_t TS_BOT = 189;

MCUFRIEND_kbv tft;
TinyGPS gps;
SoftwareSerial ss(52, 50);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;

void setup() {
  // put your setup code here, to run once:
  ss.begin(9600);
  Serial.begin(9600);
  tft.begin(0x9325);
  tft.setRotation(1);
  tft.fillScreen(0x0000);

  tft.setTextColor(0xFFFF);
  tft.setTextSize(2);
  tft.setCursor(80, 90);
  tft.print("GPS Speedometer");
  tft.setTextSize(1);
  tft.setCursor(100, 110);
  tft.print("Created by Sander");

  delay(5000);
}

void loop() {
  // put your main code here, to run repeatedly:
  tft.fillScreen(0x0000);
  char tmp[10];
  float flat, flon;
  unsigned long fix_age; // returns +- latitude/longitude in degrees
  gps.f_get_position(&flat, &flon, &fix_age);
  short fsat = gps.satellites();
  float falt = gps.f_altitude(); // +/- altitude in meters
  float fc = gps.f_course(); // course in degrees
  float fkmph = gps.f_speed_kmph(); // speed in km/hr
  if (fix_age == TinyGPS::GPS_INVALID_AGE)
  {
    tft.setTextColor(0xF800);
    tft.setTextSize(2);
    tft.setCursor(1, 1);
    tft.print("No GPS Fix!");
  }
  else if (fix_age > 5000)
  {
    tft.setTextColor(0xFC00);
    tft.setTextSize(2);
    tft.setCursor(1, 1);
    tft.print("GPS possible stale data!");
  }
  else
  {
    tft.setTextColor(0x07E0);
    tft.setTextSize(2);
    tft.setCursor(1, 1);
    tft.print("GPS Fix!");
  }
  if (fsat == TinyGPS::GPS_INVALID_SATELLITES)
  {
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 20);
    tft.print("Satellites: -");
  }
  else
  {
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 20);
    tft.print("Satellites: ");
    tft.setCursor(142, 20);
    tft.print(tmp);
  }
  print_date(gps);
  smartdelay(1000);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
  {
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 40);
    tft.print("UTC: -");
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
            day, month, year, hour, minute, second);
    String strTemp1 = "UTC: ";
    String strTemp2 = strTemp1 + sz;
    tft.setTextColor(0xFFFF);
    tft.setTextSize(2);
    tft.setCursor(1, 40);
    tft.print(strTemp2);
  }
  smartdelay(0);
}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

Get rid of all the String variables. They cause memory problems in Arduino, and mixing those with C-strings (character arrays) is asking for trouble.

For example, this very badly coded section:

    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
            day, month, year, hour, minute, second);
    String strTemp1 = "UTC: ";
    String strTemp2 = strTemp1 + sz;
...
    tft.print(strTemp2);

Can be replaced by two lines:

    sprintf(sz, "UTC: %02d/%02d/%02d %02d:%02d:%02d ",
            day, month, year, hour, minute, second);
    tft.print(sz);

jremington:
Get rid of all the String variables. They cause memory problems in Arduino, and mixing those with C-strings (character arrays) is asking for trouble.

For example, this very badly coded section:

    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",

day, month, year, hour, minute, second);
    String strTemp1 = "UTC: ";
    String strTemp2 = strTemp1 + sz;
...
    tft.print(strTemp2);




Can be replaced by two lines:



sprintf(sz, "UTC: %02d/%02d/%02d %02d:%02d:%02d ",
            day, month, year, hour, minute, second);
    tft.print(sz);

Thanks for the replacement code! But i am still stuck with random characters at the satellites value :confused:

You are printing a non-initialized array of characters. What else did you expect?
At least you got rid of the dtostrf().

Pieter

PieterP:
You are printing a non-initialized array of characters. What else did you expect?
At least you got rid of the dtostrf().

Pieter

OMG how did i miss that xD I am printing tmp which had nothing in it. I'll try to get the data from the GPS and see if it will work. If i can't get it to work i could look at some code from somebody else that has already figured it out.

I got it to work! Thanks everyone for helping!!! :slight_smile:

I do have one more question, I want to display the speed i am going at in KM/H. Now that isn't very difficult. But i want it to be displayed like 000 KM/H. So if i would be driving at 50 KM/H it would be displayed like 050 KM/H. How can i achieve this?

You should learn to use sprintf().

   sprintf(buf,"%03d KM/H",speed);
   Serial.println(buf);

jremington:
You should learn to use sprintf().

   sprintf(buf,"%03d KM/H",speed);

Serial.println(buf);

Thanks! i'll check out on how to use sprintf()