12 years, 7 months ago.

kl25z code help needed...after days of attempts.

With the following code I can send the x accelerator output to the screen, but not the y accelerator output. I have tried two whole methods of extracting it, using a function call with a pointer, as well as using separate functions for each axis, but the same code that works for the x accelerator will not extract the y accelerator value and average it. I have left the common function in the code as well as my printf statements that I tried to use to debug the code. I am not an experienced c programmer, but I have spent days trying to solve this. I'd sure appreciate some help.

#include "mbed.h"
#include "MMA8451Q.h"


#define MMA8451_I2C_ADDRESS (0x1d<<1)

Serial pc(USBTX, USBRX); // tx, rx

const int number_times = 5;
float average_x[number_times], average_y[number_times], average_z[number_times];
//float array[10][10][10];
//float *array[10];
float average_value(float value, float *array);

float average_x_value(float value);
float average_y_value(float value);


int main(void) {

    float XaccO;
    float YaccO;
    float ZaccO;
    
 //  pc.printf("Hello World! \n \r");      
    for (int i = 0; i < number_times; ++i){
        average_x[i] = 0;
    }
//  pc.printf("Hello World! \n \r");  
    for (int i = 0; i < number_times; ++i){
        average_y[i] = 0;
    }
//  pc.printf("Hello World! \n \r");     
    for (int i = 0; i < number_times; ++i){    //   for (int i = 0; i <= number_times; ++i){
       average_z[i] = 0;
    }
    
    pc.printf("Hello World! \n \r");  

    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
    PwmOut rled(LED_RED);
    PwmOut gled(LED_GREEN);
    PwmOut bled(LED_BLUE);

    while (true) {
        rled = 1.0 - abs(acc.getAccX());
        gled = 1.0 - abs(acc.getAccY());
        bled = 1.0 - abs(acc.getAccZ());
                                             //      wait(1);
        XaccO = acc.getAccX();
        YaccO = acc.getAccY();
        ZaccO = abs(acc.getAccZ());
        
        pc.printf("Hello World again! \n \r");
       
                                        pc.printf("Xacc0= ");
                                        pc.printf("%f", XaccO);
                                        pc.printf("          ");  
 
                                           
        pc.printf("%f", average_x_value(XaccO));      //, average_x));
 //     pc.printf("%f", average_value(XaccO, average_x));       I was able to get x average with this function, using a pointer
                                        pc.printf("  = Average X  \r");
                                        pc.printf("\n");
                                        
                                        
                                         
                                        pc.printf("Yacc0= ");
                                        pc.printf("%f", YaccO);  
                                        pc.printf("           ");              
        pc.printf("%f", average_y_value(YaccO));     
 //     pc.printf("%f", average_value(YaccO, average_y));      my failed attempt to get y average using a common function and a pointer
                                        pc.printf("  = Average Y  \r");
                                        pc.printf("\n");
                                              //       pc.printf("%f", ZaccO);          
        wait(10);
                                        pc.printf(" \r");
                                        pc.printf("\n");

                                        pc.printf(" \r");
                                        pc.printf("\n");

                                        pc.printf(" \r");
                                        pc.printf("\n");
   }
}

//try using a common function with a pointer (with no success):
float average_value(float value, float *array)
{
                                              //pointer example:
                                              //  float aray=1.5;
                                              //  float *addpointer; //define *addpointer as a pointer to a variable that is a floating point number
                                              //  addpointer = &aray; // addpointer now points to the value that is named aray 
                                              //  pc.printf("addpointer=  ");
                                              //  pc.printf("%f",addpointer);
                                              //end of example 
 
                                        pc.printf("       float *array=  ");
                                        pc.printf("%f",array);
                                        pc.printf("       ");
     
    float total;
    int i;
  
    for(i = (number_times-1); i >= 0; i--)
    {
        array[i+1] = array[i];
    }
    array[0] = value;
                                                //    pc.printf("value");
                                                //    pc.printf("%f", value); // value and array[0] are the same
                                        pc.printf("\r");
                                        pc.printf("\n");
                                        pc.printf("array[0]= "); 
                                        pc.printf("%f", array[0]);
                                        pc.printf(" \r");
                                        pc.printf("\n"); 

    total = 0;
    for(i = 0; i<number_times; i++)
    {
        total = array[i] + total;
                                        pc.printf("array[i]=  ");
                                        pc.printf("%f", array[i]);
                                        pc.printf("      Total= ");
                                        pc.printf("%f", total);
                                        pc.printf("   number_times= ");
                                        pc.printf("%d", number_times);
                                        pc.printf("  ");
                                        pc.printf(" \r");
                                        pc.printf("\n");
    }
   
    return (total/number_times);   
} 
//calculate the average x value: 
float average_x_value(float value)
{
float total;
int i;
for (i = (number_times-1); i >= 0; i--)
{
average_x[i+1] = average_x[i];
}   
average_x[0] = value;
    
total = 0;
for (i = 0; i<number_times; i++)
{
total = average_x[i] + total;
}
    
    return (total/number_times);
    
} 
 
//calculate the average y value (failed)
float average_y_value(float value)
{
float total;
int i;
for (i = (number_times-1); i >= 0; i--)
{
average_y[i+1] = average_y[i];
}   
average_y[0] = value;
    
total = 0;
for (i = 0; i<number_times; i++)
{
total = average_y[i] + total;
}
    
    return (total/number_times);
    
} 

1 Answer

12 years, 7 months ago.

There is something wrong in the calculation of the average. The arrays should have indexes between 0 and (number_times-1). Your code accesses average_y[i+1] with i=number_times-1. This is outside the array. Same problem for x. Do you see any values for y coming from the sensor before averaging?

//calculate the average y value (failed)
float average_y_value(float value)
{
float total;
int i;
for (i = (number_times-1); i >= 0; i--)
{
average_y[i+1] = average_y[i];
}   
average_y[0] = value;
    
total = 0;
for (i = 0; i<number_times; i++)
{
total = average_y[i] + total;
}
    
    return (total/number_times);
    
} 

Both the Y and X values come successfully from the sensor. The code successfully averages the X value, but the same code does not average the Y value. The Y values do not get into the array. I am puzzled as to why the same code does not work for Y as it does for X. The function that uses the pointer also averages X correctly, but not Y. Very puzzled. I will re-think your answer about the indexes. Maybe I am not seeing this correctly. Thanks again.

posted by Tom Eldredge 29 Apr 2013

The code accesses the array between 0 and 4, as I read it (in the "for loop" ). The number_times constant is set to 5 at the beginning of the program. The for loop stops it from decrimenting when it reaches 0, as I understand it.

posted by Tom Eldredge 29 Apr 2013

The first loop will result in i values 4, 3, 2, 1, 0

for (i = (number_times-1); i >= 0; i--)
{
average_y[i+1] = average_y[i];
// The i values lead to 
// average_y[5] = average_y[4]; //illegal
// average_y[4] = average_y[3]; 
// average_y[3] = average_y[2]; 
// average_y[2] = average_y[1]; 
// average_y[1] = average_y[0]; 
}   
average_y[0] = value;

The second loop will result in i values 0, 1, 2, 3, 4

for (i = 0; i<number_times; i++)
{
total = average_y[i] + total;
// The i values lead to a total that is the sum of all 5 values
}
posted by Wim Huiskamp 29 Apr 2013