Graphing acceleration data & saving as .txt

I am new to Arduino and C/C++ program. I am trying to develop an application that uses a LilyPad Accelerometer (Sparkfun) to measure the vibrations recorded during muscle contraction (i.e., mechanomyography). I have my Arduino up and running, thanks to this forum! My next step is to get the data to the PC and simultaneously save it as a .txt file and display a graph. I also want to be able to start and stop data collection from the PC.

I looked at the firmware for a Sparkfun WiTilt, I have used for other applications. I am sure what I need is there, but I am too much of a newbee to tease out what I don't need.

Can anyone give me some direction. I assume that I will need to load a processing software? Here is the code I have written thus far:

void setup()
{
Serial.begin(9600);
}

void loop()
{
//read the analog input into a variable:
int xpin = 5;
int ypin = 6;
int zpin = 7;
// print the sensor values:
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
delay(1);
}

I'd also like to be able to include a clock to time the data collection, but first things first. I need help taking baby steps here :slight_smile:

It's pretty easy to set up graphing if you dump your data directly into Excel, assuming you're an Excel user. One way to have your program do that is through PLX-DAQ, which is a set of Excel VB macros available for free download here:

http://www.parallax.com/ProductInfo/Microcontrollers/PLXDAQDataAcquisitiontool/tabid/393/Default.aspx

You just add the command strings to your print() statements and send the data out that way. There's a built-in timer, though I can't vouch for the precision.

GoBetwino can send data directly from Arduino to Excel, or save it to a txt file.

Thanks. I'll have to look at both software options. Any opinions on advantages/disadvantages? Mikmo, I am sure might be a bit biased :slight_smile: but I see his/her posts repeatedly on this forum, so I trust that there is certainly a familiarity with the Arduino platform. Critical for me is to find the understandable and adaptable examples that can grow with my project.

Thanks, again!

I haven't seen Gobetwino, but I imagine that software made explicitly for Arduino will be quite a bit easier to set up. PLX-DAQ is for Basic Stamps. I've used with with Arduino, but it's a bit clumsy.

Sylvie369,

Thanks. I downloaded both today. I plan to try them out this weekend.

How about reporting back on how they are?

Theres example code with the GoBetwino download for putting data into Excel, and even email the xl file to someone when its finished, all without touching the PC.

I have just made it through the manual for GoBetwino. It seems simple enough. I plan to look at the examples next. I didn't note any code for graphing. How is this handled? Does the data go into Excel in real-time such that I can view the graph here?

I checked out PLX-DAQ. It seems more complicated and I don't find clear direction on how to integrate it with my LilyPad Arduino. Through the HyperTerminal on my PC, I presume? If so, this adds a few more steps than GoBetwino. I want to keep it simple--open, start, view & save data, stop. I think GoBetwino will do this sufficiently for my needs.

I was able to upload the following, but I can't seem to get it to start logging data. Using the code from my initial post, I was able to see the data in the Serial Monitor. Now--nothing. I am also not sure how to start and stop the data logging using GoBetwino. I am obviously missing something. Can anyone help? Thanks!

int serInLen = 25;
char serInString[25];
int xpin = 5;
int ypin = 6;
int zpin = 7;
int result;

void setup()
{
Serial.begin(9600);
}
void loop()
{
}
void logData( int xpin, int ypin, int zpin)
{
char buffer[5];
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
delay(1);
Serial.print("#S|LOGTEST|[");
Serial.print(itoa((xpin), buffer, 10));
Serial.print(";");
Serial.print(itoa((ypin), buffer, 10));
Serial.print(";");
Serial.print(itoa((zpin), buffer, 10));
Serial.println("]#");
readSerialString(serInString,1000);
}
void readSerialString (char *strArray,long timeOut)
{
long startTime=millis();
int i;

while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < serInLen) {
strArray = Serial.read();

  • i++;*
  • }*
    }
void setup()
{
 Serial.begin(9600);
}
void loop()
{
}

These two functions are the heart of any Arduino program. The setup function is called once. The loop function is then called in an infinite loop.

In setup, you initialize serial communications. In loop, you do nothing.

That's the result you're seeing.

What's the problem?

Paul,

I am attempting to use GoBetwino (Gadgets og teknologi | Dansk Tech Blog - Mikmo) to log and graph data from an accelerometer and LilyPad Arguino (Sparkfun Electronics). Using the code in my original post, I was able to read the triaxial accelerations in the Serial Module. I modified the code in an effort to log the data to Excel (my previous post) and now I seem to have lost the communication. I want to be able to start and stop the datalogging using GoBetwino.

I am a newbee to C/C++ code. I understand the set up and loop concepts, but I am having trouble figuring out how to get the data from my Arduino to a .txt file I can then analyze.

In loop, you need to call the logData function.

I don't quite know how to make Excel starrt graphing in real time.

You can put data into Excel sheet cells in realtime with GoBetwino, maybe there's an Excel shark out there who can shed some light on the graphing part ?

I am still struggling with putting the code together for this project. I a have pieced together the sample code from GoBetwino, but, in my limited knowledge of C/C++, I can't resolve the error(s) in the following:

//This program was written 2-8-10. The intended purpose is to read a triaxial accelerometer with a LilyPad Arduino and record it in Excel for future analyses. I would also like to control the start and stop of the recording.

int serInLen = 25;
char serInString[25];
int pId = 0;
int result;

void setup()
{
// Set up serial communication. Start Excel.
Serial.begin(9600);
Serial.println("#S|SPXL|[]#"); // start Excel
readSerialString(serInString, 5000); //wait 5 seconds (max) for answer from Gobetwino (=process ID)
pId= atoi(serInString); // convert result to integer
sendPotValues(); // send some data to Excell
}
void loop()
{
//analogValue= random(0,1023);
//logData(analogValue);
//delay(5000);
}
void sendPotValues()
{
char buffer[5];
int xpin=5;
int ypin=6;
int zpin=7;
for (int i=1; i<=15; i++){

//Read the three pot values - acceleration values
potValue1=analogRead(xpin);
potValue2=analogRead(ypin);
potValue3=analogRead(zpin);

// print the sensor values:
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
delay(1);
//Send the values as though it was typed into Excell, using the SENDK command
// This is the total line that is send: #S,SENDK,[pId; xpin {TAB} ypin {TAB} zpin {DOWN} {LEFT} {LEFT}]#
Serial.print("#S|SENDK|[");
Serial.print(itoa((pId), buffer, 10));
Serial.print("&");
Serial.print(itoa((xpin), buffer, 10));
Serial.print(" {TAB} ");
Serial.print(itoa((ypin), buffer, 10));
Serial.print(" {TAB} ");
Serial.print(itoa((zpin), buffer, 10));
Serial.print(" {DOWN} ");
Serial.print(" {LEFT} ");
Serial.print(" {LEFT} ");
Serial.println("]#");
// wait up to 1 ms for answer from Gobetwino, answer will be in serInString, answer is 0 if all is OK
readSerialString(serInString, 1);
//Deal with answer here - omitted in this example
delay(2);
{
// Now we have sent the 3 acceleration values X times, so save and close the XL sheet.
Serial.print("#S|SENDK|[");
Serial.print(itoa((pId), buffer, 10));
Serial.print("& ");
Serial.print("^s^w"); //= CTRL+S CTRL+W = save and exit
Serial.println("]#");
// Wait 2 seconds for the save to finish
delay(2000);
// wait up to 1000 ms for answer from Gobetwino, answer will be in serInString, answer is 0 if all is OK
readSerialString(serInString, 1000);
}
//read a string from the serial and store it in an array
//you must supply the array variable - will return if timeOut ms passes before the sting is read so you should
//check the contents of the char array before making any assumptions.

void readSerialString (char *strArray,long timeOut)

long startTime=millis();
int i;

while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < serInLen) {
strArray = Serial.read();

  • i++;*
  • }*
    }
    ERROR MESSAGE:
    In function 'void setup()':
    Error: 'readSerialString' was not declared in this scope In function 'void sendPotValues()':

This is a problem:

void readSerialString (char *strArray,long timeOut)

  long startTime=millis();
  int i;

  while (!Serial.available()) {
     if (millis()-startTime >= timeOut) {
        return;
     }
  }
  while (Serial.available() && i < serInLen) {
     strArray[i] = Serial.read();
     i++;
  }
}

Count your open and close curly braces...

Mik wrote

"I don't quite know how to make Excel starrt graphing in real time.

You can put data into Excel sheet cells in realtime with GoBetwino, maybe there's an Excel shark out there who can shed some light on the graphing part ? "

PLX-DAQ (from Parallax.com) will do it, fairly easily. You have to add some command strings to your serial output, which are read by Excel macros (which are what you're using when you use PLX-DAQ) to set column labels, put data into cells, read data from cells, and so on. In the macro-enabled spreadsheet you get when you download PLX-DAQ, one of the sheets is set up to automatically graph the data as they come in. To use that sheet, you just drag it to the first position in the file. I have been using it without any trouble in my rocket telemetry project to graph the rocket's altitude in real time.

Paul,

Thanks. I cleaned up the brackets and was able to verify the code.

Sylvia,

Thanks. Sounds like in PLX-DAQ there is no code to write--other than setting up the Arduino to read my three axes? I'll try it out and see how it works.

Mikmo,

I am still having trouble sending the data to Excel. Am I possibly missing something in the GoBetwino setup? Here is the code that was verified--I might be off on some of the delays (I want to sample at 1000Hz), but I can play with this.

int serInLen = 25;
char serInString[25];
int xpin=5;
int ypin=6;
int zpin=7;
int pId = 0;
int result;

void setup()
{
// Set up serial communication. Start Excel.
Serial.begin(9600);
Serial.println("#S|SPXL|[]#"); // start Excel
readSerialString(serInString, 5000); //wait 5 seconds (max) for answer from Gobetwino (=process ID)
pId= atoi(serInString); // convert result to integer
sendPotValues(); // send some data to Excell
}
void loop()
{
//analogValue= random(0,1023);
//logData(analogValue);
//delay(5000);
}

void sendPotValues()
{
char buffer[5];
int potValue1;
int potValue2;
int potValue3;

for (int i=1; i<=15; i++){
//Read the three pot values - acceleration values
potValue1 = analogRead(xpin);
potValue2 = analogRead(ypin);
potValue3 = analogRead(zpin);

//Send the values as though it was typed into Excell, using the SENDK command
// This is the total line that is send: #S,SENDK,[pId; xpin {TAB} ypin {TAB} zpin {DOWN} {LEFT} {LEFT}]#
Serial.print("#S|SENDK|[");
Serial.print(itoa((pId), buffer, 10));
Serial.print("&");
Serial.print(itoa((xpin), buffer, 10));
Serial.print(" {TAB} ");
Serial.print(itoa((ypin), buffer, 10));
Serial.print(" {TAB} ");
Serial.print(itoa((zpin), buffer, 10));
Serial.print(" {DOWN} ");
Serial.print(" {LEFT} ");
Serial.print(" {LEFT} ");
Serial.println("]#");
// wait up to 1 ms for answer from Gobetwino, answer will be in serInString, answer is 0 if all is OK
readSerialString(serInString, 1);
//Deal with answer here - omitted in this example
delay(2);
}
// Now we have sent the 3 acceleration values X times, so save and close the XL sheet.
Serial.print("#S|SENDK|[");
Serial.print(itoa((pId), buffer, 10));
Serial.print("& ");
Serial.print("^s^w"); //= CTRL+S CTRL+W = save and exit
Serial.println("]#");
// Wait 2 seconds for the save to finish
delay(2000);
// wait up to 1000 ms for answer from Gobetwino, answer will be in serInString, answer is 0 if all is OK
readSerialString(serInString, 1000);
}
//read a string from the serial and store it in an array
//you must supply the array variable - will return if timeOut ms passes before the sting is read so you should
//check the contents of the char array before making any assumptions.

void readSerialString (char *strArray,long timeOut)
{
long startTime=millis();
int i;

while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < serInLen) {
strArray = Serial.read();

  • i++;*
  • }*
    }

Could you please explain "trouble" a bit more detailed ?

What is happening / not happening ?

Is something showing up in the text area i n GoBetwino ?

Is GoBetwino sayning the commands are ok / not ok?

Is something put in the cells in Excell ?

I don't know (i doubt) this will work at 1000Hz, that would mean many thousand characters send every second.

While your at it, can you please post your code properly? After pasting in the code, select all of it, and hit the # button on the top line. Or, select the # button first, and then paste the code.