Converting Float to Char for ASCII Byte manipulation - Help with Char* to String

I have a compass sensor that requires the data to be a float (needs to be divided by Pi for radian calculation) and leads to many decimal places of unneeded precision. My application only requires a rounded integer that will range between 0-360. I would like to convert the integer to a string in order to add special ASCII characters as a prefix and another as a suffix. For example, the heading due EAST would give a string “@090$” with a null at the end and due South would be represented a string “@180$” so I need a fixed 6 byte char array

I found the fastest code to covert the float data to a rounded string is a function with:

void setup() {
  Serial.begin(19200);
  // next code to Initialise the sensor
 }

void loop(void) 
{
  char* NewHeading;
  NewHeading = GetNewHeading();
  Serial.println("New Heading: ");
  Serial.println(NewHeading);
  // Serial.println(NewHeading.length());  THIS DOESNT COMPILE ERROR NewHeading not of type char* //
  delay (1000);
}

char* GetNewHeading()
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);

  float Pi = 3.14159;
  float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
  if (heading < 0)
    {
      heading = 360 + heading;
    }
  else if (heading > 360)
    {
      heading = heading - 360;
    }  
      
  //remove decimals 
  //structure of dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
  dtostrf(heading,2, 0, compass);  
  return compass;
}

I thought the function: GetNewHeading() could be declared as a char but I could not get it to return a string and it only works by declaring the function with char*

This works so Serial.print (NewHeading) shows the correct value but I am stuck in not being to manipulate NewHeading with any string functions, such as String.length; The compiler v 1.05 gives an error: NewHeading not of type char*

r Trim I searched but have not found a clear example. Any help appreciated.

thanks
jerry

My application only requires a rounded integer that will range between 0-360

Do this...

int compass = heading;

I would like to convert the integer to a string in order to add special ASCII characters as a prefix and another as a suffix.

Do this...

char compassString[8];
sprintf(compassString,"@%d$",compass);

thank you !

How do I return compassString from the function back to Loop() ?

I tried declaring the function as char but that did not work I had to declare the function as char* ?

jerry

Return compass to the caller, and convert to string in the loop function. Returning strings from functions is tricky, you should probably avoid that.

OK, that sounds good !! So in general no easy way to pass fix length strings between functions outside of Loop() ??

The easiest is to create a global fixed buffer.

The harder ways involve static buffers in the function, which use the same amount of storage as far as the computer is concerned, but have a more friendly scope - they cannot be seen outside of the function.

Thanks Keith,

So if I created at the top of my declarations:

char compassString[8];

and converted the integer 'compass' to string 'compassString' in my GetHeading() function, then 'compassString' should be accessible as a string in Loop() , correct? I have to try that again as that's how i started my programming but somehow the compiler didnt like it. I will try again and report back

Many thanks to both of you !!

jerry

There may be some confusion between Strings and strings (what's new ?)
NewHeading.length()is a String function
strlen(NewHeading)is a string function

I have googled and read the reference library and unfortunately still not clear to me. In your example 'NewHeading" is the same string identity. So what is the difference in the result obtained between:

NewHeading.length();
versus
strlen(NewHeading);

do both return the same integer?

k1jos:
So what is the difference in the result obtained between:

NewHeading.length();
versus
strlen(NewHeading);

One works for Strings, the other works for strings.

k1jos:
In your example 'NewHeading" is the same string identity.

As they say in all the best Pantomimes "Oh No its not !"

NewHeading.length();
versus
strlen(NewHeading);

do both return the same integer?

One of the things that I like about the Arduino is the ease with which things can be tried for oneself.....