Go Down

Topic: Arduino and SD card help (Read 1 time) previous topic - next topic

ballboii

strtok works, i have it printing out the numbers... Im just not sure how to cast them so i can store them as an integer?

SurferTim


ballboii

Thanks for all your help Tim,

I was just being a rookie towards the end and using the serial commands...

But here is the working code for anyone interested

Code: [Select]


#include <SD.h>
#include <String.h>

const int chipSelect = 4;
int i = 0;
int a = 0;

class details
{
public:
  int hours;
  int mins;
  int secs;
  int pillOne;
  int pillTwo;
};

char str[13];
char * pch;

void setup()
{

  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}


void loop()
{

  details jessica;

  File jessicaFile;

  jessicaFile = SD.open("jessica.txt");
  if(jessicaFile)
  {
    i = 0;
    while( jessicaFile.available())
    {
      char ch = jessicaFile.read();
      if(i<13)
      {
        str[i] = ch;
        Serial.write(str[i]);
        i++;
        str[i] = 0;
        delay(1000);
      }
    }
    jessicaFile.close();
  }
  else
  {
    Serial.println("error opening jessica.txt"); 

  }

  i=0;
  Serial.println("\n");
  pch = strtok(str," ,.-");
  while(pch != NULL)
  {
    switch(i)
    {
    case 0:
      jessica.hours = atoi(pch);
      break;
    case 1:
      jessica.mins = atoi(pch);
      break;
    case 2:
      jessica.secs = atoi(pch);
      break;
    case 3:
      jessica.pillOne = atoi(pch);
      break;
    case 4:
      jessica.pillTwo = atoi(pch);
      break;
    default:
      break;

    }
    i++;

    Serial.write(pch);
    pch = strtok(NULL," ,.-");

  }
  Serial.println("\nHours: ");
  Serial.print(jessica.hours);
  Serial.println("\nMinutes: ");
  Serial.print(jessica.mins);
  Serial.println("\nSeconds:");
  Serial.print(jessica.secs);
  Serial.println("\nPill One:");
  Serial.print(jessica.pillOne);
  Serial.println("\nPill Two: ");
  Serial.print(jessica.pillTwo);
  Serial.println();

  delay(10000);

}



SurferTim

#18
Jul 04, 2012, 02:01 pm Last Edit: Jul 04, 2012, 02:51 pm by SurferTim Reason: 1
Good to hear it is working, but you are still overflowing the str array.
Code: [Select]
// this is set for 13 characters
char str[13];

// then later in the code
     if(i<13)
     {
       str[i] = ch;
       Serial.write(str[i]);
       i++;
       // If i == 12, this will store zero in str[13] (14th position)
       str[i] = 0;
       delay(1000);
     }


add: Overflowing arrays can have embarrassing results. Try this code.  :smiley-eek:
Code: [Select]
char test[4] = {'a','b','c','d'};
char between[3] ={'1','2','3'};
char test2[4] = {'d','u','c','k'};
char testend = 0;

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

 Serial.println(test);    
 Serial.println(between);
 Serial.println(test2);
 // this is changing the test array, not test2
 test[7] = 'f';
  // now print test2
 Serial.println(test2);
}

void loop() {
}

PaulS

Quote
But I don't think strtok will work with a string that is not zero terminated, so you may need to find another way of parsing the array.

It will, but the results will not be what you expect. Somewhere in memory, after the address that is the start of the array, there will be a NULL. strtok() will keep processing until it finds it. The NULL is its clue (as with all string functions) that it has reached the end of the array.

If you really are too lazy to increase the size of the array by 1, and to NULL terminate the array, that's fine. Simply do not use any string handling functions (strtok, strcat, strcpy, atoi(), etc.).

Really, learning to deal with NULL terminated arrays of chars will be quite useful in most everything you do with the Arduino, and is worth taking the time and effort to learn how to do it properly.

Go Up