how to convert a float to a char* ?

Hello !
I have an Arduino nano with a Nokia 6100 LCD, and a EM-406A GPS receiver.
The fonction to upload text to the LCD is : void drawString(char s, int fg, int bg, int x, int y);
I want to upload on the screen for exemple my altitude, but i need to convert a float variable to a char
.
I tried drawString((char*)&altitude,WHITE,0,50,0); and with sprintf but it doesn't work.
:slight_smile:
Do you have a solution ?
thinks !

ftoa() or dtostrf() will do what you need to do.

Indeed, it works !
Do you know how to convert a byte in a char* ?

thinks :slight_smile:

Cast it to an int and use itoa

When i do :

  int_h = (int)h;
  itoa(int_h,char_h,2);
  drawString(char_h,WHITE,0,50,0);

It displays 0 !
But on my computer, (with serial.println) it's not 0 ...

Why do you want to print it in binary?
Just curious.

I don't !
The fonction gps.crack_datetime(&annee, &mois, &jour, &h, &min, &sec, &age); use byte variables, so I have to convert it in a char* to display it.

When i do :

int_h = (int)h;
itoa(int_h,char_h,2);
drawString(char_h,WHITE,0,50,0);

It displays 0 !
But on my computer, (with serial.println) it's not 0 ...

Which is why we don't want to see just snippets of your code. We want to see all of it.

sorry:

#include <nokialcd_controlcodes.h>
#include <nokialcd_driver.c>
#include <nokialcd_driver.h>
#include <nokialcd_font.c>
#include <nokialcd_font.h>
#include <nokialcd_io.h>
#include <nokialcd_scroll.c>
#include <nokialcd_scroll.h>
#include <sysconfig-atmega168.c>

#include <SoftwareSerial.h>
#include <TinyGPS.h>

float latitude, longitude;
float alt;
char char_alt[8];
int annee;
byte mois, jour, heure, minute, sec, age;
char *char_h, *char_min;
int int_h, int_min;
long vitesse_kmh;
long vitesse_knot;
long cap;

SoftwareSerial s_gps(6,5);
TinyGPS gps;
void getgps(TinyGPS &gps);


void setup()
{
  sysInit();
  LCDInitController();
  LCDClearScreen(0x003);  
  
  s_gps.begin(4800);

}


void loop()
{
  ///////////////////////////////
  while(s_gps.available())
  {
  byte c = s_gps.read();
  
  if(gps.encode(c))
  {
   getgps(gps); 
  }
 }
 ////////////////////////////////
  int_h = (int)heure;
  itoa(int_h,char_h,2);// The problem is here
  drawString(char_h,WHITE,0,50,0);
  drawString("heure",WHITE,0,63,0);

  dtostrf(alt,6,2,char_alt);
  if(alt<9999.0){//evite le 100000.00 qui reste sur l'écran
  drawString(char_alt,WHITE,0,10,30);
  }
}

 void getgps(TinyGPS &gps)
 {
   
   gps.f_get_position(&latitude,&longitude);
   gps.crack_datetime(&annee, &mois, &jour, &heure, &minute, &sec, &age);
   alt = gps.f_altitude();
   cap = gps.f_course();
   vitesse_kmh = gps.f_speed_kmph();
   vitesse_knot = gps.f_speed_mph();

 }

For the moment the code only displayed the hour(not the minutes), and the altitude.
when i delete the line with the itoa(), the altitude is displayed on my screen, but when i put it in my code, the altitude displayed stays at 0.00.
look at the pictures:the one with the altitude displayed is the code without the itoa().

long vitesse_knot;

"vitesse_noeud", surely?

why not ?

Parce-que
byte mois, jour, heure, :wink:

:~
The GPS can give me my speed in knot or in km/h.
Anyway, for the moment it's not the problem(later maybe).
Why the itoa() doesn't work ?

char *char_h, *char_min;

A pointer is useless UNTIL it points to something. These do not at this time.

  itoa(int_h,char_h,2);// The problem is here

It certainly is. You are trying to write to a place you have no business writing to. The function would throw an exception, if it could. But, it can't. So, it silently does nothing.

char char_h[3], char_min[3];

would be a much better declaration statement for your purposes.

And, now you know why snippets are useless.

  sprintf(char_time,"%02d:%02d",heure,minute); 
  drawString(char_time,WHITE,0,50,0);

It works !

And, now you know why snippets are useless.

Or not...