Pages: [1]   Go Down
Author Topic: Char Array erratic readings (Solved)  (Read 944 times)
0 Members and 1 Guest are viewing this topic.
Monterey, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I am trying to convert an int to show up in a char array. I have tried several methods like itoa, atoi, assign an int to a string then call string.tochararray, call a specific char of the string after being converted to an int, and I have run out of ideas.

Anything commented out is an example of what I tried.
Code segment:
Code:
void loop()
{
if(OnStart==0)
{
  while(Config1<MaxPresets)
  {  Config1++;
    
    CurrentFileName[indexToWrite]=(indexToWrite&0xf)|0x30;
 
    CurrentFileName[indexToWrite]='/';
    CurrentFileName[1+indexToWrite]='P';
    CurrentFileName[2+indexToWrite]='r';
    CurrentFileName[3+indexToWrite]='e';
    CurrentFileName[4+indexToWrite]='s';
    CurrentFileName[5+indexToWrite]='e';
    CurrentFileName[6+indexToWrite]='t';
    CurrentFileName[7+indexToWrite]='s';
    CurrentFileName[8+indexToWrite]='/';

    //itoa(Config1,CurrentFileLine,10);

    //IntToString+=Config1;
    //IntToString.toCharArray(IntToStringBuf,50);

    CurrentFileName[9+indexToWrite]=Config1;  //[color=green]This line is where I try to call the int of Config1[/color]

    //CurrentFileName[10+indexToWrite]=IntToString[1];
    //itoa(Config1,CurrentFileLine,10);
    //CurrentFileName[9+indexToWrite]=CurrentFileName&&;
    //CurrentFileName[9+indexToWrite]='\0';
    indexToWrite++;
    //CurrentFileName[indexToWrite]=CurrentFileName;
    //CurrentFileName[indexToWrite]=CurrentFileLine;
  if(Config1>9){indexToWrite++;}if(Config1>99){indexToWrite++;}if(Config1>999){indexToWrite++;}
    CurrentFileName[11+indexToWrite] = '.';
    CurrentFileName[12+indexToWrite] = 't';
    CurrentFileName[13+indexToWrite] = 'x';
    CurrentFileName[14+indexToWrite] = 't';
    CurrentFileName[15+indexToWrite] = '\0'; // terminator
    indexToWrite = 0; // reset pointer for next time we enter    
//  if(!SD.exists(CurrentFileName)){
      CurrentFile=SD.open(CurrentFileName,FILE_WRITE);
      FileFunction();
      CurrentFile.close();
      Serial.print("File not found, creating: ");
      Serial.print(CurrentFileName);
      Serial.println();
//    }
  }
    Serial.println("Initialization Complete!");
    digitalWrite(13,HIGH);
    OnStart=1;
  }
}
I would think its something wrong with my array, rather than the method that I'm trying to call the int. I can get it to work, but it doesn't return the current number of Config1, but rather several letters, including ones with different umlauts and whatnot.
Am I not converting ASCII/HEX/DEC/BIN or something similar correctly?

Thanks for any help!
« Last Edit: September 25, 2012, 09:44:00 pm by MattiasOfTheMetal » Logged

Food for thought.
Nom.

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 146
Posts: 6027
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use sprintf for array construction. Like this:
Code:
int fileNumber = 6;
char outBuf[32];

sprintf(outBuf,"fileNumber = %d",fileNumber);
Serial.println(outBuf);
Logged

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

Code:
    CurrentFileName[indexToWrite]=(indexToWrite&0xf)|0x30;
 
    CurrentFileName[indexToWrite]='/';
The first statement is accomplishing nothing, as the second one overwrites the same position in the array.

Code:
    CurrentFileName[9+indexToWrite]=Config1;
What is the value of Config1? It must be in the range 0 to 9 for this to work. If it is, then the simplest thing to do is:
Code:
    CurrentFileName[9+indexToWrite]=Config1 - '0';
Logged

Monterey, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@SurferTim I will give that a shot when I get home today, thanks!

@PaulS I figured the first line was redundant, I just wasn't sure. As for the value of Config1, it starts at zero and increments each time the while loop passes until it is equal to MaxPresets, which is at 100 right now. I plan to make it capable of being whichever size the user will want, unless it's too difficult, then I will just limit it to around 200.
Logged

Food for thought.
Nom.

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

Suppose that Config1 does get to be 200. As a string, that is "200". How do you intend to fit those three characters in one array element that can hold one character?

I do believe that the SD library limits file names to 8.3 format, meaning that your 14+ character names are not going to work.

Personally, I'd be looking at strcat() and/or sprintf() to construct the file name in one step instead of adding each character to the name one at a time.
Logged

Monterey, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The strcat worked great!! Thanks so much PaulS!
I will post my code to help out any future readers, I know I've lurked hoping to find info, but found none because the OP would figure it out and not leave their knowledge. So here it is:

Code:
//Vars:
char CurrentFileName[20];
String DirectoryString[10];
String NameString[6];
String ExtensionString[4];

char* SDFileDirectory(char* DirectoryString, char* NameString, char* ExtensionString) {
  CurrentFileName[0] = 0;          // start with a null string:
  strcat(CurrentFileName, DirectoryString);   // add first string, in this case the directory for use with an sd card
  strcat(CurrentFileName, itoa(Config1,NameString,10));  //add my Config1 incrementationilizer, I used mine to name the files: 1.txt,2.txt~~100.txt
  strcat(CurrentFileName, ExtensionString);}  //The file extension


void loop()
{
if(OnStart==0)
{
  if(!SD.exists("Config.txt"))  // This hub is used to CREATE the config if there isn't one already
  {
    CurrentFile=SD.open("Config.txt",FILE_WRITE);
    CurrentFile.print("Maximum presets :  ");
    CurrentFile.println(MaxPresets);
    CurrentFile.close();
  }

  CurrentFile=SD.open("Config.txt");  //Then proceed to read info from the file, it is closed prior then reopened as if there is a config file, it will still be able to open this one:
  if(CurrentFile)
  {
    while(CurrentFile.available())
    {
      Serial.write(CurrentFile.read());
    }
    CurrentFile.close();
  }

  while(Config1<MaxPresets) 
  {  Config1++;

      SDFileDirectory("/Presets/","",".txt");//This is the function that PaulS helped me with, 1: Directory, 2: left blank/reserved for SDFileDirectory function up at the top, 3: File extension. 1 and 3 can be renamed here/wherever this line is called, and 2 is renamed in the function, I have not yet figured out how to change what var it uses, but only to change the value of Config1. If anyone can help on this, please let me know

  if(!SD.exists(CurrentFileName)){
      CurrentFile=SD.open(CurrentFileName,FILE_WRITE);
      FileFunction();
      CurrentFile.close();
      Serial.print("File not found, creating: ");
      Serial.print(CurrentFileName);
      Serial.println();
    }
  }
    Serial.println("Initialization Complete!");
    OnStart=1;
  }
Logged

Food for thought.
Nom.

Pages: [1]   Go Up
Jump to: