Brutus Cruciatus
Brutus Cruciatus

Reputation: 414

Moving average algorithm issue

I want to smooth values in realtime. For a reason this code seems to make the microcontroller(arduino - atmel) crash or not respond at least. This is my code

float tilttemp[] = { 1,1,1,1,1,1,1,1 };
float rolltemp[] = { 1,1,1,1,1 };
float pantemp[] = { 1,1,1,1,1 };
float tiltausgabe = 0;
float rollausgabe = 0;
float panausgabe = 0;
void trackerOutput()
{

    for(int i=0; i < sizeof(tilttemp) - 1; i++)
    {
       tilttemp[i] = tilttemp[i+1];
    }

    tilttemp[sizeof(tilttemp)] = tiltAngleLP;  //tiltAngleLP is a value that is available

    tiltausgabe = 0;
    for(int i=0; i < sizeof(tilttemp); i++)
    {
       tiltausgabe += tilttemp[i];
    }

    tiltausgabe = tiltausgabe/(sizeof(tilttemp));


    Serial.print(tiltausgabe);
    Serial.print(",");
    Serial.print(rollausgabe);
    Serial.print(",");  
    Serial.println(panausgabe);   
}

If I leave everything but

    Serial.print(tiltausgabe);
    Serial.print(",");
    Serial.print(rollausgabe);
    Serial.print(",");  
    Serial.println(panausgabe);  

out I get the output, so something is wrong in the first part.

Upvotes: 0

Views: 372

Answers (1)

Mike Dunlavey
Mike Dunlavey

Reputation: 40649

You don't want sizeof. You want countof. If you want to know what that is, it is:

#define countof(a) (sizeof(a)/sizeof((a)[0]))

and

array[ countof(array) ] = ...

will not set the last element of array. It will set the element beyond the last element.

Upvotes: 1

Related Questions