Hello Everyone, I'm new with Arduino and I need a help with this sketch I'm working...
I'm using a ethernet shield w5100 with SD-Card.
Just for now, I'm working only with SD that I'm writing a text file using the SdFat library.
The point is that I'm able to read the file, find the string and return a "Match" information.
I would like also, after the string I found, write/Replace a value after the "," comma!
Let's supose that I'm lookin for this value: "13110781", on text file will be: "13110781,x" and I would like to write like "13110781,1" or some times "13110781,0".
Follow the code to read and find the value:
// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <SdFat.h>
SdFat sd;
SdFile myFile;
char buf[10]; // Buffer for read the Access file
void setup() {
Serial.begin(9600);
// disable w5100 while setting up SD
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
// open for read and write
if (!myFile.open("acesso.txt", O_RDWR))
{
sd.errorHalt("opening acesso.txt for read failed");
}
// read from the file until there's nothing else in it:
int data;
while ((data = myFile.read(buf,12)) > 0)
{
if(strncmp(buf, "13110781", 8) == 0) //Compare the "buf"(SD String) with "result" String to check out if Match!
{
Serial.println("Match!");
// I need to replace the end of "13110781" after the "," with Zero (0) or One (1)
// I tried the commands bellow... no success...
// myFile.print(buf[9,10],'0');
// myFile.write(buf[9,10]'0');
}
}
myFile.close();
}
void loop()
{
// nothing happens after setup
}
Also follow the text file format:
07836883,x
13108266,x
13033706,x
13056789,x
13035732,x
13110781,x
07901021,x
18430841,x
18427551,x
16029778,x
Thanks in Advance!