Display biggest number

OC71

New Member
Hi

I am reading four voltages into separate variables and I just want to display the highest number.
The code below works OK but is there a simpler or neater way to do this?

Main:

readadc 0,b0
readadc 1,b1
readadc 2,b2
readadc 3,b3


if b0>b1 and b0>b2 and b0>b3 then sertxd (#b0,cr,lf)endif

if b1>b0 and b1>b2 and b1>b3 then sertxd (#b1,cr,lf)endif

if b2>b0 and b2>b1 and b2>b3 then sertxd (#b2,cr,lf)endif

if b3>b0 and b3>b1 and b3>b2 then sertxd (#b3,cr,lf)endif


goto main
 

edmunds

Senior Member
Yes, I think there is:

Code:
b4 = b3 min b2 min b1 min b0                   'to find the largest value
Lookdown b4, ( b0, b1, b2, b3 ), b5    'to check which animal contains the largest value and put 0 to 3 in b5 accordingly

Select case b5                                          'show only the highest number
case 0 : sertxd (#b0,cr,lf)
case 1 : sertxd (#b1,cr,lf)
case 2 : sertxd (#b2,cr,lf)
case 3 : sertxd (#b3,cr,lf)
endselect
Not tested and not written in actual editor :)

Edmunds
P.S. You can find similar example in online reference here.
 

hippy

Technical Support
Staff member
As noted -

MIN not only limits to a minimum value, but gives the highest value of two numbers.

MAX not only limits to a maximum value, but gives the lowest value of two numbers.

Code:
Symbol first   = b0
Symbol second  = b1
Symbol lowest  = b2
Symbol highest = b3

For first = 1 To 3
  For second = 1 To 3
    lowest  = first Max second
    highest = first Min second
    SerTxd( #first, " ", #second, 9 ) 
    SerTxd( "Lowest = ", #lowest, 9 ) 
    SerTxd( "Highest = ", #highest, CR, LF )
  Next
Next
Code:
1 1   Lowest = 1  Highest = 1
1 2   Lowest = 1  Highest = 2
1 3   Lowest = 1  Highest = 3
2 1   Lowest = 1  Highest = 2
2 2   Lowest = 2  Highest = 2
2 3   Lowest = 2  Highest = 3
3 1   Lowest = 1  Highest = 3
3 2   Lowest = 2  Highest = 3
3 3   Lowest = 3  Highest = 3
 

OC71

New Member
Thank you Edmunds and hippy.
@ Edmunds. I nearly understand your code. Still working at it.
@ hippy. I am completely lost, but I think I will eventually find my way.
 

edmunds

Senior Member
Thank you Edmunds and hippy.
@ Edmunds. I nearly understand your code. Still working at it.
@ hippy. I am completely lost, but I think I will eventually find my way.
Well, I tried to compile all the versions in MacAxePad and hippy wins with 81 byte, the original code is 84 bytes and my "optimisation" is 85 bytes. So as far as program space goes, @hippy wins 3 bytes from the original and you would actually loose a byte with my version compared to your original. I don't know about the speed. I like SELECT/CASE/ENDSELECT statements for more readable code, but I got there only after spending some time with incredibly complex and repeating IF/ENDIF ladders :). The min/max is my recent discovery as well because I, too had to compare 9 ADC readings.

Edmunds
 
Top