I'm quite a newbie to this and I've been struggling for sometime now with my school project, trying to find a way to send accelerometer data from my MMA7361 in data packets, I understand that I have to send the data in packets, but I'm having problems going about it. One of the requirements of the project is that I have to send the data in array-packets of all the individual axes (e.g. a buffer array to hold the x-axis value, another for the y-axis value and also for the z-axis value). I have read quite a lot of documentation on the internet about this but putting it together is a challenge. Below is the infamous MMA7361 code sketch, do please help out if you can thankyou.
#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int x;
int y;
int z;
void setup(){
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V
accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
accelero.calibrate();
}
void loop(){
x = accelero.getXRaw();
y = accelero.getYRaw();
z = accelero.getZRaw();
Serial.print("\nx: ");
Serial.print(x);
Serial.print("\ty: ");
Serial.print(y);
Serial.print("\tz: ");
Serial.print(z);
delay(500); //(make it readable)
}
-
How large do you want the buffer to be? For instance, how many x values do you want to read from the sensor before you send the data packet?Chris– Chris2015年10月28日 03:38:18 +00:00Commented Oct 28, 2015 at 3:38
-
thanks @Chris for the response, the buffer is supposed to hold 1000 values at a timedada– dada2015年10月28日 03:39:14 +00:00Commented Oct 28, 2015 at 3:39
1 Answer 1
It's pretty simple to store the x, y and z co-ordinates in three separate arrays. Instead of storing them in single int
variables you can just store them in int
arrays. You can make three arrays that are of size 1000 a store the raw values there. In order to iterate through the arrays within loop()
you can set up i
at global scope and include an if statement that will know when the arrays are filled, for example if (i == 1000)
and within this if statement you can set up a for
loop that will iterate through the arrays and print their values and after reset i
to 0
and then reset the buffer arrays i.e. fill them with zeroes. This last step is not needed since they will be overwritten anyway but it is good to have so you don't accidentally use old values.
Also the for
loop that will print the values will take 8 minutes with that delay(500);
You can make this delay smaller or print the value before each if
statement as the data comes in.
#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int xAxisBuffer [1000] = {0};
int yAxisBuffer [1000] = {0};
int zAxisBuffer [1000] = {0};
int i = 0;
void setup(){
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V
accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
accelero.calibrate();
}
void loop(){
// This stores the x, y and z values in their
// respective arrays
xAxisBuffer[i] = accelero.getXRaw();
yAxisBuffer[i] = accelero.getYRaw();
zAxisBuffer[i] = accelero.getZRaw();
++i; // moves to the next position
if (i == 1000){ // the arrays are full
// prints the (x, y, z) values
// because the delay is .5 seconds it will
// take about 8 minutes to print all 1000
for (int j = 0; j < 1000; j++){
Serial.print("\nx: ");
Serial.print(xAxisBuffer[j]);
Serial.print("\ty: ");
Serial.print(yAxisBuffer[j]);
Serial.print("\tz: ");
Serial.print(zAxisBuffer[j]);
delay(500); //(make it readable)
}
// restart at beginning of the arrays
i = 0;
// reset the arrays
memset(xAxisBuffer,0,sizeof(xAxisBuffer));
memset(yAxisBuffer,0,sizeof(yAxisBuffer));
memset(zAxisBuffer,0,sizeof(zAxisBuffer));
}
}
-
LORD GOD!!!......Thanks @Chris ...you are a life saver...I've been stuck in this loop for a month now. Thank you very much...dada– dada2015年10月28日 04:01:49 +00:00Commented Oct 28, 2015 at 4:01
-
No problem, if you're stuck like that again ask the question here. If this answered your question choose it for best answer (It's the check mark under the score for my answer).Chris– Chris2015年10月28日 04:04:57 +00:00Commented Oct 28, 2015 at 4:04
-
Thanks, remember to pick the best answer on any future questions you have. It helps the community find good answers quickly.Chris– Chris2015年10月28日 04:07:15 +00:00Commented Oct 28, 2015 at 4:07
-
Explore related questions
See similar questions with these tags.