sdcard read line by line

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

sdcard read line by line

Post by simondid »

hey so im traying to read a sdcard line by line the text file on the sdcard looks like this
navn:margarita1
mengde:1,2,3,4,5
navn:margarita2
mengde:1,2,3,4,5
navn:margarita3
mengde:1,2,3,4,5
navn:margarita4
mengde:1,2,3,4,5
navn:margarita5
mengde:1,2,3,4,5
navn:margarita6
mengde:1,2,3,4,5
navn:margarita7
mengde:1,2,3,4,5
navn:margarita8
mengde:1,2,3,4,5
navn:margarita9
mengde:1,2,3,4,5
navn:margarita10
mengde:1,2,3,4,5
i need to seperate by " navn:" and by "mengde,"

i can read the sdcard and print it to serial with serial.write but thats the only thing i can.
when im traying to test for "\n" ore "\par" i'm getting errors any one know how to handle this problem ???

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: sdcard read line by line

Post by adafruit_support_rick »

Code: Select all

#include "SD.h"
#include <SPI.h>

#define MAX_STRING_LEN 32
char inStr[MAX_STRING_LEN];
File txtFile;
bool validStr;

...  etc ...

  if (txtFile.available())
  {
    int i = 0;
    validStr = false;
    char c;
    do      //read characters until we find a new line, hit end of file, or run out of room in our string buffer.
    {
      c = txtFile.read();
      inStr[i++] = c;
    } 
    while ((c != '\n') && (i < MAX_STRING_LEN) && (txtFile.available()));

    validStr = (c == '\n');   //input string is valid if we read a new line

    if (!validStr)   //if string is not valid, keep reading until we find a new line or run into the end of the file
    {
      while ((c != '\n') && (txtFile.available()))
      {
        c = txtFile.read();
      }
    }
  }
You can use the strstr function to locate "navn:" and "mengde:"
E.g.:

Code: Select all

  if (validStr)
  {
    if (strstr(inStr, "navn:"))
    {
      ..etc ..
    }
    else
    {
      if (strstr(inStr, "mengde:"))
      {
        ..etc ..
      }
    }
  }


User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

hey so i been reading ture youre code and i have it kinda working :P
so my problem now is that the code only reads one line off code and for some wired reason i can't just for loop it 10 times :(

here is youre code i with my changes but i can't get it reading the next line :(

Code: Select all

#define MAX_STRING_LEN 32
char inStr[MAX_STRING_LEN];
File txtFile;
bool validStr;

void setup()
{
	Serial.begin(9600);
	Serial.println("hey");
	pinMode(10, OUTPUT);
	if (!SD.begin(8)) {
	Serial.println("sdcard initialization failed!");
	return;
	}
 Serial.println("initialization done.");
 
 
 
	txtFile = SD.open("data.txt");
	
	if(txtFile){
		while(txtFile.available()){
			Serial.write(txtFile.read());
		}
	}

	
	txtFile.close();
	txtFile = SD.open("data.txt");
	
 if (txtFile) {
	 Serial.println("data.txt:");
	
	 
	if (txtFile.available())
	{
		for (int r =0;r<9;r++)
		{
		
		int i = 0;
		validStr = false;
		char c;
		if (r!=0)
		{
		
		 c = txtFile.read();
		}
		do      //read characters until we find a new line, hit end of file, or run out of room in our string buffer.
		{
			c = txtFile.read();
			inStr[i++] = c;
		}while ((c != '\n') && (i < MAX_STRING_LEN) && (txtFile.available()));

		validStr = (c == '\n');   //input string is valid if we read a new line

		if (!validStr)   //if string is not valid, keep reading until we find a new line or run into the end of the file
		{
			while ((c != '\n') && (txtFile.available()))
			{
				c = txtFile.read();
			
			}
		}
		
		if (validStr)
		{
			if (strstr(inStr, "navn:"))
			{
				Serial.println("hej1");
				for (int t =5; t<MAX_STRING_LEN;t++)
				{
					
					Serial.write(inStr[t]);
				}
			}
			else
			{
				if (strstr(inStr, "mengde:"))
				{
					Serial.println("hej2");
					
					for (int t =7; t<MAX_STRING_LEN;t++)
					{
					
						Serial.write(inStr[t]);
					}
					
				
 				}
					}
		}
				}
				
		
	}
	
	 txtFile.close();
	
	 } else {
	
	 Serial.println("error opening data.txt");
	 }
	
 Serial.println("setup don");
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: sdcard read line by line

Post by adafruit_support_bill »

One problem is that you don't clear the contents of "inStr" after each line.

User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

ya that kinda makes sens ;) did not even think about that :(

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: sdcard read line by line

Post by adafruit_support_rick »

What is this for?

Code: Select all

      for (int r =0;r<9;r++)
That would explain why you can't read 10 lines.

All of the file reading should be in your loop() function, not in setup()

User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

the for loop was to red the 10 lines.

and thanks for the hint about the loop function

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: sdcard read line by line

Post by adafruit_support_rick »

Oh, I see - you only have 10 lines of text. In that case, your for loop should be:

Code: Select all

      for (int r =0; r<=9; r++)
Also, since you only want to read the 10 lines and then stop, everything should probably stay in setup(), just the way you have it.

User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

okay so the only reason to move it to loop wut be if it was like a realy long task ?

for some reason that loop dos not read out 5 times navne and 5 times menge :( any idear why ??
also my bad had forgotten that it needs to read 20 lines

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: sdcard read line by line

Post by adafruit_support_rick »

If you want a sketch to only read 10 (or 20) lines and then stop entirely, it doesn't make a lot of sense to put the code in loop, where it will continue to execute.

Your sketch may fail to read the last line if the last line of the file does not end with a newline character.

User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

thanks alot just goth every thing working now i just have one more prolem i have found a libary a lcd screen with helps me create menus and it needs the menu names to the be in a char* array

Code: Select all

char* drinks[10] = {"1","2","3","4","5","6","7","8","9","10"};[/quote]

but i can't figur out how to get the diffrent names from my string array into this char array...
and if you know it due you know what * stands for ? and how it affecs the variable ??

here is my finished code for reading the sdcard lines into 2 difrent string arrays 

[quote]/*
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 8
*/

#include "SD.h"

#define MAX_STRING_LEN 32
char inStr[MAX_STRING_LEN];
File txtFile;
bool validStr;

int drinksArraySize = 10;
String drinks[10] = {"1","2","3","4","5","6","7","8","9","10"};
int navnCount =0;

int recipsArraySize = 10;
String recips[10] = {"1","2","3","4","5","6","7","8","9","10"};
int recipsCount =0;

void setup()
{
	Serial.begin(9600);
	Serial.println("hey");
	readSdCardData();
	
	 
 Serial.println("setup don");
}

void loop()
{

  /* add main program code here */

}

void readSdCardData(){
	
	pinMode(10, OUTPUT);
	if (!SD.begin(8)) {
		Serial.println("sdcard initialization failed!");
		return;
	}else{
	Serial.println("initialization done.");
	}
	
	txtFile.close();
	txtFile = SD.open("data.txt");
	
	if (txtFile) {
		Serial.println("data.txt:");
		
		for (int r =0;r<20;r++)
		{
			for(int i =0;i<MAX_STRING_LEN;i++){
				inStr[i] = NULL;
			}
			if (txtFile.available())
			{
				
				
				int i = 0;
				validStr = false;
				char c;
				
				
				do      //read characters until we find a new line, hit end of file, or run out of room in our string buffer.
				{
					c = txtFile.read();
					inStr[i++] = c;
				}while ((c != '\n') && (i < MAX_STRING_LEN) && (txtFile.available()));

				validStr = (c == '\n');   //input string is valid if we read a new line

				if (!validStr)   //if string is not valid, keep reading until we find a new line or run into the end of the file
				{
					while ((c != '\n') && (txtFile.available()))
					{
						c = txtFile.read();
						
					}
				}
				
				
				delay(50);
				if (validStr)
				{
					if (strstr(inStr, "navn:"))
					{
						delay(50);
						//Serial.println("hej1");
						
						String str(inStr);
						str.replace("navn:",NULL);
						drinks[navnCount] = str;
						
						navnCount++;
					//	Serial.println("");
					}
					else
					{
						if (strstr(inStr, "mengde:"))
						{
							delay(50);
						//	Serial.println("hej2");
							
							String str(inStr);
							str.replace("mengde:",NULL);
							
							recips[recipsCount] = str;
							
							recipsCount++;
							//Serial.println("");
							
						}
					}
					
					
				}
			}
			
			
		}
		
		
		
		} else {
		
		Serial.println("error opening data.txt");
	}
	txtFile.close();
	
	
	//Serial.println("");
	//Serial.println("");
// 	for (int i=0;i<drinksArraySize && i<recipsArraySize;i++)
// 	{
// 		Serial.println(drinks[i]);
// 		Serial.println(recips[i]);
// 	}
	
}
Last edited by adafruit_support_rick on Mon Apr 21, 2014 6:20 am, edited 1 time in total.
Reason: please use Code tags when posting code (</> button)

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: sdcard read line by line

Post by adafruit_support_rick »

Do you want to use a char* array, like this

Code: Select all

char* drinks[10]
or do you want to use a string array, like this:

Code: Select all

String drinks[10]
If you want to use a char* array, what is the longest possible string you are expecting to read?

User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

The longest type og navn is 27 limitede Bay The code reading from The sd card

so 10 Strings of type navn: with a max off 27 characters

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: sdcard read line by line

Post by adafruit_support_rick »

Modify your code like this:

Code: Select all

int drinksArraySize = 10;
char* drinks[drinksArraySize];
int navnCount =0;

Code: Select all

               if (strstr(inStr, "navn:"))
               {
                  delay(50);
                  //Serial.println("hej1");
                  
                  String str(inStr);
                  str.replace("navn:",NULL);
                  int len = str.length();
                  char* strBuffer = malloc(len + 1);
                  memset(strBuffer, 0, len + 1);
                  str.toCharArray(strBuffer, len);
                  drinks[navnCount] = strBuffer;
                  
                  navnCount++;
               //   Serial.println("");
               }

User avatar
simondid
 
Posts: 38
Joined: Sat Mar 01, 2014 9:46 am

Re: sdcard read line by line

Post by simondid »

hey thanks for the replay the code you postet gives me an error saying
sketch_apr21a.ino: In function 'void readSdCardData()':
sketch_apr21a:228: error: invalid conversion from 'void*' to 'char*'
line 228 is
char* strBuffer = malloc(len + 1);

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”