Go Down

Topic: char to int problem? help (Read 745 times) previous topic - next topic

Rivinoo

Apr 30, 2013, 06:39 pm Last Edit: May 09, 2013, 06:08 pm by Rivinoo Reason: 1
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: [Select]
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: [Select]
Serial.write(chartoint("1"));Serial.write("-");
Serial.write(chartoint("12"));Serial.write("-");
Serial.write(chartoint("123"));Serial.write("\n");

I have this:
Code: [Select]

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


NOTE: i try my algorithm in c++ and it works, but my arduino mega don't like it.

semicolo

use print instead of write

Rivinoo

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: [Select]
2;2;317;6
2;2;6;237
313;2;317;237
2;233;317;237
50;50;200;55



Code: [Select]
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: [Select]
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? :(


Msquare

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy