cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect max value of real time data in an array

Mohan1
Associate III

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.

0693W000003RTlMQAW.png

1 ACCEPTED SOLUTION

Accepted Solutions

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​

View solution in original post

8 REPLIES 8
sanju
Senior

#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.

@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.

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.

}

}

@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;

    }

}

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​

@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 ?

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

@sanju​ Now everything is cleared sir, thank you and special thanks to your patience for clearing my basic doubts.