Home › Forums › EasyTransfer Library Support Forum › Easy Transfer Array Populating (from SD?)
Tagged: array, Easy Transfer, SD, Teensy
- This topic has 8 replies, 2 voices, and was last updated 8 years, 5 months ago by Anonymous.
-
AuthorPosts
-
April 13, 2016 at 11:57 pm #3522AnonymousInactive
Hi Bill and gang,
First let me say that I love the library. I’m relatively new to the game, and so you’ll excuse me if my questions are rudimentary or I’m missing something obvious. I have a few questions about how to utilize your Easy Transfer library…
I’m trying to create an array on one Arduino, and then transfer the array to a secondary Arduino (both Teensy 3.2 units). I have found that I can populate the array manually:
mydata.arraydata[0] = {0x000000};
mydata.arraydata[1] = {0x0000ff};
mydata.arraydata[2] = {0x00ff00};
mydata.arraydata[3] = {0x00ffff};
mydata.arraydata[4] = {0xff0000};
but can’t populate it via a single like:
mydata.arraydata[] = {0x000000,0x0000ff,0x00ff00,0x00ffff,0xff0000};
Am I missing something, or simply doing this incorrectly? This is the first part of my problem. I have an SD card reader attached to the TX teensy, which I would like to read a comma delineated txt file from, and import that data into the arraydata[] array. This way I can load a new file, and automatically populate the array, have easy transfer send to the RX teensy unit, and work with the array there.
Any guidance you can provide would be greatly appreciated.
April 14, 2016 at 7:30 am #3523Michael P. FlagaMemberI would recommend looking at the second answer at the following StackExchanage
http://stackoverflow.com/questions/25314702/data-in-sd-card-convert-it-to-array-arduino
also
http://forum.arduino.cc/index.php?topic=60939.0
April 14, 2016 at 7:33 am #3524Michael P. FlagaMemberAs for your array issue I suspect something un-related to this library.
the following snippet works:
#define LENGTH_OF_ARRAY(x) ((sizeof(x)/sizeof(x[0])))
int arraydata[] = {0x000000,0x0000ff,0x00ff00,0x00ffff,0xff0000};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println(“Hit any key to continue.”);
Serial.flush(); //flush all previous received and transmitted data
while(!Serial.available()) ;
for (uint8_t channel = 0; channel < LENGTH_OF_ARRAY(arraydata); channel++) {
Serial.print(“arraydata[“);
Serial.print(channel);
Serial.print(“]=”);
Serial.println(arraydata[channel]);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
with the following results:
Hit any key to continue.
arraydata[0]=0
arraydata[1]=255
arraydata[2]=-256
arraydata[3]=-1
arraydata[4]=0
April 14, 2016 at 1:25 pm #3525AnonymousInactiveFirst, thank you for taking the time to respond. I’ve looked at those links previously but will work with them again to read my array in from the SD card. To deal with the second issue, from what I can see in your code snippet, you are assigning the int the bulk values as I would expect, and it works properly locally. However, using Easy Transfer, the array name switches from being arraydata[] to mydata.arraydata[], as per the Send_Data_Structure line declaration. When trying to print my arraydata it works, but mydata.arraydata prints a different value. Here is my code. FYI, I’m running the latest Easy Transfer with Arduino 1.6.5 (for compatibility reasons with Teensy and SDFat).
#include <EasyTransfer.h>
#define LENGTH_OF_ARRAY(x) ((sizeof(x)/sizeof(x[0])))
//create object
EasyTransfer ET;
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[];
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup(){
Serial1.begin(57600);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Serial1);}
void loop(){
//this is how you access the variables. [name of the group].[variable name]
int arraydata[] = {0x000001,0x1000ff,0x10ff00,0x10ffff,0xff0001};
//send the data
ET.sendData();
// Serial.println(mydata.arraydata[2], HEX);
for (uint8_t channel = 0; channel < LENGTH_OF_ARRAY(arraydata); channel++) {
Serial.print(“arraydata[“);
Serial.print(channel);
Serial.print(“]=”);
Serial.println(arraydata[channel], HEX);
Serial.print(“mydata.arraydata[“);
Serial.print(channel);
Serial.print(“]=”);
Serial.println(mydata.arraydata[channel], HEX);
delay(1000);
}
}
April 14, 2016 at 5:33 pm #3526Michael P. FlagaMemberplease note that arraydata[] and mydata.arraydata[] are two different entities.
The line of code in the main loop:
<p class=”MsoPlainText”>int arraydata[] = {0x000001,0x1000ff,0x10ff00,0x10ffff,0xff0001};</p>
<p class=”MsoPlainText”>creates a new instance of memory, unique from mydata. The “int” at the beginning is the initializer.</p>
<p class=”MsoPlainText”>The simplest fix is to copy one array to another. See the below example where memcpy() does this.</p>
<p class=”MsoPlainText”>void loop() {</p>
<p class=”MsoPlainText”> //this is how you access the variables. [name of the group].[variable name]</p>
<p class=”MsoPlainText”> int arraydata[] = {0x31,0x100032,0x33,0x34,0x35};</p>
<p class=”MsoPlainText”> memcpy(mydata.arraydata, arraydata, sizeof(arraydata));</p>
<p class=”MsoPlainText”> //send the data</p>
<p class=”MsoPlainText”>Alternatively you can use pointers. as not use twice the memory.</p>April 14, 2016 at 6:04 pm #3527AnonymousInactiveThe addition of the memcpy line, once uploaded to my Teensy3.2, causes the unit to no longer be recognized as a USB Device. Reverting to the previous code and reflashing fixes the issue. Ultimately, I don’t need two copies of the array data, only 1. However, I tried editing this section:
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[];
to read
<span style=”line-height: 1.5;”> </span><span style=”line-height: 1.5;”>int mydata.arraydata[];</span>
and changed all references to arraydata to mydata.arraydata, however it fails to compile.
Ultimately, my goal is this: Teensy unit 1 (server), will read a .txt file which will contain the array data, and will send that array data to Teensy unit 2 (client), which will then use the array to light up some LEDs. At pre-specified times, the server Teensy will load a new .txt file and overwrite the array data, changing the display on the client Teensy. Am I approaching this wrong? Thank you! Your help responsiveness is greatly appreciated.
April 14, 2016 at 7:40 pm #3528Michael P. FlagaMemberSounds like a good plan.
I believe the lock up is from
int arraydata[];
in the struct. This does not specify a length. Hence it was simply a pointer to a single INT not really an array and the memcopy over wrote from that point, locking the code up, when used by the ET.sendData().
I was able to get https://gist.github.com/mpflaga/c1f957f4f5146b990c28daa8d82fb16e to not lock up, printing out the results to monitor. I did not test the ET feature to a RX. But assume it works, as it does not lock up.
The inherent danger of using MemCopy is that it can over/under write things. Hence pointers are safer, but a bit tricky to understand and follow.
the SdCard example I see had several typo’s. So not sure if it even works. But I have compiled it into a solution similar to your specification at https://gist.github.com/mpflaga/1a9d0ad89258f069d5c0c2d6a6b10e5e this posted GIST does compile. I did not actually try it with a SdCard, (not one handy), but it seems like it should work.
Assuming this works, be mindful of the Scope of an entity and its true length.
happy Blinking.
April 14, 2016 at 7:43 pm #3529Michael P. FlagaMemberFollowup. I am not sure about how you plan on updating the SdCard. Seams like you would have to pull it out update it with different values and put it back in. That may cause problems with the SdCard library.
In an attempt to deal with that the SD.open() is performed each time and closed, each time. So it may be able to be removed. I have not tried that. However, the Arduino SD library is not a really Hot Plug. So go at your own risk. and may need to improve on my Try/Excepts.
April 21, 2016 at 1:47 am #3533AnonymousInactiveWell, I’ve made some progress, but am still having difficulties. I can get both units talking to one another, and the data on the sending side is properly being populated into the mydata.arraydata array. However, when the data is being sent over to the receiving unit, it only populates XX amount of fields, then the rest of the array returns 0 values. The unusual part is that if I have 250 values, it transfers the first 59 values and the rest are 0. If I set it for 230 I get 43 values properly sent, etc etc. There’s no rhyme or reason for the amount that properly send, that I can figure out. Any ideas?
Here’s the TX code:
#include <EasyTransfer.h>
#define LENGTH_OF_ARRAY(x)((sizeof(x) / sizeof(x[0])))
File file;
//create object
EasyTransfer ET;
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[13000];
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
void setup() {
Serial.begin(57600);
Serial1.begin(57600);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Serial1);
while (!Serial) {} // wait for Leonardo
Serial.println(“Type any character to start”);
while (Serial.read() <= 0) {}
delay(400); // catch Due reset problem
}
void loop() {
int arraydata[250] = { 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0x00fffe, 0x000306, 0xffffff, 0x00fffe, 0xffffff};
memcpy(mydata.arraydata, arraydata, sizeof(arraydata));
//send the data
ET.sendData();
//print array data to serial monitor
for (uint16_t channel = 0; channel < LENGTH_OF_ARRAY(mydata.arraydata); channel++) {
Serial.print(“mydata.arraydata[“);
Serial.print(channel);
Serial.print(“]=”);
Serial.println(mydata.arraydata[channel], HEX);
delay(15);
}
Serial.print(“Iteration: “);
Serial.println(iter);
iter++;
}
—
The RX code:
#include <EasyTransfer.h>
#define LENGTH_OF_ARRAY(x)((sizeof(x) / sizeof(x[0])))
//create object
EasyTransfer ET;
struct RECEIVE_DATA_STRUCTURE {
//put your variable definitions here for the data you want to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int arraydata[250];
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
void setup() {
Serial1.begin(57600);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Serial1);
}
void loop() {
//check and see if a data packet has come in.
if (ET.receiveData()) {
for (uint16_t channel = 0; channel < LENGTH_OF_ARRAY(mydata.arraydata); channel++) {
Serial.print(“mydata.arraydata[“);
Serial.print(channel);
Serial.print(“]=”);
Serial.println(mydata.arraydata[channel], HEX);
delay(10);
}
}
//you should make this delay shorter then your transmit delay or else messages could be lost
// delay(250);
}
—–
Thanks for taking a look.
-
AuthorPosts
- You must be logged in to reply to this topic.