Pages: [1]   Go Down
Author Topic: software low pass filter debug help  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm trying to add a software low pass filter to a noisy sensor to make the output smoother. I think I have the idea right and it compiles and uploads fine, but my arduino seems to crash when I run it; no serial output when if I call the lowPassFilter function. I'm used to seeing run time errors when I usually program, and now that I don't I'm stuck figuring out my error. Is there any way to simulate run time somehow to figure out errors? Or do I need a hardware debugger? I'm new to arduino so I'm just using the standard arduino IDE. It's not very feature rich in terms of auto completion or built in documentation. Should I be using something else?

here is my code:

Code:

float data[10];

void setup(){
  Serial.begin(9600);

  // initialize array
  for (int i = 0; i < sizeof(data); i++){
    data[i] = 0.0; 
  }
}

void loop(){
  float current_value = readData();
  float filtered_value = lowPassFilter(current_value, data);

  Serial.print("Value = ");
  Serial.println(filtered_value);
}

float lowPassFilter(float new_data, float *data_array){
 
  float filtered_value = 0;
  float data_array_sum = 0;
 
  // remove oldest data in array (first index) and shift all other indexes to the left
  for (int i = 0; i < sizeof(data_array); i++)
    data_array[i] = data_array[i + 1];
 
  // add new data to array (last index)
  data_array[sizeof(data_array) - 1] = new_data;
 
  // get average value from the data array
  for (int i = 0; i < sizeof(data_array); i++){
    data_array_sum += data_array[i];
  }
  filtered_value = data_array_sum / sizeof(data_array);
 
  return filtered_value;
}


Any ideas of what my problem could be or how I could figure it out without a hardware debugger?

Thanks

« Last Edit: December 09, 2012, 01:09:04 pm by waspinator » Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not sure where current_data[] in setup came from.

You loop through every element when getting rid of the old one, but you access data[i+1] which might access an element past the end of the array.

It might be better to use something like avg_val = avg_val* (1-avg_factor)/avg_factor + new_val/avg_factor
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry that should have been just `data[]`.

hmm, so maybe my for loop should look like this:

Code:

  for (int i = 0; i < sizeof(data_array) - 1; i++)
    data_array[i] = data_array[i + 1];


edit: ok it runs now, but for some reason my array seems to be only of size 2, but when I print out sizeof(data) it is 40? And I thought I set it to 10...
« Last Edit: December 09, 2012, 02:02:41 pm by waspinator » Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sizeof returns the size of the array in bytes, not the number of elements.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh I see. Hmm I guess I'll just define the size of the array. I wish arduino had more libraries built in. Thanks.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// initialize array
  for (int i = 0; i < sizeof(data); i++){
    data[i] = 0.0; 

Quote
Sizeof returns the size of the array in bytes, not the number of elements.


However you can convert that to the number of elements:

Code:
// number of items in an array
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))

Now:

Code:
// initialize array
  for (int i = 0; i < NUMITEMS(data); i++){
    data[i] = 0.0; 
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: