graphing accelerometer results (solved)

Hi

I am trying to get results from my ADXL 345 accelerometer to display graphically by using the Megunolink software however i dont understand the code it wants me to use after looking through the tutorials. I can get the results to display on the serial monitor just not on the plotter feature.

thanks for any help.

here is the code i am trying to use
#include <SPI.h>
#include <GraphSeries.h>

GraphSeries g_aGraphs[]= {"x", "y", "z"};

int CS=10;

char POWER_CTL = 0x2D;
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;
char DATAX1 = 0x33;
char DATAY0 = 0x34;
char DATAY1 = 0x35;
char DATAZ0 = 0x36;
char DATAZ1 = 0x37;

char values[10];
int x,y,z;

void setup(){

SPI.begin();

SPI.setDataMode(SPI_MODE3);

Serial.begin(9600);

pinMode(CS, OUTPUT);

digitalWrite(CS, HIGH);

writeRegister(DATA_FORMAT, 0x01D);

writeRegister(POWER_CTL, 0x08); //Measurement mode
}

void loop(){
/
readRegister(DATAX0, 6, values);

x = ((int)values[1]<<8)|(int)values[0];

y = ((int)values[3]<<8)|(int)values[2];

z = ((int)values[5]<<8)|(int)values[4];

Serial.print(x, DEC);
Serial.print(',');
Serial.print(y, DEC);
Serial.print(',');
Serial.println(z, DEC);
g_aGraphs[].SendData("x","y","z");
delay(10);

}

void writeRegister(char registerAddress, char value){
digitalWrite(CS, LOW);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}

void readRegister(char registerAddress, int numBytes, char * values){
char address = 0x80 | registerAddress;
if(numBytes > 1)address = address | 0x40;

digitalWrite(CS, LOW);
SPI.transfer(address);
for(int i=0; i<numBytes; i++){
values = SPI.transfer(0x00);

  • }*
  • digitalWrite(CS, HIGH);*
    }

How many times does the smiley face shift the bits?

There was a sticky at the top of the forum that you apparently failed to read.

Those emoticons certainly make your code look pretty, don't they ?
Can I suggest that you put code tags around your code to prevent it being mangled when it is displayed ?

Sorry guys this should be better.

#include <SPI.h>
#include <GraphSeries.h>

GraphSeries g_aGraphs[]= {"x", "y", "z"};

int CS=10;

char POWER_CTL = 0x2D;
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;
char DATAX1 = 0x33;
char DATAY0 = 0x34;
char DATAY1 = 0x35;
char DATAZ0 = 0x36;
char DATAZ1 = 0x37;

char values[10];
int x,y,z;

void setup(){

SPI.begin();

SPI.setDataMode(SPI_MODE3);

Serial.begin(9600);

pinMode(CS, OUTPUT);

digitalWrite(CS, HIGH);

writeRegister(DATA_FORMAT, 0x01D);

writeRegister(POWER_CTL, 0x08); //Measurement mode
}

void loop(){
/
readRegister(DATAX0, 6, values);

x = ((int)values[1]<<|(int)values[0];

y = ((int)values[3]<<|(int)values[2];

z = ((int)values[5]<<|(int)values[4];

Serial.print(x, DEC);
Serial.print(',');
Serial.print(y, DEC);
Serial.print(',');
Serial.println(z, DEC);
g_aGraphs[].SendData("x","y","z");
delay(10);

}

void writeRegister(char registerAddress, char value){
digitalWrite(CS, LOW);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}

void readRegister(char registerAddress, int numBytes, char * values){
char address = 0x80 | registerAddress;
if(numBytes > 1)address = address | 0x40;

digitalWrite(CS, LOW);
SPI.transfer(address);
for(int i=0; i<numBytes; i++){
values = SPI.transfer(0x00);
}
digitalWrite(CS, HIGH);
}

Sorry guys this should be better.

Oh, sure lots better.

  x = ((int)values[1]<<|(int)values[0];
  y = ((int)values[3]<<|(int)values[2];
  z = ((int)values[5]<<|(int)values[4];

We didn't need those steenkink shifts, anyway.

Now, go read the sticky.

Ok I have read the sticky, i will try again.

I am trying to use an Accelerometer ( ADXL 345) to try and measure impacts and to get the results to display graphically. I have tried using the Megunolink software which has a tool to plot graphs. According to MegunoLink the code should be in the format (SeriesName,T,Datavalue) with T being the computer time(this format did not work). After looking at the examples in the Megunolink tutorial http://www.blueleafsoftware.com/Resources/EmbeddedSand/Plotting_Arduino_Data I think you are meant to declare the graph by using Graph series and then send the data by using Send Data. However after trial and error it does not seem to work.

Here is the error code I am currently getting

Dave_sketch_1.ino: In function 'void loop()':
Dave_sketch_1:66: error: expected primary-expression before ']' token
Dave_sketch_1:66: error: expected `;' before '{' token

I amusing this could be because I have put it in the wrong part of my code, but I have tried moving it around to over parts. However I am unsure if this is even the right code to be using.

Ok I have read the sticky, i will try again.

By not posting code this time? Sorry, we can't help you, then.

sorry same code as before

#include <SPI.h>
#include <GraphSeries.h>

GraphSeries g_aGraphs[]= {"x", "y", "z"};

int CS=10;

char POWER_CTL = 0x2D;	
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32;	
char DATAX1 = 0x33;	
char DATAY0 = 0x34;	
char DATAY1 = 0x35;	
char DATAZ0 = 0x36;	
char DATAZ1 = 0x37;	

char values[10];
int x,y,z;

void setup(){ 
  
  SPI.begin();
  
  SPI.setDataMode(SPI_MODE3);
  
  Serial.begin(9600);
  
  pinMode(CS, OUTPUT);
 
  digitalWrite(CS, HIGH);
  
  writeRegister(DATA_FORMAT, 0x01D);
  
  writeRegister(POWER_CTL, 0x08);  //Measurement mode  
}

void loop(){
  /
  readRegister(DATAX0, 6, values);

  x = ((int)values[1]<<8)|(int)values[0];
  
  y = ((int)values[3]<<8)|(int)values[2];
  
  z = ((int)values[5]<<8)|(int)values[4];
  
  Serial.print(x, DEC);
  Serial.print(',');
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.println(z, DEC);   
  g_aGraphs[].SendData("x","y","z"); 
  delay(10);
  
}

  
void writeRegister(char registerAddress, char value){
  digitalWrite(CS, LOW);
  SPI.transfer(registerAddress);
  SPI.transfer(value);
  digitalWrite(CS, HIGH);
}

void readRegister(char registerAddress, int numBytes, char * values){
  char address = 0x80 | registerAddress;
  if(numBytes > 1)address = address | 0x40;
  
  
  digitalWrite(CS, LOW);
  SPI.transfer(address);
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00);
  }
  digitalWrite(CS, HIGH);
}
[code]

[/code]

Post your current code.
Now, looking at your previously posted code, I noticed this.

g_aGraphs[].SendData("x","y","z");

This is not sending the "data" in x,y, or z, but rather the actual characters "x", "y", "z". You want to use single quotes. 'x', 'y','z', to send your data.

Same here "GraphSeries g_aGraphs[]= {"x", "y", "z"};"

Added: Your code does not like this straggler,

void loop(){
/

void loop(){
  /
  readRegister(DATAX0, 6, values);

What is the slash there for?

  g_aGraphs[].SendData("x","y","z");

You have created an array of GraphSeries objects. Which one are you trying to send data to? Why are you trying to graph letters? Don't you think that you will have better results graphing numbers?

  g_aGraphs[0].SendData(x);
  g_aGraphs[1].SendData(y);
  g_aGraphs[2].SendData(z);

Thanks for the help guys. After a few more tweaks to the rest of the code it works very well.