How to set a threshold value? How to know the threshold value?

Hi, I’m new to Arduino. I need to set a threshold value for my program. Actually have result from sensor in the form of sign wave. And I need to find the max and min of the sin wave. Then set a threshold by (min+max)/2. The problem is, I need to get first threshold from 1st min and max, then use this threshold as a reference for the next part of the wave. Then find another threshold and so on. ie… get threshold from previous data for the coming data.

I have got values from sensor. I did smoothing and find average, then also find max and min. But have so many values for min and max as attached. How to find which one to use? And then how to set threshold? Please help.

Thanks in advance.

1.jpg

Your little picture of text is useless. DO NOT POST PICTURES OF TEXT. Post text AS TEXT.

Your code obviously has flaws. If you need help fixing it, you MUST post it. Properly.

In addition to what @PaulS has said, does your threshold need to change while the program is running? If so, how does it know when a change is required?

Also it makes it much easier to help if you describe your project.

...R

Sorry that my question wasn’t clear.

From the image 123, I need to find Max 1 and Min 1 first. Then find threshold for this max and min ((max1+min1)/2). Set this threshold value for the next part of curve and print 1 if above threshold value and print 0 if below threshold value. Then max and min return to zero and start again to find next max and min. Similarly find the max and min of every curve for setting threshold for next curve.

123.jpg

Image from Reply #3 so we don’t have to download it. See this Simple Image Guide

ef8a14be8d8af54031823e51be69834d666f60ae.jpg

…R

You have not told us what your project is about.

What is the frequency of the wave you are trying to sample.

...R

A classic xyproblem...

You don't use thresholds for maximum/minimum. Your question should have been "how do I
detect maxima and minima in a sequence".

Local maxima/minima are defined by a change in direction - so you just need to record the direction
of last change and compare to most recent change.

For global maximum and minimum you set your original maximum variable to the most negative value,
and the minimum variable to the largest positive value - that way the first reading is guaranteed to
set both variables.

Hi everyone,

Thanks for the help. I have another problem to discuss now. I’m trying to get MAX and MIN. I need to find MIN and MAX from curve. I use count. I want to count to 10 and get the MAX value from the 10 value. Similarly for MIN. My code is below. But it shows some error.

while((counta<10),(countb<10))
{
average=filter(numReadings);
Serial.println(average);
if(average > sensorMax) sensorMax = average;
if(average<sensorMax)
{counta++;
Serial.print (“Less than Max\n”);
if (counta>10) A=true;
}

if (average < sensorMin) sensorMin = average;
if(average>sensorMin)
{countb++;
Serial.print (“Larger than Min\n”);
if (countb>10) B=true;
}

if((counta>10)&&(countb>10))break;
}

Output obtained

Expected output

while((counta<10),(countb<10))

Doesn’t work that way.

if((counta>10)&&(countb>10))break;

You did it right on that line.

As for any other errors, post a complete code and the error messages you see.

Thank you for the sudden help.

Actually it shows no error message. My expected output is to count until 10 and print all the 10 values and finally print the max value from these 10 values. Similarly for MIN.

Attached :13.jpg" is my approximately expected result.
But I get “12.jpg” which is counting more than 10 times.

Delta_G:
post a complete code

12.jpg

13.jpg

const int numReadings = 100;
double readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
double average = 0; // the average
double sensorMax=0; //Max sensor value
double sensorMin=1000; //Min sensor value
int inputPin = A0;
bool A,B;

double filter(int num)
{
double sum=0;
for (int i=0; i<num; i++) readings = 0;
for (int i=0; i<num; i++)
{
_ readings*=(analogRead(A0));_
_
// Serial.println(readings[readIndex]);_
_ sum+=readings;
}
return sum/(double)num;
}
void setup() {
// initialize serial communication with computer:
int counta=0;
int countb=0;
Serial.begin(9600);
pinMode (A0,INPUT);
sensorMax = analogRead(A0);
sensorMin = sensorMax;
// initialize all the readings to 0:
A=false;
B=false;*_

while(1)
{
* average=filter(numReadings);*
* Serial.println(average);*
if(average > sensorMax) sensorMax = average;
if(average<sensorMax)
{counta++;
Serial.print (“Less than Max\n”);
if (counta>10) A=true;
}

if (average < sensorMin) sensorMin = average;
if(average>sensorMin)
{countb++;
Serial.print (“Larger than Min\n”);
* if (countb>10) B=true;*
}
if((counta>10)&&(countb>10))break;
}
* Serial.print("\tMAX:\t");*
* Serial.print (sensorMax);*
* Serial.print("\tMIN:\t");*
* Serial.print (sensorMin);*
}
void loop() {
}

function has why error.
for (int i=0; i_ sum+=readings*; } *return sum/(double)num; }
please help me

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