Go Down

Topic: Printing floats to the Serial port (Read 1 time) previous topic - next topic

mjaredwilson

Mar 03, 2010, 09:25 pm Last Edit: Mar 03, 2010, 09:28 pm by mjaredwilson Reason: 1
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: [Select]

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

AWOL

#1
Mar 03, 2010, 09:28 pm Last Edit: Mar 03, 2010, 09:28 pm by AWOL Reason: 1
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).
"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.

mjaredwilson

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];

PaulS

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.

mjaredwilson

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

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

PaulS

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

mjaredwilson

#6
Mar 04, 2010, 02:05 am Last Edit: Mar 04, 2010, 02:06 am by mjaredwilson Reason: 1
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: [Select]
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);
}
         

Go Up