Hi , i need to find the maximum value in array of 140 elements , and the index associated with this max value to use it in the code again .
please help me, and thank you so much
Hi , i need to find the maximum value in array of 140 elements , and the index associated with this max value to use it in the code again .
please help me, and thank you so much
Welcome,
Here is a small example: http://ideone.com/fKTrSo
bilalalagha:
thank you very much , but can i use this code in arduino ??
The fact that you had to ask this question tells me you need to spend some time studying the C language. There are a bunch of free online tutorials available. Spend a few days reading those and I really do believe you'll be able to answer your own question.
If you have two values, and one is bigger than the other, then the smaller one is not the maximum. The bigger one might be, but you'll need to check all the array elements to be sure.
bilalalagha:
Hi , i need to find the maximum value in array of 140 elements , and the index associated with this max value to use it in the code again .please help me, and thank you so much
If you start with an empty array initially, you can easily check before inserting a value to the array, whether it is higher than any value added before, and if so, just remember the value and the array index where you added the value.
If you don't start with an empty array, but with a full array with al values added before, you could "SORT" the array: In a naturally sorted array (ascending order), the highest value is the last value in the array.
The first thing to do is to post your code properly, all of it. I have seen some bizarre formatting but yours takes the biscuit.
I am working on my graduating project (solar tracking system) ,
my problem is :
i am using 4 LDR to find the best light intensity over 133 degree , after finding the maximum average LDR reading , how can i tell the servo to return to that position .
note that for every degree,I've stored the average reading from the LDRs into an array(avgarray)
here is part of my code :
//Â for (desired=180; desired >=47;desired -= 1) {Â Â Â Â Â // goes from 0 degrees to 133 degrees
  servo1.write(desired);                  // tell servo to go to position in variable 'desired'
  delay(10);                          // waits for the servo to reach the position
Â
 for ( i=0 ;i<134 ; i++) {
 int LDRReading1 = analogRead(LDR1);
 int LDRReading2 = analogRead(LDR2);
 int LDRReading3 = analogRead(LDR3);
 int LDRReading4 = analogRead(LDR4);
 AVG_LDR=(LDRReading1+LDRReading2+LDRReading3+LDRReading4)/4;
 AVG_LDR == avgarray ;
Â
  }
in one word , i want the servo to return to the maximum ldr reading from array !
thank you so much , and I apologize for not being word perfect in English
I would ditch the whole array, just do it when you go along. Storing 266 bytes just to get a maximum is kind of a wast.
byte getMaxAngle(){
 int maxReading = 0;
 byte maxAngle = 0;
 for([i = all angles]){
  //so i is the angles
 Â
  servo.write(i);
 Â
  //don't scream, don't use all caps
  int reading = 0;
 Â
  //Gammon tip 1, use array for things that are alike
  //So use an array for the LdrPins (and make then a const byte)
  for(byte j = 0; j < sizeof(LdrPins); j++){
   reading += analogRead(LdrPins[j]);
  }
  reading /= 4; //for average
 Â
  if(reading > maxReading){
   maxReading = reading;
   maxAngle = i;
  }
 }
Â
 return maxAngle;
}
Now I think about it, remove the line
reading =/ 4;
Yes, it averages it but it also removes resolution. Just use the sum of all the sensors to determine the max.
Here's another idea. Generate solar noon time as a function of GMT for the year at your location using data from this chart site. Position the array based on the calculated position of the sun. This would eliminate possible sensor confusion during certain cloud conditions.
But it does include the need for a RTC, making it indeed nice but wayyyyyyy more complex then it is now.
Two users with very similar problems, very similar usernames and very identical IP addresses.
What are the chance of that?
Thank you all I really appreciate your comments!
yes totally true , i am sorry for conflict.
AWOL:
Two users with very similar problems, very similar usernames and very identical IP addresses.What are the chance of that?
 int bestReadingValue=-1;
 int bestReadingLocation;
 for (int i=0 ;i<134 ; i++) {
  float value = analogRead(LDR1)+analogRead(LDR1)+analogRead(LDR1)+analogRead(LDR1);
  if(value > bestReadingValue) {
   bestReadingValue = value;
   bestReadingLocation = i;
  }
  }
 Serial.out.print("The sun appears to be at location ");
 Serial.out.print(bestReadingLocation);
 Serial.out.println();
Thank you so much !
PaulMurrayCbr:
int bestReadingValue=-1;
int bestReadingLocation;
for (int i=0 ;i<134 ; i++) {
float value = analogRead(LDR1)+analogRead(LDR1)+analogRead(LDR1)+analogRead(LDR1);
if(value > bestReadingValue) {
bestReadingValue = value;
bestReadingLocation = i;
}
}
Serial.out.print("The sun appears to be at location ");
Serial.out.print(bestReadingLocation);
Serial.out.println();
You know that's the same as in reaction 7? And don't use a float for the sum.
int array_values[]={1,2,3,4,5,6}; //declare array
int array_length=sizeof(array_values)/sizeof(int);
int max_value_of_array=0;
int num1,num2,num_max,i,j;
//get max value
for (i=0;i<(array_length-1);i++){
num1=array_values*;*