Go Down

Topic: how to convert a float to a char* ? (Read 1 time) previous topic - next topic

helium255

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.
:)
Do you have a solution ?
thinks !

PaulS

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

helium255

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

thinks  :)

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

helium255

#4
Jun 14, 2012, 08:22 pm Last Edit: Jun 14, 2012, 08:30 pm by helium255 Reason: 1
When i do :
 
Code: [Select]

 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 ...

AWOL

Why do you want to print it in binary?
Just curious.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

helium255

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.

PaulS

Quote
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.

helium255

sorry:
Code: [Select]

#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().

AWOL

Code: [Select]
long vitesse_knot;
"vitesse_noeud", surely?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


AWOL

Parce-que
Code: [Select]
byte mois, jour, heure,   ;)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

helium255

#12
Jun 15, 2012, 11:00 am Last Edit: Jun 15, 2012, 11:58 am by helium255 Reason: 1
:~
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 ?

PaulS

Code: [Select]
char *char_h, *char_min;
A pointer is useless UNTIL it points to something. These do not at this time.

Code: [Select]
  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.

Code: [Select]
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.

helium255

Code: [Select]

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


It works !

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy