Pages: [1] 2   Go Down
Author Topic: how to convert a float to a char* ?  (Read 1209 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thinks  smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cast it to an int and use itoa
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When i do :
  
Code:
 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 ...
« Last Edit: June 14, 2012, 01:30:33 pm by helium255 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* IMGP2297.JPG (1947.27 KB, 3648x2736 - viewed 21 times.)

* IMGP2296.JPG (1888.69 KB, 3648x2736 - viewed 23 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
long vitesse_knot;
"vitesse_noeud", surely?
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why not ?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Parce-que
Code:
byte mois, jour, heure,
   smiley-wink
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-confuse
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 ?
« Last Edit: June 15, 2012, 04:58:00 am by helium255 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It works !
Logged

Pages: [1] 2   Go Up
Jump to: