Skip to content

Instantly share code, notes, and snippets.

@mpflaga
Last active April 15, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpflaga/1a9d0ad89258f069d5c0c2d6a6b10e5e to your computer and use it in GitHub Desktop.
Save mpflaga/1a9d0ad89258f069d5c0c2d6a6b10e5e to your computer and use it in GitHub Desktop.
Read a SdCard File and send contents over EasyTransfer library, support hot-swap of SdCard
#include <SPI.h>
#include <SD.h>
#include <EasyTransfer.h>
#define LENGTH_OF_ARRAY(x)((sizeof(x) / sizeof(x[0])))
//create object
EasyTransfer ET;
//Timer
const long intervalFileRead = 5 * 1000; // seconds
unsigned long previousMillisFileRead = 0;
const long intervalSendET = 1 * 1000; // seconds
unsigned long previousMillisSendET = 0;
struct SEND_DATA_STRUCTURE {
//put your variable definitions here for the data you want to send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int arraydata[5];
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup() {
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// disable my VS1053 chip on same sheild as SdCard socket.
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
if (!SD.begin(9)) {
Serial.println("initialization failed!");
return;
}
Serial.println("Begin...");
#if defined(__AVR_ATmega32U4__) // I am using a UNO that does not have Serial1, for initial testing.
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
Serial1.begin(57600);
ET.begin(details(mydata), &Serial1);
#endif
//Initialize Data with default values
int arraydata[] = {0x31, 0x32, 0x33, 0x34, 0x35};
memcpy(mydata.arraydata, arraydata, sizeof(arraydata));
// above not needed due to below, but good demo.
Serial.println("initialized values with out SdCard.");
for (uint8_t channel = 0; channel < LENGTH_OF_ARRAY(mydata.arraydata); channel++) {
Serial.print("arraydata[");
Serial.print(channel);
Serial.print("]=");
Serial.println(arraydata[channel], HEX);
}
// Initial values as to immediately send in main loop.
previousMillisFileRead = millis() - intervalFileRead;
previousMillisSendET = millis() - intervalSendET;
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillisFileRead >= intervalFileRead) {
previousMillisFileRead = currentMillis;
// Open and read file
File dataFile = SD.open("file.txt");
if (!dataFile) {
if (SD.begin(9)) {
Serial.println("re-initialization of SdCard");
dataFile = SD.open("file.txt");
}
}
if (dataFile) {
Serial.println("SdCard Found, updating...");
for (int channel = 0; channel <= LENGTH_OF_ARRAY(mydata.arraydata) - 1; channel++) {
mydata.arraydata[channel] = dataFile.parseInt();
Serial.print(" SdCard data[");
Serial.print(channel);
Serial.print("]=");
Serial.print(mydata.arraydata[channel], DEC);
}
Serial.println();
dataFile.close();
}
else
{
Serial.println("No SdCard Found!");
}
}
if (currentMillis - previousMillisSendET >= intervalSendET) {
previousMillisSendET = currentMillis;
//send the data
// ET.sendData();
// Serial.println(mydata.arraydata[2], HEX);
for (uint8_t channel = 0; channel < LENGTH_OF_ARRAY(mydata.arraydata); channel++) {
Serial.print(" mydata.arraydata[");
Serial.print(channel);
Serial.print("]=");
Serial.print(mydata.arraydata[channel], DEC);
}
Serial.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment