Pages: [1]   Go Down
Author Topic: char to int problem? help  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi people, I have a problem. I read a text file from a MicroSD file built into TFT 3.2' touchscreen display.
I checked data reading via serial communication and it is ok.
now I must convert my char number to int.
I tried "number=atoi(string);" but it makes error.
So I decided to write my chartoint algorithm but it makes the same error!
it is my algorithm:
Code:
int chartoint(char string[])
{
  int k,length,number;
  int position=1;
  number=0;
  // length of the string
  for(k=0;string[k]!=0;k++);
  length=k;
  // string to int
  for(k=length-1;k>=0;k--)
  {
    number+=(int(string[k])-48)*position;
    position=position*10;
  }
  return number;
}
if I try:
Code:
Serial.write(chartoint("1"));Serial.write("-");
Serial.write(chartoint("12"));Serial.write("-");
Serial.write(chartoint("123"));Serial.write("\n");

I have this:
Code:

 - - {
 - - {
 - - {
 - - {

NOTE: i try my algorithm in c++ and it works, but my arduino mega don't like it.
« Last Edit: May 09, 2013, 11:08:16 am by Rivinoo » Logged

Quebec
Offline Offline
Jr. Member
**
Karma: 3
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

use print instead of write
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Works! But I have another problem:
Looks my code.
This is my file content, which are the position of wall (for my labyrinth project):
Code:
2;2;317;6
2;2;6;237
313;2;317;237
2;233;317;237
50;50;200;55


Code:
void function()
{
  int x1[10],x2[10],y1[10],y2[10];
  int res,wall_number;
  char row[20],number_string[10];
  int k;
 
  /////////////////////////////////////////////////////
  ////////////GET WALL POSITION FROM MICROSD///////////
  Serial.begin(9600);
  res=file.openFile("liv1.txt", FILEMODE_TEXT_READ);
  if (res==NO_ERROR)
  {
    k=0;
    myGLCD.setColor(255,255,0);
    myGLCD.clrScr();
    do
    {
      res=file.readLn(row,20);
      extract(row,1,number_string);
      x1[k]=chartoint(number_string);
      extract(row,2,number_string);
      y1[k]=chartoint(number_string);
      extract(row,3,number_string);
      x2[k]=chartoint(number_string);
      extract(row,4,number_string);
      y2[k]=chartoint(number_string);
      //monitoring the array's content
      Serial.print(x1[k]);
      Serial.print("-");
      Serial.print(y1[k]);
      Serial.print("-");
      Serial.print(x2[k]);
      Serial.print("-");
      Serial.print(y2[k]);
      Serial.print("\n");
      myGLCD.fillRect(x1[k],y1[k],x2[k],y2[k]);
      k++;
    }while(res!=EOF);
    file.closeFile();
  }
  wall_number=k;
  //////////////////////////////////////////////////
 /////// NOW I REREAD THE CONTENTS OF ARRAY////////
  Serial.print("\n");
 
  for(k=0;k<wall_number;k++)
  { 
    Serial.print(x1[k]);
    Serial.print("-");
    Serial.print(y1[k]);
    Serial.print("-");
    Serial.print(x2[k]);
    Serial.print("-");
    Serial.print(y2[k]);
    Serial.print("-");
    Serial.print("\n");
  }
}

// (THIS FUNCTION WORK SUCCESSFULLY)
// void extract(char FROM_WHERE,int POSITION,char TO_WHERE)
//
void extract(char esaminare[],int campo,char restituire[])
{
 int k,contatore=0,z;
 campo--;
 for(k=0;contatore<campo&&esaminare[k]!=10;k++)
    if(esaminare[k]==';')
         contatore++;
 if(esaminare[k]!=13)
   {
   for(z=k;esaminare[z]!=';'&&esaminare[z]!=10;z++)
       restituire[z-k]=esaminare[z];
   restituire[z-k]=0;
   }
 else
   restituire[0]=0;
}



Serial Monitor give me that result:
Code:
2-2-317-6
2-2-6-237
313-2-317-237
2-233-317-237
50-50-200-55

0-1024--32000-0-
110--28671-181-237-
25977-2-30976-237-
0-1281-4-237-
15-11776-4-55-

The values of my array are changhd!! What could have happened? smiley-sad
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

up
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1199
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Having only seen a fraction of the sketch (and no personal experience iwth GLCD library) there are two "standard" answers which may or may not apply to your problem:

1: Some other part of your code has some array access that access beyond aoms array bounds, overwriteing your x, y, z arrays
2: You have run out of memory and the stack is overwriting your arrays. Use "memoryFree" or similar utility to print out how much RAM memory you have left. Note: This value varies if you call it in the loop() or inside one of your routines, and indeed inside the GLCD library.
Logged

Pages: [1]   Go Up
Jump to: