Delay is not working for applied filter on serial analog data sample

int pot = A0;
int value;
float v[4]={0.0};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pot,INPUT);

}

void loop() {
// put your main code here, to run repeatedly:

value = analogRead(pot);
float x = float(value)*5/1024;

v[0] = v[1];
v[1] = v[2];
v[2] = v[3];
v[3] = v[4];
v[4] = (5.410813636231793788e-1 * x)
+ (-0.49297369305844135345 * v[0])
+ (0.10136107908629476970 * v[1])
+ (0.59665868765467522383 * v[2])
+ (0.45077979658470224145 * v[3]);

float D = v[4]-v[3];
delay(100);
Serial.println(x);
Serial.print(" ");
Serial.print(v[4]);
Serial.print(" ");
Serial.println(D);
}

// I used this code to filter the signal but after adding delay it is not doing serial plotting or serial monitoring. Please give your valuable suggestions.

Your topic was MOVED to its current forum category as it is more suitable than the original

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

You have defined an array with 4 values. The indices of those values is 0,1,2, and 3.

v[4] is out of range.

The program is crashing and constantly restarting. Put some more print statements in setup to see this.

Also.. floats have about 6-7 digits of precision.

You're dreaming using values like this.

Now, I have corrected it and wrote: float v[3] = {0.0};

Now you have an array that can hold only 3 values with index values 0, 1 and 2

How are you accessing the array ?

So you've defined an even smaller array ???

I think by v[3] = {0.0} will initialize v[0], v[1], v[2], v[3] as zero.

Then you are wrong. It will allocate space to 3 array elements numbered 0, 1 and 2 and initialise them to zero

Better to read the man page :wink:

or take a view here to get some ideas:

The point is this array holds 3 (THREE) values.

In your code you are trying to access 5 (FIVE) values.

Computer will say NO to this.

If only it did !

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.