How to detect max value of real time data in an array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-11 10:17 PM
I have connected a sine wave of amplitude 3V with 1.5V shifted above the zero to 12-bit ADC channel by using dsPIC33FJ32MC204 controller and stored in an array. I want to detect the max value out of five samples because after the five samples my data is repeating, so please give me any suggestion on it. I am getting output as a zero.
Solved! Go to Solution.
- Labels:
-
ADC
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-14 6:17 AM
If(I<arrayLenght)
{
} This case is to fill the array with adc values
When array will be filled completely then
If(I==arrayLength)
{
} This section will find the max value
Because until the array is not completed no need to check for max value​
​
The code you want to write will never fill the array completely because
Each time i will be 0​
​
You have take another variable for the loop to do by your way
​
​array[i] =adc_value;​
for(j=0;j<8;j++)
{
if(array[j] > max)
{
max = array[j];
}
sprintf(data1,"%.2f",max);
LCD_String_xy(1,1,data1);
max=0;
}
i++;​
if(i == 8)
{
i = 0;
}
This will work​
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-12 2:34 AM
#define arrayLength 5
unsigned int array[arrayLength]; //Array values should be updated with ADC values.
unsigned int i, max = 0;
void main()
{
for(i=0;i<sizeof(array)/sizeof(array[0]);i++)
{
if(array[i] > max)
{
max = array[i];
}
}
//here after the loop completion you can get the maximum value in variable max
}
You can find max number in a array by this way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-13 9:07 PM
@sanju​ Thanks for your quick response sir and your logic is working properly and getting max value accurately but facing one problem that as data is real time ,so how can i take infinite data in array means how can i update array values with ADC values?
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-13 11:14 PM
First of all, you can't store infinite data in array because RAM size of your device is 2 KB. so you have to define arrayLength
#define arrayLength 5(you have to set it first).
suppose you have this define it as 8
then code like this
#define arrayLength 8
unsigned int array[arrayLength]; //Array values should be updated with ADC values.
unsigned int i=0, max = 0;
void main()
{
while(1)
{
if(i<arrayLength )
{
//Read adc value and assign in array
array[i] = adc_value;
i++;
}
if(i == arrayLength )
{
for(i=0;i<sizeof(array)/sizeof(array[0]);i++)
{
if(array[i] > max)
{
max = array[i];
}
}
//here after the loop completion you can get the maximum value in variable max
i=0;
//Read adc value and assign in array
array[i] = adc_value;
i++;
}
// do your other tasks here
//Put some delay if required between adc samples.
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-14 5:42 AM
@sanju​ Thanks for your reply sir. I have to detect max value in both cases (i<arrayLength & i == arrayLength ),but in one condition i.e i<arrayLength max value detection is missing. And another thing is can i replace the whole code by writing the code like-
array[i] =adc_value;
for(i=0;i<8;i++)
{
if(array[i] > max)
{
max = array[i];
}
sprintf(data1,"%.2f",max);
LCD_String_xy(1,1,data1);
max=0;
}
if(i == 8)
{
i = 0;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-14 6:17 AM
If(I<arrayLenght)
{
} This case is to fill the array with adc values
When array will be filled completely then
If(I==arrayLength)
{
} This section will find the max value
Because until the array is not completed no need to check for max value​
​
The code you want to write will never fill the array completely because
Each time i will be 0​
​
You have take another variable for the loop to do by your way
​
​array[i] =adc_value;​
for(j=0;j<8;j++)
{
if(array[j] > max)
{
max = array[j];
}
sprintf(data1,"%.2f",max);
LCD_String_xy(1,1,data1);
max=0;
}
i++;​
if(i == 8)
{
i = 0;
}
This will work​
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-14 6:50 AM
@sanju​ I got your point sir but once j becomes greater than 8,it will never goes in for loop and my data is real time data, so I have to do it continuously. I think instead of i = 0, it should be j = 0 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-14 8:16 PM
Dear Mohan,
j is running a loop
i.e. for(j=0;j<8;j++)
so if j>8, your code is in while loop, so every time j will be initialized with j=0 to start the loop.
i and j are separately working i is filling array and j is finding max value by running a loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-14 10:41 PM
@sanju​ Now everything is cleared sir, thank you and special thanks to your patience for clearing my basic doubts.
