Pages: [1]   Go Down
Author Topic: syntax problem: should I use Char, String or what?  (Read 1016 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

You can't add an int and a string, so use two 'print's
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: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: May 25, 2010, 03:31:00 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is how it' s done.

Code:
     #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);
Logged

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

Code:
  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:
d[0] = '\0';
is sufficient to initialize d.
Logged

Liverpool, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

1)

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

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

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

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

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

Liverpool, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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.
« Last Edit: May 26, 2010, 12:03:08 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.

Pages: [1]   Go Up
Jump to: