I can't figure out how to read a load of bytes into a byte array. For instance the code below outputs "50" when I would like it to have filled the byte array with B10000000 (128). I had it working with single bytes using SD but it took 39 milliseconds to read 1024 bytes and I wanted to reduce that time.
#include <SPI.h>
#include <SdFat.h>
SdFat sd;
SdFile myFile;
const int chipSelect = 9;
byte data[512];
void setup()
{
Serial.begin(115200);
pinMode(chipSelect, OUTPUT);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
sd.initErrorHalt();
}
WriteData();
ReadData();
Serial.println(data[400]);
}
void loop()
{
}
void ReadData()
{
if (!myFile.open("test.txt")) {
sd.errorHalt("opening test.txt for read failed");
}
myFile.read(&data,512);
myFile.close();
}
void WriteData()
{
if(sd.exists("test.txt")) {
sd.remove("test.txt");
}
if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("ofailed");
}
for(int i=0;i<1024;i++) {
myFile.print(B10000000);
}
myFile.close();
}
Any suggestions would be most welcome. Cheers!