Pages: 1 [2]   Go Down
Author Topic: Arduino and SD card help  (Read 1193 times)
0 Members and 1 Guest are viewing this topic.
Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

atoi
Logged

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

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:

#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);

}


Logged

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

Good to hear it is working, but you are still overflowing the str array.
Code:
// 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:
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() {
}
« Last Edit: July 04, 2012, 07:51:54 am by SurferTim » Logged

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

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.
Logged

Pages: 1 [2]   Go Up
Jump to: