Go Down

Topic: Save changes into SD (Read 2 times) previous topic - next topic

PaulS

Quote
I'm still trying thing and looking for information on internet with out results by now.

Two possible avenues to explore. One would be to take your snippets over to snippets-r-us.com. The other, and this is far more likely to prove useful, is to post ALL of your code here.

We can see from that snippet and output that you can read the card correctly, but no where do you actually save that data in arrays or anywhere where you can compare input to.

Why the failure to compare serial data to card data happens is a mystery because you didn't post that code.

tuxduino

So there were the contents of the file:

Code: [Select]
char user1[]=67202;
char user2[]=62601;
char user3[]=60920;
char user4[]=61014;


right ?

You were not expecting Arduino to include a C-parser, did you ? :)

I'd put one number on each line and write a function that would look more or less like this:

declare char readBuffer
declare int cnt=0

read one byte from file
if no more bytes, return
if the byte is CR or LF
    readBuffer[cnt] =0
    parse readBuffer to an integer, and store that as a new user id into the user ids array
else
    store the byte in readBuffer
    cnt++
endif

dgelectron

Hi guys thanks for your help.

Right now i made a smaller program just to learn how to use SD properly. I have a txt file in the Sd (user.txt) with 4 users codes. For now i im trying to save it like: user[1]=67202....user[4]61014 but ir doesn't work. The serial shows user[1]=59, user[2]=59, user[3]=59,user[4]=59. Here is my code.

67202
62601
60920
61014

Code: [Select]

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

int CS=53;
String dataString;
File dataFile;

int i=1;

unsigned long dato;
unsigned long user[100];

void setup(){
  Serial.begin(115200);
  Serial.println("Initiating SD");
  pinMode(CS, OUTPUT);
  if(!SD.begin(CS)){
    Serial.println("SD failed");
    return;
  }
  Serial.println("SD OK");
  leerSD();
}
 
void leerSD(){
  Serial.println("Reading SD...");
  dato=0;
  dataFile=SD.open("user.txt",FILE_READ);
    // read from the file until there's nothing else in it:
    while (dataFile.available()&& dataFile.peek() != '\n'){
      Serial.write(dataFile.read());
      delay(50);
    }
      asignauser();
}

void asignauser(){
        if(i<=4){
        user[i]=dataFile.read();
        Serial.print("The user");
        Serial.print(i);
        Serial.print("is:");
        Serial.println(user[i]);
        i++;
        leerSD();
        }
        loop();
}

void loop(){
}


Many thanks!!

dgelectron

This one is better, but still not works...
Code: [Select]

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

int CS=53;
String dataString;
File dataFile;

int i=1;

unsigned long dato;
unsigned long user[100];

void setup(){
  Serial.begin(115200);
  Serial.println("Initiating SD");
  pinMode(CS, OUTPUT);
  if(!SD.begin(CS)){
    Serial.println("SD failed");
    return;
  }
  Serial.println("SD OK");
  openSD();
}
 
void openSD(){
  Serial.println("Reading SD...");
  dato=0;
  dataFile=SD.open("user.txt",FILE_READ);
  readSD();
}

void readSD(){
    while (dataFile.available() && dataFile.peek() != '\n'){
      Serial.write(dataFile.read());
      delay(50);
    }
    asignauser();
}
   
void asignauser(){
    if(i<=4){
      user[i]=dataFile.read();
      Serial.print("The user ");
      Serial.print(i);
      Serial.print(" is:");
      Serial.println(user[i]);
      i++;
      readSD();
    }
}

The serial returns me this:

Initiating SD
SD OK
Reading SD...
67202 The user 1 is:10
62601 The user 2 is:10
60920 The user 3 is:10
61014 The user 4 is:4294967295[/b]

Obviously it hast to be --> user 1 is:67202...etc

PaulS

Quote
This one is better, but still not works...

Better how? Still not works how?

Recursive calls are not a good idea.

dataFile.read() returns ONE character, not the rest of the record.

Go Up