Pages: [1]   Go Down
Author Topic: Arduino004 and C string functions  (Read 1168 times)
0 Members and 1 Guest are viewing this topic.
Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, i've got some prob with the C++ string functions

my code worked well in arduino003 now, i've got an error

Quote
error invalid conversion from 'const char*' to 'char'

i think i understand what it mean but i don't know how to resolve the prob.
is it about my string copy ?
and how to modify the char type

Code:
// init poeme
int jour = 0;
int nJour = 17;
int mois = 2;
int an = 2;
char chaine[65] = " ";
char ligne[0x10] = "";
int jMax[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
const char *sJour[33] = {"", " premier", " deux", " trois", " quatre", " cinq", " six", " sept", " huit", " neuf", " dix", " onze", " douze", " treize", " quatorze", " quinze", " seize", " dix-sept", " dix-huit", " dix-neuf", " vingt", " vingt-et-un", " vingt-deux", " vingt-trois", " vingt-quatre", " vingt-cinq", " vingt-six", " vingt-sept", " vingt-huit", " vingt-neuf", " trente", " trente-et-un", " mille"};
const char *sMois[13] = {"", " janvier", " fevrier", " mars", " avril", " mai", " juin", " juillet", " aout", " septembre", " octobre", " novembre", " decembre"};
const char *sAnnee[8] = {"", ""," deux mille deux", " deux mille trois", " deux mille quatre", " deux mille cinq", " deux mille six"};
const char *sNomJour[7] = {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"};


void setup (void) {
  Serial.begin(115200);
  delay(100);                
}
void loop (void) {
  int i = 0;
  int j = 0;

  if (jour > 6) {jour = 0;}
  if (nJour > jMax[mois]) {nJour = 1; mois++;}
  if (mois == 2 && an == 3) { jMax[2] = 29;} else { jMax[2] = 28;}
  if (mois > 12) {mois = 1; an++;}

  // concatenation des chaines
  *chaine = " ";
  strcpy(chaine, sNomJour[jour]); // dimanche
  strcat(chaine, sJour[nJour]); // dix-sept
  strcat(chaine, sMois[mois]); // Février
  strcat(chaine, sJour[2]); // deux
  strcat(chaine, sJour[32]);// mille
  strcat(chaine, sJour[an]); // deux
  Serial.print(chaine);


  jour++;
  nJour++;
  // ecriture des lignes
  strncpy(ligne,chaine,16);
  if (strlen(ligne) >= 16) {
    ligne[16] = '\0';
  }
  // ecrit la ligne
  Serial.print(ligne);
  delay(100);
  *ligne=" ";
  for (i=2; i<=4;i++) {
    strncpy(ligne,&chaine[16*(i-1)],16);
    if (strlen(ligne) >= 16) {
      ligne[16] = '\0';
    }
    // ecrit la ligne
    Serial.print(ligne);
    delay(100);
    *ligne=" ";
  }
}

i was just trying to understand a bit C, now it is C++ :'(

any solution or a begin of solution ?

regards

eric
Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char *chaine = " ";
char *ligne = "";

i've just changed these two line and take off any *chaine = " " or *ligne=" " in  the loop()...

but pb compilation on serial...

eric
« Last Edit: April 30, 2006, 10:40:48 am by mrbbp » Logged

Forum Administrator
Offline Offline
Newbie
*****
Karma: 3
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what do you mean with "but pb compilation on serial..." ?

massimo
Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i mean this :
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1146411379

but now i haven't got any prob with serial error... :-X

eric
« Last Edit: April 30, 2006, 02:16:12 pm by mrbbp » Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've tried this code

Code:
int ledPin = 13;
// init poeme
int jour = 0;
int nJour = 17;
int mois = 2;
int an = 2;
char *chaine = " ";
char *ligne = " ";
int jMax[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *sJour[33] = {"", " premier", " deux", " trois", " quatre", " cinq", " six", " sept", " huit", " neuf", " dix", " onze", " douze", " treize", " quatorze", " quinze", " seize", " dix-sept", " dix-huit", " dix-neuf", " vingt", " vingt-et-un", " vingt-deux", " vingt-trois", " vingt-quatre", " vingt-cinq", " vingt-six", " vingt-sept", " vingt-huit", " vingt-neuf", " trente", " trente-et-un", " mille"};
char *sMois[13] = {"", " janvier", " fevrier", " mars", " avril", " mai", " juin", " juillet", " aout", " septembre", " octobre", " novembre", " decembre"};
char *sAnnee[8] = {"", ""," deux mille deux", " deux mille trois", " deux mille quatre", " deux mille cinq", " deux mille six"};
char *sNomJour[7] = {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"};


void setup (void) {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);                 
}
void loop (void) {
  int i = 0;
  int j = 0;

  if (jour > 6) {jour = 0;}
  if (nJour > jMax[mois]) {nJour = 1; mois++;}
  if (mois == 2 && an == 3) { jMax[2] = 29;} else { jMax[2] = 28;}
  if (mois > 12) {mois = 1; an++;}

  // concatenation des chaines
  //*chaine = " ";
  strcpy(chaine, sNomJour[jour]); // dimanche
  strcat(chaine, sJour[nJour]); // dix-sept
  strcat(chaine, sMois[mois]); // Février
  strcat(chaine, sJour[2]); // deux
  strcat(chaine, sJour[32]);// mille
  strcat(chaine, sJour[an]); // deux
  Serial.print(chaine);
  Serial.println();


  jour++;
  nJour++;
  // ecriture des lignes
  strncpy(ligne,chaine,16);
  if (strlen(ligne) >= 16) {
    ligne[16] = '\0';
  }
  // ecrit la ligne
  //Serial.print(ligne);
  Serial.print(ligne);
  Serial.println();
  delay(100);
  //*ligne=" ";
  for (i=2; i<=4;i++) {
    strncpy(ligne,&chaine[16*(i-1)],16);
    if (strlen(ligne) >= 16) {
      ligne[16] = '\0';
    }
    // ecrit la ligne
    //Serial.print(ligne);
    Serial.print(ligne);
    Serial.println();
    delay(100);
    //*ligne=" ";
  }
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);
}

no compilation error
done uploading
but nothing appear on the serial monitor window and no blinking led

what's wrong ?

regards

eric
« Last Edit: April 30, 2006, 02:15:19 pm by mrbbp » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

First, you have the Serial Monitor Baud Rate set to 115200, right?

Also, you're not allocating any room for chaine and ligne, so when you strcpy into them, you're overwriting random bits of RAM, which tends to cause problems.  Try something like:

Code:
char chaine[100];
char ligne[100];

or, to allocate space and initlialize the strings:

Code:
char chaine[100] = { 0 };
char ligne[100] = { 0 };

or don't bother with strcpy or strcat at all and just print the pieces one at a time.
« Last Edit: April 30, 2006, 03:38:14 pm by mellis » Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the tips mellis, but that doesn't solve my prob.

how can i "reset" my string in loop()?

if i write :
 *ligne=" ";
i've got this error :
invalid conversion from 'const char*' to 'char'
"ligne" is not a constant char*...
does it mean that the strcpy is only a copy of pointers and not a "physical" copy of char's array ?
i'm a bit confused!

if i compile with arduino003, that's works.
but not with arduino004...

eric
« Last Edit: April 30, 2006, 04:17:41 pm by mrbbp » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The strcpy() will "reset" the string.  I.e.

Code:
char foo[100];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  strcpy(foo, "hello");
  Serial.println(foo);
  strcpy(foo, "world");
  Serial.println(foo);
}

will send:
hello
world
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

P.S.  The problem with
Code:
*ligne=" ";
is that *ligne is a character (the same as ligne[0]), but " " is a string.  To create a character constant, use single quotes: 'a'.  Thus, to assign to a particular character of the string:
Code:
ligne[3] = 'b';
or
Code:
*ligne = 'b';
or even
Code:
*(ligne + 3) = 'b';
 To "reset" the string in this way, you want to assign the character with ASCII code zero (not the character '0' which has ASCII code 48) to the first character in the string:
Code:
*ligne = '\0';
or
Code:
*ligne = 0;
Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 :-* yesssss

i think, i've finally understood.
many thanks.

eric
Logged

Pages: [1]   Go Up
Jump to: