News:

Let's find out together what makes a PIC Tick!

Main Menu

ARRAY Comparison Question?

Started by Craig, Sep 10, 2021, 05:42 PM

Previous topic - Next topic

Craig

Hello
Using Positron8 Latest Version 4.0.0.8 Pic 18F47K40 Running @ 64Mhz Int Xtal.

The Problem I am having is with The Comparisons taking Place in Loop LP:
I First Load the various Readings into LArray1#1 etc in Loop LL: and that is working fine but, when it goes to Loop LP:
to Do a comparison between the various readings to find the Lowest Point then there are weird things happening!!

Is the Statement Syntax correct as Follows: If LHeight1 < LHeight2 And LHeight1 < LHeight3 Then
This Comparison Seems to be where the problem lies?
Is this the Right way to do a Comparison using < AND < ?????

Dim LArray1[4]              As Word
Dim DArray1[4]              As Word
Dim LHeight1                As Word
Dim LHeight2                As Word
ETC......
'____________________________________________________________________________
LL:
If LArray1#1 < LArray1#3 Then                     
     LHeight1 = LArray1#1                         
    Else
     LHeight1 = LArray1#3                         
     Print At 1,1, Dec3 LHeight1
    EndIf
Return

'_____________________________________________________________________________

LP:
If LHeight1 < LHeight2 And LHeight1 < LHeight3 Then
LHeightT = LHeight1                         
DISTResultT = (DArray1#1 + DArray1#3)         
DistT = (DISTResultT/2)     
Print At 1,1, Dec3 LHeightT                       
Print At 1,4, Dec3 DistT," "
Endif
Return
'_____________________________________________________________________________

Thanks for the Help!
Regards
Craig

Gamboa

Craig,

When I have several consecutive ANDs I use nested Ifs.
 For instance:

If LHeight1 < LHeight2 Then
  If LHeight1 < LHeight3 Then
    LHeightT = LHeight1                         
    DISTResultT = (DArray1#1 + DArray1#3)         
    DistT = (DISTResultT/2)     
    Print At 1,1, Dec3 LHeightT                       
    Print At 1,4, Dec3 DistT," "
  Else

  Endif

Else

Endif

Regards,
Gamboa
Long live for you

Craig

Morning Gamboa

Thanks I have tried nesting it with similar results but, will have a look at it again.
Regards
Craig

Stephen Moss

Perhaps you have just left out some code for brevity but in Loop LP you are performing a comparison with LHeight 2 without apparently assigning LHeight2 a value in Loop LL

Maybe I am not quite grasping what you are trying to do, but if you want to find the lowest value stored in the array could you not do something like...
Dim LowestVaule as byte   'Hold the Lowest value contained in the array
LowestValue = Array[0]

For x = 0 to (Array_Length - 1)
   If Array[x] < LowestValue then
   LowestValue = Array[x]
   EndIf
Next

Craig

Hi Stephen
Thank you for your help.

Yes I left some code out to keep the Thread as short as possible. I do need to find the lowest value
between Array Values so will try what you have suggested as this can be done on more readings and
will work very well instead of using so many loops.

Thanks again for your help.

Kind Regards
Craig