Go Down

Topic: syntax problem: should I use Char, String or what? (Read 1 time) previous topic - next topic

Sirius Black

Hello everybody,
I'm trying to make a simple compass that tells you the bearing in both words and degree (ex: 359 Nord)

I got a variable called "bearing" that tell me exately the degree (0-360)
and I can get the word (north, south, west, east)

int x = bearing;
     
  if (x < 44 || x > 315)
    {
    Serial.println("NORD");
    }
   else if (x > 45 && x < 134)
    {
    Serial.println("EST");
    }
   else if (x > 135 && x < 224)
     {
     Serial.println("OVEST");
     }
   else if (x > 225 && x < 314)
     {
     Serial.println("SUD");
     }

this work fine!
What I cannot get working is to merge the two variables

I tried
Serial.println("SUD"+bearing); // return a blank line
Serial.println("SUD".bearing); // return an error, cannot compile

I also tried to use
#include <string.h>
char msg(bearing);
Serial.println("SUD"+bearing); // return again a blank line

how should I do?
thank you

AWOL

You can't add an int and a string, so use two 'print's
"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.

Sirius Black

Ok, but the
with
char(x)
I convert the int into a char

so which is the right syntax to merge two or more char variables?

example
char a="dog";
char b="is";
char c="barking";
char d=a+b+c;

Serial.println(d) //should print "dog is barking"

thank you

Groove

#3
May 25, 2010, 10:28 am Last Edit: May 25, 2010, 10:31 am by GrooveFlotilla Reason: 1
Code: [Select]
Serial.print ("SUD ");
Serial.println(bearing);


"bearing" must be an "int" (you can't store 0...359 in a byte), so it will automatically print in decimal.


Quote
char a="dog";
char b="is";
char c="barking";
char d=a+b+c;

A "char" variable can hold a single character, but "dog" "is" "barking" are all strings, so the syntax is incorrect.
Even "char" arrays cannot be added. You need to read about "strcat".

String handling is possible with correctly declared classes, but the AVR has so little memory, it is hardly worth implementing.
Per Arduino ad Astra

lloyddean

This is how it' s done.

Code: [Select]

     #include <string.h>

   const char* a   = "dog";
   const char* b   = "is";
   const char* c   = "barking";
   
   // WARNING -
   //  mutable buffer used for string building
   //  must be large enough to contain string being built including terminationing null
   char        d[256];

   memset(d, 0, sizeof(d));

   strcat(strcat(strcat(strcat(strcat(d, a), " "), b), " "), c);
   Serial.println(d);

PaulS

Code: [Select]
  char        d[256];
   memset(d, 0, sizeof(d));

If the array d is being used only for string purposes, it is not necessary to set every position to NULL. The string processing routines stop on the 1st NULL.

Code: [Select]
d[0] = '\0';
is sufficient to initialize d.

AdeV

Of these two, which will consume the least clock cycles?

1)

Code: [Select]
#include <string.h>

   char* a = "dog";
   char* b = "is";
   char* c = "barking";

   char d[256];
   memset(d, 0, sizeof(d)); // or the PaulS variation
   strcat(strcat(strcat(strcat(strcat(d, a), " "), b), " "), c);
   Serial.println(d);


or 2)

Code: [Select]

   Serial.print("dog");
   Serial.print(" ");
   Serial.print("is");
   Serial.print(" ");
   Serial.println("barking");


I've separated out the individual print statements; because in my case I will be printing some text, then a number, a separator, number, etc.; but I'm wondering if it would be more efficient to assemble the entire string in memory & pump it into Serial.println as a done deal.

AWOL

Counting cycles in the context of a serial line is usually fairly pointless.
Go with what is easier to read, understand and maintain.
"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.

PaulS

The slowest part of the code is the Serial.print, by orders of magnitude. Whether it is called once with a single string, or many times with smaller pieces, the measurable amount of time is going to occur in the Serial.print function.

AdeV

Paul - in essence, then, the quickest code will call serial.print as few times as possible?

Or - is it the string length which determines the speed (or otherwise)?

Apologies if my questions are dumb/unclear: I'm used to programming in vb.net - where I'd just concatenate my strings together & pass them off to a dedicated serial thread...


AWOL: I want to minimise the time my program spends in the serial.print/println routines, assuming they are the most risky places to be when I've got interrupts going on.

AWOL

#10
May 26, 2010, 07:02 pm Last Edit: May 26, 2010, 07:03 pm by AWOL Reason: 1
Quote
I want to minimise the time my program spends in the serial.print/println routines,


When PaulS said "orders of magnitude", he really wasn't joking.
As far as the processor is concerned, most serial is not merely glacially slowly, but bordering on the geological.

Think about it - @ 9600 baud, it takes over one millisecond to transmit a single character, on a processor executing single instructions in the time it takes for light to travel twenty metres.

Virtually all the time spent in a "print" method is simply the time the processor spends twiddling its thumbs, waiting for the transmit register to come free - no interrupts involved.

Readabilty, and maintainability every time.
"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.

Go Up