Good afternoon,
I have this portion of code. My attempt is to send the buffer myFile.read coming from an SD card to another Arduino via I2C protocol.
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
File myFile;
String buffer;
char dati[10];
void setup()
{
Serial.begin(9600);
//while (!Serial){;} // wait for serial port to connect. Needed for native USB port only
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
Serial.print("Initializing SD card...");
if (!SD.begin(10))
{
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void loop()
{
delay(100);
}
void requestEvent()
{
myFile = SD.open("test.txt");
if (myFile)
{
Serial.println("test.txt:");
Serial.println(myFile.size());
while (myFile.available())
{
buffer = myFile.read();
buffer.toCharArray(dati, 60);
}
myFile.close();
Wire.write(dati, sizeof(dati));
}
else
{
Serial.println("error opening test.txt");
}
}
This is the Arduino slave's code.
I have two issues:
while (myFile.available())
This while is freezing my program.
If I delete it, the program starts to work fine.
I need this part for another purpose, so I need to leave it.
Can you help me finding the mistake?
- I am not sure that this is the correct way to send a buffer via I2C, because if I read the single lines from an SD I can do it without any issue but it doean't works with the whole file (all lines packaged in the same buffer).
buffer = myFile.read();
buffer.toCharArray(dati, 60);
}
myFile.close();
Wire.write(dati, sizeof(dati));
Many thanks.