create filters

I have a project where the project is to create a filter. the purpose of this filter to stabilize the value of the input sensor pt-100. but during the process of summation want to do the value of the sum equal to the value of adc received. I beg its support for my project.

I created a sample program that I attach below.

void setup()
{
analogReadResolution(12);
Serial.begin(9600);
}

void loop()
{
int Sensorsuhu = analogRead(A0);
int suhu[10];
float temp1, mean1;

suhu[9]=suhu[8];
suhu[8]=suhu[7];
suhu[7]=suhu[6];
suhu[6]=suhu[5];
suhu[5]=suhu[4];
suhu[4]=suhu[3];
suhu[3]=suhu[2];
suhu[2]=suhu[1];
suhu[1]=suhu[0];
suhu[0]=Sensorsuhu;

temp1 = suhu[0] + suhu [1] + suhu[2] + suhu[3] + suhu[4] + suhu[5] + suhu[6] + suhu[7] + suhu[8] + suhu[9];
mean1 = temp1/10;
delay (1000);
float TeganganSensorsuhu = mean1 * (3300.0/4096.0);
Serial.print("biner :");
Serial.println(Sensorsuhu);
Serial.print("mean1 :");
Serial.println(mean1);
Serial.print("temp1 :");
Serial.println(temp1);
Serial.print("tegangan :");
Serial.println(TeganganSensorsuhu);

Welcome to the forums! Its easier to read your code if you put the code tags </> around it.

Perhaps you need to declare suhu outside of the loop() function or use the static keyword to prevent it being re-initialised every time loop() is run.

void loop()
{
  int Sensorsuhu = analogRead(A0);
  static int suhu[10];
  float temp1, mean1;

You will find your code is more compact and easy to read if you learn about for-loops eg

temp1 = suhu[0] + suhu [1] + suhu[2] + suhu[3] + suhu[4] + suhu[5] + suhu[6] + suhu[7] + suhu[8] + suhu[9];

Might become

temp1=0;

for (i=0; i<=9; i++) {
    temp1=temp1+suhu[i];
}

Thank you for suggesting if there are more questions I asked for his help.

Take a look at routines for running average. I will find a smart alternative

If you want a simple low pass digital filter you can use code like this:

float  temp1;

void setup ()
{
  temp1 = analogRead (A0) ;
}

void loop ()
{
  int input = analogRead (A0) ;
  temp1 += (input - temp1) * 0.1 ;  // classic 1 pole low-pass filter in numerically stable form.
  ....
}

Much less state is needed, since its an IIR filter, not an FIR filter (google/wikipedia are your friends!)

Your FIR filter takes 10 samples before its output is equal to the input even if the input is constant,
this filter is correctly initialized.

  suhu[9]=suhu[8];
  suhu[8]=suhu[7];
  suhu[7]=suhu[6];
  suhu[6]=suhu[5];
  suhu[5]=suhu[4];
  suhu[4]=suhu[3];
  suhu[3]=suhu[2];
  suhu[2]=suhu[1];
  suhu[1]=suhu[0];
  suhu[0]=Sensorsuhu;

There's two reason's not to do this. First is it's 'hard coded' and you can't easily change your filter size. Use a for(;:wink: loop. The other reason is that copying all of this data every time you take a reading is 'expensive'. It takes the processor a lot of time to read in a value and write it back out to the main memory. The better way of doing it is to record where you last saved a value and write the next value in the next cell. When you get to the last cell, the next value will be written to position 0.

Then you also have another two problems here:delay(1000); Don't do that. The first problem is that you can't do anything else while you are on delay. This is actually not a big problem for this small demo, but it will be significant if you try to add anything else. The real issue for this program is the other parts of the program don't execute consistently. There's a small variation in the duration of each loop. While delay() has a fixed duration, the sampling frequency will vary just a tiny bit. This means that any filter you design won't work quite like you expect.

Now go and read about FIR and IIR filters. This can be a pretty advanced subject but really you just need to understand the difference between them and why the impulse response curve is a good way to visualise the action of the filters. Can you draw the impulse response of your filter above?

Once you have understood all of the above points, come back here and I'll tell you how to design a filter that will blow your professor's socks off.

For designing FIR filters, I use http://t-filter.engineerjs.com/. This is the absolute best way of designing filters and comparing different options. It lets you see the impulse response and it has lots of other great output options. You can simply copy-paste the code from here into your Arduino program and have an amazingly good filter for any purpose.

The filter-design program is exceptionally good but you do need to understand a little bit about digital signal processing to understand what the limitations are. The chart looks great on the screen but if there's frequency components in your signal outside that chart, you need to know what effect they have.