Finding Maximum If Statement

Hi all, I am running a loop that should find the max value. The condition should check to see if the current XAccel value is greater than the previous value of the array, and if it is move to a second if statement that determines if it is greater than the current max value, and replace it if it is. The problem is that the max value is being replaced even when the current XAccel value is lower. I am too nooby to know what I am doing wrong, any help would be much appreciated.

Thanks ya'll

XAccel.txt (222 Bytes)

The problem may be in the part of the code that you did not provide. Always provide complete programs or at least a complete example that exhibits the problem.

When posting code, use the autoformat function of the Arduino IDE, and post the entire program inline between code tags.

An if statement does not have to have an else portion.

x=x has no effect except to waste your effort in typing it.

Here's the rest of the code, thanks.

XAccel.ino (780 Bytes)

I don't understand the purpose of these two IF statements - how do they relate to each other

       if( i > 1 &&  XAccel[i] > XAccel[i-1] ) {
            
            if((XAccel[i]) > MaxValx){

and wouldn't the code be a lot simpler if you change this

           if((XAccel[i]) > MaxValx){
                MaxValX = Accel[i]; 
                Serial.print(MaxValX);
            }

            else { 
                MaxValX = MaxValX;

                Serial.print(MaxValX);
            }

to this

           if((XAccel[i]) > MaxValx){
                MaxValX = Accel[i]; 
            }
            Serial.print(MaxValX);

By the way, you don't seem to set MaxValX back to 0 before you start looking for the max value.

...R

Posting an ino attachment is not useful to me (and probably to others) when I use a mobile device. I explained how to post and this wasn't it.

... or just

           MaxValX = max(XAccel[i], MaxValX) ;
           Serial.print(MaxValX) ;