Hello guys,
I'm working in a project which i have to read a entry of the Serial input and search the entry in SD card. The entry is 12 byte length, and it terminate with '' (in SD card, don't have '').
The code works well in the first entries, but when i try read the entries in card above the position 2730, the function "seek" return false.
I tried read the card sequentially, and the arduino can read all bytes of the archive. This mean the problem isn't the archive lenght.
here is my code
#include <SD.h>
// Variáveis para entrada (buffer e contador)
byte bufferIn[15];
byte count_buffer = 0;
// indicador de comando
boolean command = false;
// Controlador de Arquivo
File fp;
int count_busca;
char c;
boolean found = false;
void setup(){
Serial.begin(9600);
if(!SD.begin(4)){ // inicializando o cartão
Serial.println("Error");
return;
}
Serial.println("Memoria Pronta");
}
void loop(){
if(command){ // Espera por comando
command = false; // Para não voltar
fp = SD.open("teste.txt", FILE_READ); // Abre arquivo
for(count_busca = 0; fp.seek(12*count_busca) && !found;count_busca++){
found = compare();
}
Serial.println(count_busca);
fp.close();
if(found){
Serial.print("Entrada encontrada na posicao ");
Serial.println(count_busca);
}
else{
Serial.println("Entrada nao encontrada!!");
}
found = false;
}
}
boolean compare(){ // Compara dado de entrada com o cartão
int i = 0;
for(i = 0; i< 12; i++){
if(bufferIn[i] != fp.read()){ // Se algum byte for diferente, a entrada é diferente
return false;
}
}
return true; // Caso os 12 bytes sejam iguais, Retorna true. Caso algum byte seja diferente,o código entra no "if"
}
void serialEvent(){ // Dado de entrado pronto
bufferIn[count_buffer] = Serial.read(); // Leitura da porta Serial
if(bufferIn[count_buffer] == '*'){ // Final de String(*) e tamanho do dado(12)
if(count_buffer != 12){
Serial.println("Entrada Rejeitada");
count_buffer = 0;
return;
}
Serial.println("Entrada aceita");
count_buffer = 0; // zera buffer
command = true; // Command is ready
return;
}
count_buffer++;
if(count_buffer > 12){
Serial.println("Entrada rejeitada");
count_buffer = 0;
}
}
Sorry by lacks in my english and thanks for replies