Pages: [1]   Go Down
Author Topic: Printing floats to the Serial port  (Read 1001 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a problem with printing a float array.  It says the call of the overloaded 'print(float[11], int)' is ambiguous.

here is the code:

Code:
#include <OLED320.h>
#include <string.h>
#include <ctype.h>
#include <print.h>

 int rx1Pin = 19;                    // RX1 PIN
 int tx1Pin = 18;                    // TX1 TX
 int byteGPS=-1;
 char linea[300] = "";
 char arguments[13][11];
 [glow]double float_arguments[13][11];[/glow]
 char comandoGPR[7] = "$GPRMC";
 int cont=0;
 int bien=0;
 int conta=0;
 int indices[13];

OLED320 LED;

void setup() {
pinMode(rx1Pin, INPUT);
pinMode(tx1Pin, OUTPUT);
Serial1.begin(4800);
Serial2.begin(9600);
Serial.begin(19200);

for (int i=0;i<300;i++){       // Initialize a buffer for received data
  linea[i]=' ';
}

Serial2.print(0x55,BYTE);          //initialize led
delay(1000);

LED.LED_Clear();                    //clear led
delay(100);

}

void loop() {
  byteGPS=Serial1.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
     delay(100);
  } else {
     linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
     conta++;                      
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       cont=0;
       bien=0;
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (linea[i]==comandoGPR[i-1]){
           bien++;
         }
       }
       if(bien==6){               // If yes, continue and process data
         for (int i=0;i<300;i++){
           if (linea[i]==','){    // check for position of "," separator
             indices[cont]=i;
             cont++;
           }
           if (linea[i]=='*'){    // ... and the "*"
             indices[12]=i;
             cont++;
           }
         }
                  
           char* str = linea;   //direct linea char array to str pointer array
           char seperator = ',';
           int count = 0, index = 0, sIndex = 0;
           int theSize = 299;
           arguments[index][count] = '\0';  // clear first argument before starting
        
           while (theSize > sIndex + 1){
             if (*str != seperator){
               arguments[index][count] = *str;  //store array sections into argument arrays
               [glow]float_arguments[index][count] = (float) arguments[index][count];[/glow]
             }
             else {
               arguments[index][count] = '\0';
             }
             *str++;
             count++;
             sIndex++;
             if (*str == seperator){
               *str++;                          // jump over the separator
               count = 0;
               index++;
               arguments[index][count] = '\0';  // clear argument before starting
               float_arguments[index][count] = '\0';
             }
           }          
          
          Serial.print("LAT: ");
          [glow]Serial.print(float_arguments[3],2);[/glow]
       }
       conta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         linea[i]=' ';            
       }                
     }
   }
}


I thought print could now handle floating point.  Possibly I was wrong in assuming it could handle an array of floating points.
« Last Edit: March 03, 2010, 03:28:34 pm by mjaredwilson » Logged

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

You can't print an array of floats in a single print, you have to use a loop.
The only type you can print an array of is "char" (a string).
« Last Edit: March 03, 2010, 03:28:51 pm by AWOL » 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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK.  I will attempt that but is this conversion from an array of characters to a float array legal:

float_arguments[index][count] = (float) arguments[index][count];

with them declared as:

 char arguments[13][11];
 float float_arguments[13][11];
Logged

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

Quote
is this conversion from an array of characters to a float array legal:

It's legal, but it won't work. The cast only works between related types. Integers can be cast to floats, because both are numeric.

Converting an array of chars to a float requires that the array of chars be NULL terminated, and the use of the atof function.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried this code with the error invalid conversion from 'char' to 'const char*'.

Code:
if(bien==6){               // If yes, continue and process the data        
           char* str = linea;              //direct linea char array to str pointer array
           char seperator = ',';
           int count = 0, index = 0, sIndex = 0;
           int theSize = 299;
           arguments[index][count] = '\0';  // clear first argument before starting
        
           while (theSize > sIndex + 1){
             if (*str != seperator){
               arguments[index][count] = *str;  //store array sections into argument arrays
               flt_arguments[index][count] = atof (*str);
             }
             else {
               arguments[index][count] = '\0';
               flt_arguments[index][count] = '\0';
             }
             *str++;
             count++;
             sIndex++;
             if (*str == seperator){
               *str++;                          // jump over the separator
               count = 0;
               index++;
               arguments[index][count] = '\0';  // clear argument before starting
               flt_arguments[index][count] = '\0';
             }
             //flt_arguments[index][count] = (float) arguments[index][count];
           }
    }
Logged

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

*str points to a character. str points to a string. atof wants a string.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.  All is right with the world, for now.  This worked:

declared flt_arguments as a one dimensional array:
float flt_arguments[13];

and the code:

Code:
if(bien==6){               // If yes, continue and process the data        
           char* str = linea;              //direct linea char array to str pointer array
           char seperator = ',';
           int count = 0, index = 0, sIndex = 0;
           int theSize = 299;
           arguments[index][count] = '\0';  // clear first argument before starting
           flt_arguments[index] = '\0';
        
           while (theSize > sIndex + 1){
             if (*str != seperator){
               arguments[index][count] = *str;  //store array sections into argument arrays
             }
             else {
               arguments[index][count] = '\0';
             }
             *str++;
             count++;
             sIndex++;

             if (*str == seperator){
               *str++;                          // jump over the separator
               count = 0;
               index++;
               arguments[index][count] = '\0';  // clear argument before starting
             }
            
           }          
                
          [glow]for(int i=1; i<10; i++){
            flt_arguments[i] = atof (arguments[i]);
          }[/glow]
          
          Serial.print("LAT: ");
          Serial.println(flt_arguments[3],4);
          Serial.print("LONG: ");
          Serial.println(flt_arguments[5],4);
}
          
« Last Edit: March 03, 2010, 08:06:29 pm by mjaredwilson » Logged

Pages: [1]   Go Up
Jump to: