Hello!
In my project I am using the microSD card on the arduino ethernet shield with a Mega. What I want to do is transfer the data on the SD card via I2C to another mega (the master). I can write to and read from the SD card using the SD library, and I can send small chunks of analog data (as ints) over I2C, but I can't figure out how to send the data on the SD card over I2C. I know that this should be a fairly easy combination of my other two sketches, but I can't get it to work. I have little experience programming (none using C++), and I suspect the problem is coming from using data types improperly or a discrepancy in the speeds at which the arduino can read the SD card and write to the I2C bus. The data on the SD card is stored in the following format:
timestamp: 543; Voltage read: 2.61
timestamp: 1055; Voltage read: 1.89
timestamp: 1566; Voltage read: 0.19
...
The timestamp will actually be coming from a real time clock, but I thought I should solve this problem first before I add anything else. My first attempt was this code:
Slave:
#include <SD.h>
#include <Wire.h>
File file;
void setup()
{
Serial.begin(57600);
pinMode (53,OUTPUT);
if (!SD.begin(4)){
Serial.println("SD not initialized");
}
else {
Serial.println("SD initialized");
}
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop()
{
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
file = SD.open("logdata.txt");
if(file) {
Serial.println("File opened");
while (file.available()>0) {
Wire.write(file.read());
}
Serial.println("Data Sent");
}
}
Master:
#include <SPI.h>
#include <Wire.h>
void setup ()
{
//Open Serial communication
Serial.begin(57600);
//Open Wire Communication on I2C and assign the slave an address
Wire.begin(1);
}
void loop ()
{
//Send a request for info to device 2
Wire.requestFrom(2,10);
Serial.println("Data Requested");
//Receive data while it's available
while(Wire.available()) {
//Receive firt byte as c
char c = Wire.read();
//Print c
Serial.print(c);
//move to next byte
}
delay (500); //wait half a second before sending next request
}
I got "Data Requested ÿÿÿÿÿÿÿÿ" on the master Serial port.
I think the error is in the slave in the "Wire.write(file.read());" line. That was just wishful thinking on my part. So next I tried populating a string with characters as the SD card was read and sending that over I2C, but got the same result. (I can print the string to the slave serial port and get the data off of the card).
Sorry for the long post.
Thank you for your help