The size of a defined array is changing when writing values to it











up vote
0
down vote

favorite












Okay so i'm trying to read data from the gy521 sensor for arduino. I'm at the stage where i am reading data from the FIFO register, but when i try and assign the data to an array so i can calculate position, the array writes 20 values instead of the 10 i defined at the start. Here is my work in progress code:



#include <Wire.h>

#define MPU_6050 0x68
#define PWR_MGMT_1 0x6B
#define FIFO_EN 0x23
#define FIFO_COUNTH 0x72
#define FIFO_COUNTL 0x73
#define FIFO_R_W 0x74
#define SMPLRT_DIV 0x19
#define USER_CTRL 0x6A
#define GY_CONFIG 0x1B
#define SELF_TEST_X 0x0D
#define SELF_TEST_Y 0x0E
#define SELF_TEST_Z 0x0F
#define INT_EN 0x38
#define INT_STAT 0x3A
#define ARRAY_SIZE 10

int FIFO_OVERFLOW_STAT;
int FIFO_DATA_X;
int FIFO_DATA_Y;
int FIFO_DATA_Z;
int x0;
int y0;
int z0;
int FIFO_COUNT;
int x_val[ARRAY_SIZE];
int y_val[ARRAY_SIZE];
int z_val[ARRAY_SIZE];

void setup() {
Serial.begin(115200); //Start a serial interface
Wire.begin(); //set up i2c
Wire.beginTransmission(MPU_6050); //start transmission to slave
Wire.write(PWR_MGMT_1);
Wire.write(0); //Wake up
Wire.endTransmission(true); // Send byte in buffer
Wire.beginTransmission(MPU_6050);
Wire.write(SMPLRT_DIV);
Wire.write(5); //set the sample rate to 250hz (because 8000/1+31=250)
Wire.endTransmission(true);
disable_FIFO();
Wire.beginTransmission(MPU_6050);
Wire.write(USER_CTRL);
Wire.write(68); //enable the FIFO Line and Clear it
Wire.endTransmission(true);
check_FIFO_size();
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_X);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Y);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Z);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(GY_CONFIG);
Wire.write(224); //Self test gyro and set Full Scale range to 250*/s
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(INT_EN);
Wire.write(16); //set up interrupt overflow of FIFO to prevent loss of data
Wire.endTransmission(true);
enable_FIFO();
get_0_vals();
}

void enable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(112); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void disable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(0); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void check_FIFO_overflow_stat(){
Wire.beginTransmission(MPU_6050);
Wire.write(INT_STAT);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 1, true);
if(Wire.available() >= 1){
FIFO_OVERFLOW_STAT = Wire.read() >> 4;
}
Serial.println(FIFO_OVERFLOW_STAT);
}

void check_FIFO_size(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_COUNTH);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 2, true);
if(Wire.available() >=2){
FIFO_COUNT = Wire.read() << 8;
FIFO_COUNT |= Wire.read();
}
Wire.endTransmission();
Serial.print("FIFO count: ");
Serial.println(FIFO_COUNT);
}
void get_0_vals(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >= 6){
x0 = Wire.read() << 8;
x0 |= Wire.read();
y0 = Wire.read() << 8;
y0 |= Wire.read();
z0 = Wire.read() << 8;
z0 |= Wire.read();
}
Wire.endTransmission();
}
void read_FIFO_xyz(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >=6){
FIFO_DATA_X = Wire.read() << 8;
FIFO_DATA_X |= Wire.read();
FIFO_DATA_Y = Wire.read() << 8;
FIFO_DATA_Y |= Wire.read();
FIFO_DATA_Z = Wire.read() << 8;
FIFO_DATA_Z |= Wire.read();
}
Wire.endTransmission(true);
}

void zero_FIFO_data(){
FIFO_DATA_X = FIFO_DATA_X - x0;
FIFO_DATA_Y = FIFO_DATA_Y - y0;
FIFO_DATA_Z = FIFO_DATA_Z - z0;
}

void fill_arrays(){
for(int i = 0; i<10; i++){
should_FIFO_be_on();
if(FIFO_COUNT > 6){
read_FIFO_xyz();
zero_FIFO_data();
x_val[i] = FIFO_DATA_X;
y_val[i] = FIFO_DATA_Y;
z_val[i] = FIFO_DATA_Z;
}
}
}

void should_FIFO_be_on(){
check_FIFO_size();
if(FIFO_COUNT > 900){
disable_FIFO();
}
else{
enable_FIFO();
}
}

void loop() {
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
Serial.println("Start of Main");
check_FIFO_size();
should_FIFO_be_on();
if(FIFO_COUNT > 60){
fill_arrays();
}
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
for(int i = 0; i<10; i++){
Serial.print("Element ");
Serial.print(i);
Serial.print(" of x_val: ");
Serial.println(x_val[i]);
}
}


I have defined x_val as being of size ARRAY_SIZE which is defined as 10.



After calling my fill array function, Serial.print(sizeof(x_val)); returns 20. I have a feeling this is related to the fact that the values of FIFO_DATA_X are 2 Bytes, but that doesnt make sense to me because an int can be 2 or 4 byte.



Sorry in advance, my code is nowhere near perfect, but i can't use the MPU_6050 library so ive had to do my best from the data sheet and register map, plus my understanding of C++ and arduino is pretty poor (as is probably evident).










share|improve this question


















  • 5




    sizeof(x_val) is 20 because it is sizeof(int) * 10. (int is 16-bit on Arduino)
    – Johnny Mopp
    Nov 9 at 15:21












  • This is a long way from a Minimal, Complete, and Verifiable example
    – Tim Randall
    Nov 9 at 16:26















up vote
0
down vote

favorite












Okay so i'm trying to read data from the gy521 sensor for arduino. I'm at the stage where i am reading data from the FIFO register, but when i try and assign the data to an array so i can calculate position, the array writes 20 values instead of the 10 i defined at the start. Here is my work in progress code:



#include <Wire.h>

#define MPU_6050 0x68
#define PWR_MGMT_1 0x6B
#define FIFO_EN 0x23
#define FIFO_COUNTH 0x72
#define FIFO_COUNTL 0x73
#define FIFO_R_W 0x74
#define SMPLRT_DIV 0x19
#define USER_CTRL 0x6A
#define GY_CONFIG 0x1B
#define SELF_TEST_X 0x0D
#define SELF_TEST_Y 0x0E
#define SELF_TEST_Z 0x0F
#define INT_EN 0x38
#define INT_STAT 0x3A
#define ARRAY_SIZE 10

int FIFO_OVERFLOW_STAT;
int FIFO_DATA_X;
int FIFO_DATA_Y;
int FIFO_DATA_Z;
int x0;
int y0;
int z0;
int FIFO_COUNT;
int x_val[ARRAY_SIZE];
int y_val[ARRAY_SIZE];
int z_val[ARRAY_SIZE];

void setup() {
Serial.begin(115200); //Start a serial interface
Wire.begin(); //set up i2c
Wire.beginTransmission(MPU_6050); //start transmission to slave
Wire.write(PWR_MGMT_1);
Wire.write(0); //Wake up
Wire.endTransmission(true); // Send byte in buffer
Wire.beginTransmission(MPU_6050);
Wire.write(SMPLRT_DIV);
Wire.write(5); //set the sample rate to 250hz (because 8000/1+31=250)
Wire.endTransmission(true);
disable_FIFO();
Wire.beginTransmission(MPU_6050);
Wire.write(USER_CTRL);
Wire.write(68); //enable the FIFO Line and Clear it
Wire.endTransmission(true);
check_FIFO_size();
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_X);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Y);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Z);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(GY_CONFIG);
Wire.write(224); //Self test gyro and set Full Scale range to 250*/s
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(INT_EN);
Wire.write(16); //set up interrupt overflow of FIFO to prevent loss of data
Wire.endTransmission(true);
enable_FIFO();
get_0_vals();
}

void enable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(112); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void disable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(0); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void check_FIFO_overflow_stat(){
Wire.beginTransmission(MPU_6050);
Wire.write(INT_STAT);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 1, true);
if(Wire.available() >= 1){
FIFO_OVERFLOW_STAT = Wire.read() >> 4;
}
Serial.println(FIFO_OVERFLOW_STAT);
}

void check_FIFO_size(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_COUNTH);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 2, true);
if(Wire.available() >=2){
FIFO_COUNT = Wire.read() << 8;
FIFO_COUNT |= Wire.read();
}
Wire.endTransmission();
Serial.print("FIFO count: ");
Serial.println(FIFO_COUNT);
}
void get_0_vals(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >= 6){
x0 = Wire.read() << 8;
x0 |= Wire.read();
y0 = Wire.read() << 8;
y0 |= Wire.read();
z0 = Wire.read() << 8;
z0 |= Wire.read();
}
Wire.endTransmission();
}
void read_FIFO_xyz(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >=6){
FIFO_DATA_X = Wire.read() << 8;
FIFO_DATA_X |= Wire.read();
FIFO_DATA_Y = Wire.read() << 8;
FIFO_DATA_Y |= Wire.read();
FIFO_DATA_Z = Wire.read() << 8;
FIFO_DATA_Z |= Wire.read();
}
Wire.endTransmission(true);
}

void zero_FIFO_data(){
FIFO_DATA_X = FIFO_DATA_X - x0;
FIFO_DATA_Y = FIFO_DATA_Y - y0;
FIFO_DATA_Z = FIFO_DATA_Z - z0;
}

void fill_arrays(){
for(int i = 0; i<10; i++){
should_FIFO_be_on();
if(FIFO_COUNT > 6){
read_FIFO_xyz();
zero_FIFO_data();
x_val[i] = FIFO_DATA_X;
y_val[i] = FIFO_DATA_Y;
z_val[i] = FIFO_DATA_Z;
}
}
}

void should_FIFO_be_on(){
check_FIFO_size();
if(FIFO_COUNT > 900){
disable_FIFO();
}
else{
enable_FIFO();
}
}

void loop() {
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
Serial.println("Start of Main");
check_FIFO_size();
should_FIFO_be_on();
if(FIFO_COUNT > 60){
fill_arrays();
}
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
for(int i = 0; i<10; i++){
Serial.print("Element ");
Serial.print(i);
Serial.print(" of x_val: ");
Serial.println(x_val[i]);
}
}


I have defined x_val as being of size ARRAY_SIZE which is defined as 10.



After calling my fill array function, Serial.print(sizeof(x_val)); returns 20. I have a feeling this is related to the fact that the values of FIFO_DATA_X are 2 Bytes, but that doesnt make sense to me because an int can be 2 or 4 byte.



Sorry in advance, my code is nowhere near perfect, but i can't use the MPU_6050 library so ive had to do my best from the data sheet and register map, plus my understanding of C++ and arduino is pretty poor (as is probably evident).










share|improve this question


















  • 5




    sizeof(x_val) is 20 because it is sizeof(int) * 10. (int is 16-bit on Arduino)
    – Johnny Mopp
    Nov 9 at 15:21












  • This is a long way from a Minimal, Complete, and Verifiable example
    – Tim Randall
    Nov 9 at 16:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Okay so i'm trying to read data from the gy521 sensor for arduino. I'm at the stage where i am reading data from the FIFO register, but when i try and assign the data to an array so i can calculate position, the array writes 20 values instead of the 10 i defined at the start. Here is my work in progress code:



#include <Wire.h>

#define MPU_6050 0x68
#define PWR_MGMT_1 0x6B
#define FIFO_EN 0x23
#define FIFO_COUNTH 0x72
#define FIFO_COUNTL 0x73
#define FIFO_R_W 0x74
#define SMPLRT_DIV 0x19
#define USER_CTRL 0x6A
#define GY_CONFIG 0x1B
#define SELF_TEST_X 0x0D
#define SELF_TEST_Y 0x0E
#define SELF_TEST_Z 0x0F
#define INT_EN 0x38
#define INT_STAT 0x3A
#define ARRAY_SIZE 10

int FIFO_OVERFLOW_STAT;
int FIFO_DATA_X;
int FIFO_DATA_Y;
int FIFO_DATA_Z;
int x0;
int y0;
int z0;
int FIFO_COUNT;
int x_val[ARRAY_SIZE];
int y_val[ARRAY_SIZE];
int z_val[ARRAY_SIZE];

void setup() {
Serial.begin(115200); //Start a serial interface
Wire.begin(); //set up i2c
Wire.beginTransmission(MPU_6050); //start transmission to slave
Wire.write(PWR_MGMT_1);
Wire.write(0); //Wake up
Wire.endTransmission(true); // Send byte in buffer
Wire.beginTransmission(MPU_6050);
Wire.write(SMPLRT_DIV);
Wire.write(5); //set the sample rate to 250hz (because 8000/1+31=250)
Wire.endTransmission(true);
disable_FIFO();
Wire.beginTransmission(MPU_6050);
Wire.write(USER_CTRL);
Wire.write(68); //enable the FIFO Line and Clear it
Wire.endTransmission(true);
check_FIFO_size();
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_X);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Y);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Z);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(GY_CONFIG);
Wire.write(224); //Self test gyro and set Full Scale range to 250*/s
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(INT_EN);
Wire.write(16); //set up interrupt overflow of FIFO to prevent loss of data
Wire.endTransmission(true);
enable_FIFO();
get_0_vals();
}

void enable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(112); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void disable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(0); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void check_FIFO_overflow_stat(){
Wire.beginTransmission(MPU_6050);
Wire.write(INT_STAT);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 1, true);
if(Wire.available() >= 1){
FIFO_OVERFLOW_STAT = Wire.read() >> 4;
}
Serial.println(FIFO_OVERFLOW_STAT);
}

void check_FIFO_size(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_COUNTH);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 2, true);
if(Wire.available() >=2){
FIFO_COUNT = Wire.read() << 8;
FIFO_COUNT |= Wire.read();
}
Wire.endTransmission();
Serial.print("FIFO count: ");
Serial.println(FIFO_COUNT);
}
void get_0_vals(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >= 6){
x0 = Wire.read() << 8;
x0 |= Wire.read();
y0 = Wire.read() << 8;
y0 |= Wire.read();
z0 = Wire.read() << 8;
z0 |= Wire.read();
}
Wire.endTransmission();
}
void read_FIFO_xyz(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >=6){
FIFO_DATA_X = Wire.read() << 8;
FIFO_DATA_X |= Wire.read();
FIFO_DATA_Y = Wire.read() << 8;
FIFO_DATA_Y |= Wire.read();
FIFO_DATA_Z = Wire.read() << 8;
FIFO_DATA_Z |= Wire.read();
}
Wire.endTransmission(true);
}

void zero_FIFO_data(){
FIFO_DATA_X = FIFO_DATA_X - x0;
FIFO_DATA_Y = FIFO_DATA_Y - y0;
FIFO_DATA_Z = FIFO_DATA_Z - z0;
}

void fill_arrays(){
for(int i = 0; i<10; i++){
should_FIFO_be_on();
if(FIFO_COUNT > 6){
read_FIFO_xyz();
zero_FIFO_data();
x_val[i] = FIFO_DATA_X;
y_val[i] = FIFO_DATA_Y;
z_val[i] = FIFO_DATA_Z;
}
}
}

void should_FIFO_be_on(){
check_FIFO_size();
if(FIFO_COUNT > 900){
disable_FIFO();
}
else{
enable_FIFO();
}
}

void loop() {
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
Serial.println("Start of Main");
check_FIFO_size();
should_FIFO_be_on();
if(FIFO_COUNT > 60){
fill_arrays();
}
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
for(int i = 0; i<10; i++){
Serial.print("Element ");
Serial.print(i);
Serial.print(" of x_val: ");
Serial.println(x_val[i]);
}
}


I have defined x_val as being of size ARRAY_SIZE which is defined as 10.



After calling my fill array function, Serial.print(sizeof(x_val)); returns 20. I have a feeling this is related to the fact that the values of FIFO_DATA_X are 2 Bytes, but that doesnt make sense to me because an int can be 2 or 4 byte.



Sorry in advance, my code is nowhere near perfect, but i can't use the MPU_6050 library so ive had to do my best from the data sheet and register map, plus my understanding of C++ and arduino is pretty poor (as is probably evident).










share|improve this question













Okay so i'm trying to read data from the gy521 sensor for arduino. I'm at the stage where i am reading data from the FIFO register, but when i try and assign the data to an array so i can calculate position, the array writes 20 values instead of the 10 i defined at the start. Here is my work in progress code:



#include <Wire.h>

#define MPU_6050 0x68
#define PWR_MGMT_1 0x6B
#define FIFO_EN 0x23
#define FIFO_COUNTH 0x72
#define FIFO_COUNTL 0x73
#define FIFO_R_W 0x74
#define SMPLRT_DIV 0x19
#define USER_CTRL 0x6A
#define GY_CONFIG 0x1B
#define SELF_TEST_X 0x0D
#define SELF_TEST_Y 0x0E
#define SELF_TEST_Z 0x0F
#define INT_EN 0x38
#define INT_STAT 0x3A
#define ARRAY_SIZE 10

int FIFO_OVERFLOW_STAT;
int FIFO_DATA_X;
int FIFO_DATA_Y;
int FIFO_DATA_Z;
int x0;
int y0;
int z0;
int FIFO_COUNT;
int x_val[ARRAY_SIZE];
int y_val[ARRAY_SIZE];
int z_val[ARRAY_SIZE];

void setup() {
Serial.begin(115200); //Start a serial interface
Wire.begin(); //set up i2c
Wire.beginTransmission(MPU_6050); //start transmission to slave
Wire.write(PWR_MGMT_1);
Wire.write(0); //Wake up
Wire.endTransmission(true); // Send byte in buffer
Wire.beginTransmission(MPU_6050);
Wire.write(SMPLRT_DIV);
Wire.write(5); //set the sample rate to 250hz (because 8000/1+31=250)
Wire.endTransmission(true);
disable_FIFO();
Wire.beginTransmission(MPU_6050);
Wire.write(USER_CTRL);
Wire.write(68); //enable the FIFO Line and Clear it
Wire.endTransmission(true);
check_FIFO_size();
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_X);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Y);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(SELF_TEST_Z);
Wire.write(7); //set up self tests
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(GY_CONFIG);
Wire.write(224); //Self test gyro and set Full Scale range to 250*/s
Wire.endTransmission(true);
Wire.beginTransmission(MPU_6050);
Wire.write(INT_EN);
Wire.write(16); //set up interrupt overflow of FIFO to prevent loss of data
Wire.endTransmission(true);
enable_FIFO();
get_0_vals();
}

void enable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(112); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void disable_FIFO(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_EN);
Wire.write(0); //enable the FIFO line for GY x,y and z
Wire.endTransmission(true);
}

void check_FIFO_overflow_stat(){
Wire.beginTransmission(MPU_6050);
Wire.write(INT_STAT);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 1, true);
if(Wire.available() >= 1){
FIFO_OVERFLOW_STAT = Wire.read() >> 4;
}
Serial.println(FIFO_OVERFLOW_STAT);
}

void check_FIFO_size(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_COUNTH);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 2, true);
if(Wire.available() >=2){
FIFO_COUNT = Wire.read() << 8;
FIFO_COUNT |= Wire.read();
}
Wire.endTransmission();
Serial.print("FIFO count: ");
Serial.println(FIFO_COUNT);
}
void get_0_vals(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >= 6){
x0 = Wire.read() << 8;
x0 |= Wire.read();
y0 = Wire.read() << 8;
y0 |= Wire.read();
z0 = Wire.read() << 8;
z0 |= Wire.read();
}
Wire.endTransmission();
}
void read_FIFO_xyz(){
Wire.beginTransmission(MPU_6050);
Wire.write(FIFO_R_W);
Wire.endTransmission(false);
Wire.requestFrom(MPU_6050, 6, true);
if(Wire.available() >=6){
FIFO_DATA_X = Wire.read() << 8;
FIFO_DATA_X |= Wire.read();
FIFO_DATA_Y = Wire.read() << 8;
FIFO_DATA_Y |= Wire.read();
FIFO_DATA_Z = Wire.read() << 8;
FIFO_DATA_Z |= Wire.read();
}
Wire.endTransmission(true);
}

void zero_FIFO_data(){
FIFO_DATA_X = FIFO_DATA_X - x0;
FIFO_DATA_Y = FIFO_DATA_Y - y0;
FIFO_DATA_Z = FIFO_DATA_Z - z0;
}

void fill_arrays(){
for(int i = 0; i<10; i++){
should_FIFO_be_on();
if(FIFO_COUNT > 6){
read_FIFO_xyz();
zero_FIFO_data();
x_val[i] = FIFO_DATA_X;
y_val[i] = FIFO_DATA_Y;
z_val[i] = FIFO_DATA_Z;
}
}
}

void should_FIFO_be_on(){
check_FIFO_size();
if(FIFO_COUNT > 900){
disable_FIFO();
}
else{
enable_FIFO();
}
}

void loop() {
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
Serial.println("Start of Main");
check_FIFO_size();
should_FIFO_be_on();
if(FIFO_COUNT > 60){
fill_arrays();
}
Serial.print("Size of x_val: ");
Serial.println(sizeof(x_val));
for(int i = 0; i<10; i++){
Serial.print("Element ");
Serial.print(i);
Serial.print(" of x_val: ");
Serial.println(x_val[i]);
}
}


I have defined x_val as being of size ARRAY_SIZE which is defined as 10.



After calling my fill array function, Serial.print(sizeof(x_val)); returns 20. I have a feeling this is related to the fact that the values of FIFO_DATA_X are 2 Bytes, but that doesnt make sense to me because an int can be 2 or 4 byte.



Sorry in advance, my code is nowhere near perfect, but i can't use the MPU_6050 library so ive had to do my best from the data sheet and register map, plus my understanding of C++ and arduino is pretty poor (as is probably evident).







c++ arrays arduino i2c fifo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 15:19









Alex Banton

112




112








  • 5




    sizeof(x_val) is 20 because it is sizeof(int) * 10. (int is 16-bit on Arduino)
    – Johnny Mopp
    Nov 9 at 15:21












  • This is a long way from a Minimal, Complete, and Verifiable example
    – Tim Randall
    Nov 9 at 16:26














  • 5




    sizeof(x_val) is 20 because it is sizeof(int) * 10. (int is 16-bit on Arduino)
    – Johnny Mopp
    Nov 9 at 15:21












  • This is a long way from a Minimal, Complete, and Verifiable example
    – Tim Randall
    Nov 9 at 16:26








5




5




sizeof(x_val) is 20 because it is sizeof(int) * 10. (int is 16-bit on Arduino)
– Johnny Mopp
Nov 9 at 15:21






sizeof(x_val) is 20 because it is sizeof(int) * 10. (int is 16-bit on Arduino)
– Johnny Mopp
Nov 9 at 15:21














This is a long way from a Minimal, Complete, and Verifiable example
– Tim Randall
Nov 9 at 16:26




This is a long way from a Minimal, Complete, and Verifiable example
– Tim Randall
Nov 9 at 16:26












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You are using sizeof operator, which does not return the number of elements in the array.




Returns size in bytes of the object representation of type.




Here's a solution:



sizeof(x_val) / sizeof(x_val[0])


This returns number of elements in the array. More on this here.






share|improve this answer























  • Ahh right okay that makes alot more sense, thanks.
    – Alex Banton
    Nov 9 at 15:29










  • @AlexBanton Please make sure to accept the answer for further references.
    – Ayxan
    Nov 9 at 15:31










  • Yeah I tried straight away it just told me I had to wait 5 minutes
    – Alex Banton
    Nov 9 at 15:37










  • Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
    – Peter
    Nov 9 at 23:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228487%2fthe-size-of-a-defined-array-is-changing-when-writing-values-to-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You are using sizeof operator, which does not return the number of elements in the array.




Returns size in bytes of the object representation of type.




Here's a solution:



sizeof(x_val) / sizeof(x_val[0])


This returns number of elements in the array. More on this here.






share|improve this answer























  • Ahh right okay that makes alot more sense, thanks.
    – Alex Banton
    Nov 9 at 15:29










  • @AlexBanton Please make sure to accept the answer for further references.
    – Ayxan
    Nov 9 at 15:31










  • Yeah I tried straight away it just told me I had to wait 5 minutes
    – Alex Banton
    Nov 9 at 15:37










  • Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
    – Peter
    Nov 9 at 23:54















up vote
2
down vote



accepted










You are using sizeof operator, which does not return the number of elements in the array.




Returns size in bytes of the object representation of type.




Here's a solution:



sizeof(x_val) / sizeof(x_val[0])


This returns number of elements in the array. More on this here.






share|improve this answer























  • Ahh right okay that makes alot more sense, thanks.
    – Alex Banton
    Nov 9 at 15:29










  • @AlexBanton Please make sure to accept the answer for further references.
    – Ayxan
    Nov 9 at 15:31










  • Yeah I tried straight away it just told me I had to wait 5 minutes
    – Alex Banton
    Nov 9 at 15:37










  • Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
    – Peter
    Nov 9 at 23:54













up vote
2
down vote



accepted







up vote
2
down vote



accepted






You are using sizeof operator, which does not return the number of elements in the array.




Returns size in bytes of the object representation of type.




Here's a solution:



sizeof(x_val) / sizeof(x_val[0])


This returns number of elements in the array. More on this here.






share|improve this answer














You are using sizeof operator, which does not return the number of elements in the array.




Returns size in bytes of the object representation of type.




Here's a solution:



sizeof(x_val) / sizeof(x_val[0])


This returns number of elements in the array. More on this here.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 15:29

























answered Nov 9 at 15:25









Ayxan

1,200115




1,200115












  • Ahh right okay that makes alot more sense, thanks.
    – Alex Banton
    Nov 9 at 15:29










  • @AlexBanton Please make sure to accept the answer for further references.
    – Ayxan
    Nov 9 at 15:31










  • Yeah I tried straight away it just told me I had to wait 5 minutes
    – Alex Banton
    Nov 9 at 15:37










  • Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
    – Peter
    Nov 9 at 23:54


















  • Ahh right okay that makes alot more sense, thanks.
    – Alex Banton
    Nov 9 at 15:29










  • @AlexBanton Please make sure to accept the answer for further references.
    – Ayxan
    Nov 9 at 15:31










  • Yeah I tried straight away it just told me I had to wait 5 minutes
    – Alex Banton
    Nov 9 at 15:37










  • Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
    – Peter
    Nov 9 at 23:54
















Ahh right okay that makes alot more sense, thanks.
– Alex Banton
Nov 9 at 15:29




Ahh right okay that makes alot more sense, thanks.
– Alex Banton
Nov 9 at 15:29












@AlexBanton Please make sure to accept the answer for further references.
– Ayxan
Nov 9 at 15:31




@AlexBanton Please make sure to accept the answer for further references.
– Ayxan
Nov 9 at 15:31












Yeah I tried straight away it just told me I had to wait 5 minutes
– Alex Banton
Nov 9 at 15:37




Yeah I tried straight away it just told me I had to wait 5 minutes
– Alex Banton
Nov 9 at 15:37












Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
– Peter
Nov 9 at 23:54




Bear in mind this doesn't work if x_val is a pointer (if an array is passed as an argument to a function, the function receives a pointer and not an array, so it is necessary to pass the size in some other way).
– Peter
Nov 9 at 23:54


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228487%2fthe-size-of-a-defined-array-is-changing-when-writing-values-to-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check